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.
- *
- * --
- *
- * sonHaricBoncukDose(); is used for putting beepers along a
- * horizontal line, which can be translated from Turkish to
- * English as putBeepersExceptTheLast();
- *
- * boncukVarkenGitSonBoncuguAl(); is used for picking beepers
- * that are at the end of a beeper line, which can be translated
- * from Turkish to English as
- * asLongAsThereAreBeepersGoPickUpTheLastOne();
- *
- * the loop is stopped by changing the direction of Karel,
- * by making it turn left.
- *
- * this algorithm is built to be able to work even at a 1x1 world.
- */
- import stanford.karel.*;
- public class MidpointFindingKarel extends SuperKarel {
- public void run(){
- if(leftIsBlocked()&&rightIsBlocked()&&frontIsBlocked()){
- putBeeper();
- turnLeft();
- }
- while(facingEast()||facingWest()){
- sonHaricBoncukDose();
- boncukVarkenGitSonBoncuguAl();
- }
- turnLeft();
- if(noBeepersPresent()){
- move();
- }
- if(facingWest()){
- turnAround();
- }
- }
- private void boncukVarkenGitSonBoncuguAl() {
- while (beepersPresent()&&frontIsClear()){
- move();
- }
- turnAround();
- if (noBeepersPresent()){
- if(frontIsClear()){
- move();
- }
- }
- if(beepersPresent()){
- move();
- if(beepersPresent()){
- turnAround();
- move();
- turnAround();
- pickBeeper();
- }
- else{
- turnLeft();
- }
- }
- if(facingEast()||facingWest()){
- if(frontIsClear()){
- move();
- }
- boncukVarkenGitSonBoncuguAl();
- }
- }
- private void sonHaricBoncukDose() {
- while(frontIsClear()){
- putBeeper();
- move();
- }
- turnAround();
- move();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement