Created
October 28, 2011 03:57
-
-
Save NatashaTheRobot/1321586 to your computer and use it in GitHub Desktop.
This is the solution to the CheckerboardKarel problem in the online Stanford CS 106A class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: CheckerboardKarel.java | |
* ---------------------------- | |
* When you finish writing it, the CheckerboardKarel class should draw | |
* a checkerboard using beepers, as described in Assignment 1. You | |
* should make sure that your program works for all of the sample | |
* worlds supplied in the starter folder. | |
*/ | |
import stanford.karel.*; | |
public class CheckerboardKarel extends SuperKarel { | |
public void run() { | |
putBeeper(); | |
checkWall(); | |
while (frontIsClear()) { | |
beepersEast(); | |
beepersWest(); | |
} | |
} | |
private void beepersEast() { | |
while (facingEast()) { | |
move(); | |
if (frontIsClear()) { | |
move(); | |
putBeeper(); | |
} | |
upEast(); | |
} | |
} | |
private void upEast() { | |
if (frontIsBlocked()) { | |
if (noBeepersPresent()) { | |
turnLeft(); | |
if (frontIsClear()) { | |
move(); | |
turnLeft(); | |
putBeeper(); | |
} | |
} | |
else { | |
turnLeft(); | |
if (frontIsClear()) { | |
move(); | |
turnLeft(); | |
move(); | |
putBeeper(); | |
} | |
} | |
} | |
} | |
private void beepersWest() { | |
while (facingWest()) { | |
move(); | |
if (frontIsClear()) { | |
move(); | |
putBeeper(); | |
} | |
upWest(); | |
} | |
} | |
private void upWest() { | |
if (frontIsBlocked()) { | |
if (noBeepersPresent()) { | |
turnRight(); | |
if (frontIsClear()) { | |
move(); | |
turnRight(); | |
putBeeper(); | |
} | |
} | |
else { | |
turnRight(); | |
if (frontIsClear()) { | |
move(); | |
turnRight(); | |
move(); | |
putBeeper(); | |
} | |
} | |
} | |
} | |
private void checkWall() { | |
if (frontIsBlocked()) { | |
turnLeft(); | |
while (frontIsClear()) { | |
move(); | |
if (frontIsClear()) { | |
move(); | |
putBeeper(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment