Pages

Monday, May 23, 2011

How to do the earthquake problem

/* The StoneMasonKarel subclass ,when finished solves the "earthquake"
* problem.
*/

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel
{
public void run()
{
while(frontIsClear())
{
job();
}
job();
}
private void job()
{
ascend();
turnAround();
descend();
}
private void ascend()
{
turnLeft();
while(frontIsClear())
{
move();
}
}
private void descend()
{
while(frontIsClear())
{
if(beepersPresent())
move();
else
putBeeper();
}
if(noBeepersPresent())
putBeeper();
turnLeft();
for(int i=0;i<4;i++)
move();
}

}

No comments:

Post a Comment