Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: MidpointFindingKarel.java
- * -------------------------------
- * The MidpointFindingKarel subclass places a single beeper on the
- * corner closest to the center of 1st Street(or on one of the two
- * central corners if 1st Street has an even number of corners).
- * See further description in assignment #1 handout.
- */
- import stanford.karel.*;
- public class MidpointFindingKarel extends SuperKarel {
- public void run() {
- if (frontIsClear()) {
- moveToMidpoint();
- }
- putBeeper();
- }
- private void moveToMidpoint() { // starting east, for every two corners Karel moves east, after reaching the east end of the street, Karel moves back one corner to the west.
- if (facingEast()) { // move up to two corners to the east, if possible
- move();
- if (frontIsClear()) {
- move();
- if (frontIsClear()) {
- moveToMidpoint(); //method calls itself
- } else {
- turnAround(); // turn around, if end of street in the east is reached
- }
- } else {
- turnAround(); // turn around, if end of street in the east is reached
- }
- }
- move(); // move back one corner to the west
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement