Sunday, February 2, 2014

More Karel sovling problems in his little world... finding the middle...

Ok so if I had to do this in the real world with all syntax and methods available I would probably do something like:
int count = 1
findCount() {
while frontIsClear() {
move();
int count = count + 1
}
turnAround();

int steps = count/2

while steps != 0 {
move():
int steps = steps-1

putBeeper

however the rules of this assignment state that Count and int are not a part of the karel program so you have to do it with logic.

The simplest method I could think of was to fill in the row with Beepers
from farthest point in so go to wall put beeper turn around go to wall or Beeper  stop turn around and put beeper back and forth till Karel is standing in the middle spot
he moves forward one spot and starts picking up the beepers then when he hits a wall turns around and moves till he hits a beeper again (this one is the middle) he moves one more step and picksup the beepers till he hits a wall
then turns around and moves back to the middle by stopping on the beeper.

Final solution with some help with the && statement from Arnos Blog:

* 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 {

    // You fill in this part

    public void run() {
        findWalls();
        while (noBeepersPresent()){
            fillToMiddle();
        }
        pickUpExtraBeepers();
        keepMiddle();
        pickUpExtraBeepers();
           
    }
    // this method defines the far west and east of Karel's world. 
    // Precondition karel facing east
    // Postcondition karel will be one avenue to the west of farthest wall on the east facing west
    private void findWalls() {
        putBeeper();
        while (frontIsClear()){
            move();
        }
        putBeeper();
        turnAround();
        move();
        }
   
    // this will fill in the row east to west and back till Karel can't move due to beepers or wall
    private void fillToMiddle() {
        while (frontIsClear() && noBeepersPresent()) {
            move();
        }
        turnAround();
        move();
        putBeeper();
        move();
    }
   
    //Karel is standing on a beeper one avenue past the middle
    private void pickUpExtraBeepers() {
        while (frontIsClear()) {
            pickBeeper();
            move();
        }
        pickBeeper();
        turnAround();
        while (noBeepersPresent()) {
            move();
        }
    }
   
    //keepMiddle just moves karel one past the middle beeper so it is kept to the otherside of it.
    private void keepMiddle() {
        move();
    }
}

So that completes my first Assignment... now on the Lecture 4 and delving into actual java.
    

No comments:

Post a Comment