Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: MidpointFindingKarel.java
- * -------------------------------
- * When you finish writing it, the MidpointFindingKarel class should
- * leave a beeper on the corner closest to the center of 1st Street
- * (or either of the two central corners if 1st Street has an even
- * number of corners). Karel can put down additional beepers as it
- * looks for the midpoint, but must pick them up again before it
- * stops. The world may be of any size, but you are allowed to
- * assume that it is at least as tall as it is wide.
- */
- import stanford.karel.*;
- /*
- * PRE-CONDITIONS: Karel starts in lower-left corner facing east
- * Square world
- * POST-CONDITIONS: Karel stands on a beeper in the center (or left/right
- * center) of First Street
- *
- */
- public class MidpointFindingKarel extends SuperKarel {
- public void run () {
- spiralAreaWithBeepers();
- orientAndHeadSouth();
- // double the beeper count on center, to find later
- putBeeper();
- // go to starting position
- goHome();
- unSpiralBeepers();
- goHome();
- goToBeeper();
- }
- private void spiralAreaWithBeepers() {
- while (noBeepersPresent()) {
- dropBeepersEast();
- dropBeepersNorth();
- dropBeepersWest();
- dropBeepersSouth();
- }
- }
- private void orientAndHeadSouth() {
- while (notFacingSouth()) {
- turnLeft();
- }
- while (frontIsClear()) {
- move();
- }
- }
- private void goHome() {
- while (notFacingWest()) {
- turnLeft();
- }
- while (frontIsClear()) {
- move();
- }
- if (leftIsBlocked()) {
- turnAround();
- } else {
- orientAndHeadSouth();
- turnLeft();
- }
- }
- private void unSpiralBeepers() {
- while (beepersPresent()) {
- pickBeepersEast();
- pickBeepersNorth();
- pickBeepersWest();
- pickBeepersSouth();
- }
- }
- private void goToBeeper() {
- while (noBeepersPresent()) {
- move();
- }
- }
- private void dropBeepersEast() {
- while (facingEast()) {
- if (noBeepersPresent()) {
- putBeeper();
- }
- if (frontIsClear()) {
- move();
- if (beepersPresent()) {
- turnAround();
- move();
- turnRight();
- move();
- }
- } else {
- turnLeft();
- move();
- }
- }
- }
- private void dropBeepersNorth() {
- while (facingNorth()) {
- if (noBeepersPresent()) {
- putBeeper();
- }
- if (frontIsClear()) {
- move();
- if (beepersPresent()) {
- turnAround();
- move();
- turnRight();
- move();
- }
- } else {
- turnLeft();
- move();
- }
- }
- }
- private void dropBeepersWest() {
- while (facingWest()) {
- if (noBeepersPresent()) {
- putBeeper();
- }
- if (frontIsClear()) {
- move();
- if (beepersPresent()) {
- turnAround();
- move();
- turnRight();
- move();
- }
- } else {
- turnLeft();
- }
- }
- }
- private void dropBeepersSouth() {
- while (facingSouth()) {
- if (noBeepersPresent()) {
- putBeeper();
- }
- if (frontIsClear()) {
- move();
- if (beepersPresent()) {
- turnAround();
- move();
- turnRight();
- move();
- }
- } else {
- turnLeft();
- }
- }
- }
- private void pickBeepersEast() {
- while (facingEast()) {
- if (beepersPresent()) {
- pickBeeper();
- }
- if (frontIsClear()) {
- move();
- if (noBeepersPresent()) {
- turnAround();
- move();
- turnRight();
- move();
- }
- } else {
- turnLeft();
- }
- }
- }
- private void pickBeepersNorth() {
- while (facingNorth()) {
- if (beepersPresent()) {
- pickBeeper();
- }
- if (frontIsClear()) {
- move();
- if (noBeepersPresent()) {
- turnAround();
- move();
- turnRight();
- move();
- }
- } else {
- turnLeft();
- }
- }
- }
- private void pickBeepersWest() {
- while (facingWest()) {
- if (beepersPresent()) {
- pickBeeper();
- }
- if (frontIsClear()) {
- move();
- if (noBeepersPresent()) {
- turnAround();
- move();
- turnRight();
- move();
- }
- } else {
- turnLeft();
- }
- }
- }
- private void pickBeepersSouth() {
- while (facingSouth()) {
- if (beepersPresent()) {
- pickBeeper();
- }
- if (frontIsClear()) {
- move();
- if (noBeepersPresent()) {
- turnAround();
- move();
- turnRight();
- move();
- }
- } else {
- turnLeft();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement