Posted on Leave a comment

MidPointFindingKarel – My Solution

I had a pretty challenging two weeks of solving this problem. I finally found a solution. I am so relieved.

[code lang=”javafx”]

/*
* 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(){

/*
* Pseudocode
* 1.laybeepers
* 2.pick them up one end to the other
*
* Decomposition
* * solves one problem
* * 1-15 lines
* * good names
* * comments /preconditions
* * /postconditions
*
*/
LayBeepers();
BackToStart();

/* FindTheMiddle
* here is the method that
* does the work
*/
FindTheMiddle();
}

/*
* LayBeeper
* put beepers from one end to the other
* pre: unlimited beepers, front is clear
* post: wall in front, beeper where iam standing
*/
private void LayBeepers(){
while(frontIsClear()){
putBeeper();
move();
}
putBeeper();
}

/* BackToStart
* moves Karel to the starting position
* pre: front is blocked
* post:front is clear
*/
private void BackToStart(){
turnAround();
MoveToWall();
turnAround();
}

/* MoveToWall
* keep moving until karel hits a wall
* pre: front clear
* post : front blocked
*/
private void MoveToWall(){
while(frontIsClear()){
move();
}
}

/* FindTheMiddle
* Finds the midpoint of a line
* Pre: beepers present in front
* post: no more beepers in front
*
*/
private void FindTheMiddle(){
if(beepersPresent()){
pickBeeper();
}
while(noBeepersPresent()){
GotoTheOtherEnd();
if(frontIsClear()){
checkabeeper();
}
pickBeeper();
}
}

/*
* Gootheotherend
* traverse the line of beepers
*/
private void GotoTheOtherEnd(){
FirstBeeper();
EndingBeeper();
}

/* FirstBeeper
* go to the first beeper
*/
private void FirstBeeper(){
if(noBeepersPresent()){
move();
}
}

/* endingbeeper
* check the lastbeeper
*
*/
private void EndingBeeper(){
while(frontIsClear()){
move();
}
turnAround();

while(noBeepersPresent()){
move();
}
}

/* checkabeeper
* checks if there is still a beeper for the last
* time
*/
private void checkabeeper(){
if(beepersPresent()){
move();
if(beepersPresent()){
MoveBack();
} else {
MoveBack();
putBeeper();
}
}
}

/* Moveback
*
*/
private void MoveBack(){
turnAround();
move();
turnAround();
}
}

[/code]

Posted on Leave a comment

My MidpointFindingKarel Algorithm

I am trying to solve the MidpointFindingKarel problem in the assignment 1 of Stanford’s CS106a. As of now, It is in my head but it still is not working perfectly.

I can lay down the beepers and have Karel move and pick them up one by one from one end to the other but I can’t have him stop at the middle.

  • lay down the beepers
  • move to the starting position
  • pick the beepers one at a time from one end to the other
  1. Lay down the beepers is easy. ┬áIt is just a “while” loop with the put beeper command in the body
  2. move to the starting position is just a “turnAround” and “movetowall” method
  3. This is the tough one which probably needs more decomposition. Still a work in progress.