Created
October 28, 2011 04:18
-
-
Save NatashaTheRobot/1321608 to your computer and use it in GitHub Desktop.
This is the solution the MidpointFindingKarel problem in the online Stanford CS 106A class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 () { | |
putEndBeepers(); | |
while (frontIsClear()) { | |
takeLastBeeperWest(); | |
takeLastBeeperEast(); | |
} | |
} | |
private void putEndBeepers() { | |
move(); | |
putBeeper(); | |
while (frontIsClear()) { | |
move(); | |
} | |
turnAround(); | |
move(); | |
putBeeper(); | |
} | |
private void takeLastBeeperWest() { | |
if (facingWest()) { | |
move(); | |
if (noBeepersPresent()) { | |
putBeeper(); | |
} | |
turnAround(); | |
move(); | |
pickBeeper(); | |
turnAround(); | |
move(); | |
move(); | |
} | |
detectBeeper(); | |
turnAround(); | |
} | |
private void takeLastBeeperEast() { | |
if (facingEast()) { | |
pickBeeper(); | |
move(); | |
if(noBeepersPresent()) { | |
putBeeper(); | |
} | |
move(); | |
detectBeeper(); | |
turnAround(); | |
} | |
} | |
private void detectBeeper() { | |
while (noBeepersPresent()) { | |
if(frontIsClear()) { | |
move(); | |
} | |
if(frontIsBlocked()) { | |
turnAround(); | |
while(frontIsClear()) { | |
if(noBeepersPresent()) { | |
move(); | |
} | |
} | |
} | |
} | |
} | |
} |
On CodeHS, here is the way that I used without variables, including in for loops. Basically I go to the top middle, then go down and place a ball and turn to face the right way.
public class MidpointKarel extends SuperKarel
{
public void run()
{
while(frontIsClear())
{
turnLeft();
if(frontIsClear())
{
move();
}
if(frontIsClear())
{
move();
}
else
{
break;
}
turnRight();
move();
}
while(notFacingSouth())
{
turnLeft();
}
while(frontIsClear())
{
move();
}
putBall();
turnLeft();
}
}
public class MidpointFinderKarel extends SuperKarel {
public void run() {
// move from bothsides to mid. step by step
/* pre-condition: Karel travel between two beepers,
* collect the beeper it found, and
* put one 1Ave closer to mid.
* post-condition: Karel have both beepers by bothsides,
* it stand on the mid corner.
*/
// set pre-condition
// put beepers on bothsides.
putBeeper();
while (frontIsClear()) {
move();
}
putBeeper();
turnAround();
move();
/* travel between two beepers,
* collect the beeper it found, and
* put one 1Ave closer to mid.
*/
while (frontIsClear()){
while (noBeepersPresent()) {
move();
}
pickBeeper();
turnAround();
move();
putBeeper(); //put beeper on mid.
move(); //move to next Ave.
//This is when to tell if find midpoint.
if (beepersPresent()) {
pickBeeper();
turnAround();
move();
if (facingEast()) {
turnRight();
} else {
turnLeft();
}
}
}
}
}
thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So on CodeHS, we are allowed no variables.
There is a fairly simple recursive way to do it but since we are allowed for loops, I re purpose the loop control variables in this example.
public class MidpointKarel extends SuperKarel
{
}