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 () {
- makeDiagonalRight () ;
- putBeeper () ;
- setUpForNextDiagonal () ;
- makeDiagonalLeft () ;
- putBeeper () ;
- searchingForMidpoint () ;
- }
- private void makeDiagonalRight () {
- while (frontIsClear()) {
- putBeeper () ;
- move () ;
- turnLeft () ;
- move () ;
- turnRight () ;
- }
- }
- private void setUpForNextDiagonal () {
- turnAround () ;
- while (frontIsClear()) {
- move () ;
- }
- turnAround () ;
- }
- private void makeDiagonalLeft () {
- while (frontIsClear()) {
- if (noBeepersPresent()) {
- putBeeper () ;
- }
- move () ;
- turnRight () ;
- move () ;
- turnLeft () ;
- }
- }
- private void searchingForMidpoint () {
- turnLeft () ;
- while (frontIsClear()) {
- if (beepersPresent()) {
- move () ;
- }
- move () ;
- if (beepersPresent()) {
- turnLeft () ;
- move () ;
- turnLeft () ;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement