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.*;
- public class MidpointFindingKarel extends SuperKarel {
- public void run() {
- fillRow();
- moveBack();
- pickBeeper();
- turnAround();
- pickUpAllBeepersAtEnds();
- }
- /*
- * fillRow:
- * karel will place a beeper along the 1st row, except for the last block.
- *
- * pre-conditions: karel is in the first block of the first row and facing east
- *
- * post-conditions: karel will be in the last block of the first row facing east
- */
- private void fillRow(){
- while (frontIsClear()) {
- putBeeper();
- move();
- }
- }
- /*
- * moveBack:
- * karel will move back to her intital staring position.
- *
- * post-conditions: karel will be facing west
- */
- private void moveBack() {
- turnAround();
- while (frontIsClear()){
- move();
- }
- }
- /*
- * pickUpAllBeepersAtEnds:
- * karel will move to all the outer beepers and remove them till only the
- * center beeper remains.
- *
- * pre-condition: must be in 1st block of 1st row and facing east.
- *
- * post-condition: karel will end up at the end of the row, either on the east
- * or west side, depending on how many blocks are in a row.
- */
- private void pickUpAllBeepersAtEnds() {
- while (frontIsClear()){
- moveToOuterBeepers();
- checkIfOuterBeeper();
- }
- }
- /*
- * moveToOuterBeepers:
- * karel will conduct a test to determine which way to move to reach
- *the outer beeper farthest from her
- *
- * pre-conditions: karel must be facing east and standing on a block with no beeper.
- *
- * post-condition: karel will be on the outer beeper furthest from her starting point
- * and be facing east.
- */
- private void moveToOuterBeepers(){
- move();
- if (beepersPresent()){
- turnAround();
- move();
- turnAround();
- moveRightToOuterBeeper();
- } else {
- turnAround();
- move();
- turnAround();
- moveLeftToOuterBeeper();
- }
- }
- /*
- * moveRightToOuterBeeper:
- * karel will move right to reach the outer beeper
- *
- * pre-conditions: karel must be facing east and standing on a block with no beeper.
- *
- * post-condition: karel will be on the outer beeper furthest from her starting point
- * and be facing east.
- */
- private void moveRightToOuterBeeper(){
- move();
- while (beepersPresent()){
- move();
- }
- turnAround();
- move();
- turnAround();
- }
- /*
- * moveLeftToOuterBeeper:
- * karel will move left to reach the outer beeper
- *
- * pre-conditions: karel must be facing east and standing on a block with no beeper.
- *
- * post-condition: karel will be on the outer beeper furthest from her starting point
- * and be facing east.
- */
- private void moveLeftToOuterBeeper(){
- turnAround();
- move();
- while (beepersPresent()){
- move();
- }
- turnAround();
- move();
- }
- /*
- * checkIfOuterBeeper:
- * karel will check to see if the beeper she is standing on is an outer beeper.
- * if so then karel will remove it.
- *
- * pre-conditions: karel must be standing on a beeper and facing east.
- *
- * post-conditions1: karel will be standing in the same position she was in and be facing east.
- * the only difference is that there will be no beeper.
- *
- * post-condition2: karel will be at either end of the row with her front blocked, if the beeper
- * she was on turns out to be the center beeper.
- */
- private void checkIfOuterBeeper(){
- if (frontIsClear()){
- turnAround();
- move();
- if (beepersPresent()){
- turnAround();
- move();
- pickBeeper();
- } else {
- if (frontIsBlocked()){
- turnAround();
- move();
- pickBeeper();
- } else {
- checkOtherSide();
- }
- }
- }
- }
- /*
- * checkOtherSide:
- * another test to double check if beeper is center or outer
- */
- private void checkOtherSide(){
- turnAround();
- move();
- move();
- if (beepersPresent()){
- turnAround();
- move();
- pickBeeper();
- } else {
- while (frontIsClear()){
- move();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement