Advertisement
Guest User

Midpoint Finding Karel solution

a guest
Feb 1st, 2012
3,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. /*
  2.  * File: MidpointFindingKarel.java
  3.  * -------------------------------
  4.  * When you finish writing it, the MidpointFindingKarel class should
  5.  * leave a beeper on the corner closest to the center of 1st Street
  6.  * (or either of the two central corners if 1st Street has an even
  7.  * number of corners).  Karel can put down additional beepers as it
  8.  * looks for the midpoint, but must pick them up again before it
  9.  * stops.  The world may be of any size, but you are allowed to
  10.  * assume that it is at least as tall as it is wide.
  11.  */
  12.  
  13. import stanford.karel.*;
  14.  
  15. public class MidpointFindingKarel extends SuperKarel {
  16.  
  17. public void run() {
  18.     putFirstBeepers();
  19.     findMidpoint();
  20. }
  21.  
  22. private void putFirstBeepers() {
  23.     while (frontIsClear()) {
  24.         move();
  25.     }
  26.     putBeeper();
  27.     turnAround();
  28.     while (frontIsClear()) {
  29.         move();
  30.     }
  31.     putBeeper();
  32.     turnAround();
  33. }
  34.  
  35. private void findMidpoint() {
  36.     move();
  37.     while (noBeepersPresent()) {
  38.         move();
  39.         if (beepersPresent()) {
  40.             pickBeeper();
  41.             turnAround();
  42.             move();
  43.             putBeeper();
  44.             move();
  45.             if (beepersPresent()) {
  46.                 pickBeeper();
  47.                 turnAround();
  48.                 move();
  49.             }
  50.         }
  51.     }
  52. }
  53. }//**final brace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement