0% found this document useful (0 votes)
51 views6 pages

Assignment 1: Advanced Programming

The document contains the code for several Karel programs: 1. PotatoKarel collects lines of beepers in a grid pattern. 2. SkylineKarel collects lines of beepers while navigating obstacles. 3. LumberjackKarel plants beepers in a grid pattern. 4. AriadneKarel navigates a maze to collect all beepers. 5. EggHuntKarel navigates a more complex maze to collect all beepers.

Uploaded by

jaswanth1017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

Assignment 1: Advanced Programming

The document contains the code for several Karel programs: 1. PotatoKarel collects lines of beepers in a grid pattern. 2. SkylineKarel collects lines of beepers while navigating obstacles. 3. LumberjackKarel plants beepers in a grid pattern. 4. AriadneKarel navigates a maze to collect all beepers. 5. EggHuntKarel navigates a more complex maze to collect all beepers.

Uploaded by

jaswanth1017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 1

Advanced Programming
Gautam Kumar

1
PotatoKarel

public class PotatoKarel extends SuperKarel {


public void run() {
collectLineOfBeepers();
/* Horizontal scan */
for (int i = 1; i < 9; i++) {
/* check odd row */
if (i % 2 == 1) {
turnLeft();
move();
turnLeft();
}
/* check even row */
else {
turnRight();
move();
turnRight();
}
collectLineOfBeepers();
}
}
private void collectLineOfBeepers() {
while (frontIsClear()) {
if (beepersPresent()) {
pickBeeper();
}
move();
}
}

SkylineKarel

public class SkylineKarel extends SuperKarel {


public void run() {
for(int i = 0; i < 12; i++) {
turnLeft();
collectLineOfBeepers();
jumpHurdle();
}
}
private void collectLineOfBeepers() {
while (rightIsBlocked()) {
if (beepersPresent()) {
pickBeeper();

2
}
if (frontIsBlocked() && leftIsClear()) {
turnLeft();
}
else if (frontIsBlocked()) {
turnAround();
}
move();
}
}
private void jumpHurdle() {
turnRight();
move();
turnRight();
}
LumberjackKarel
public class LumberjackKarel extends SuperKarel {
public void run() {
turnLeft();
while (frontIsClear()) {
move();
}
turnRight();
while (leftIsBlocked()) {
if (noBeepersPresent()) {
putBeeper();
}
move();
if (leftIsBlocked() && noBeepersPresent()) {
putBeeper();
}
turnAround();
move();
while (beepersPresent()) {
pickBeeper();
turnAround();
move();
putBeeper();
turnAround();
move();
}
turnAround();
move();
}
}

AriadneKarel
public class AriadneKarel extends SuperKarel {
public void run() {

scan();
turnRight();
moveDown();
private void moveDown() {

scan();
moveOver();
}
private void moveOver() {

turnRight();
pickBeeper();
move();
turnLeftScan();
turnLeft();
for(int i=0;i<2;i++)
{
pickBeeper();
move();
}
pickBeeper();
turnLeft();
move();
pickBeeper();
turnRightMove();
pickBeeper();
turnLeft();
for(int i=0;i<2;i++)
{
move();
pickBeeper();
}
turnRightMove();
turnRightScan();
turnLeftScan();
pickBeeper();
private void scan(){
while(frontIsClear())
{
if(beepersPresent()){
pickBeeper();
}
move();
}
}
private void turnLeftScan(){
turnLeft();
scan();
}
private void turnRightScan(){
turnRight();
scan();
}
private void turnRightMove(){
turnRight();

4
move();
}
EggHuntKarel
public class EggHuntKarel extends SuperKarel {
public void run() {
move();
turnLeftScanMove();
pickBeeper();
turnRightMove();
turnRightMove();
pickBeeper();
turnRight();
scan();
turnLeftScanMove();
turnAround();
turnRightMove();
pickBeeper();
turnRightMove();
pickBeeper();
checkBeeper();
turnRightMove();
turnRight();
scan();
turnLeftScanMove();
move();
pickBeeper();
turnAround();
scan();
turnLeftScanMove();
move();
pickBeeper();
turnLeftMove();
turnRight();
checkBeeper();
turnRightMove();
move();
turnRightMove();
pickBeeper();
move();

}
private void scan() {
for (int i = 0; i < 3; i++) {
move();
}
}
/* to check if front is clear and to pick beeper */
private void checkBeeper() {
while (frontIsClear()) {
move();
}
if (beepersPresent()) {
pickBeeper();
}
}
/* to turn right and to move */
private void turnRightMove(){
turnRight();
move();
}
/* to turn Left and to move */

5
private void turnLeftMove(){
turnLeft();
move();
}
/* to turn Left and to scan */
private void turnLeftScan(){
turnLeft();
scan();
}
/* to turn Left and to move and scan*/
private void turnLeftScanMove(){
turnLeftScan();
turnLeftMove();
}

You might also like