Stanford Karel: Unit 12: Lesson 1

//Karel finds the midpoint in the world. It is not the center.

function main() {

markWalls();
putBeepersDown();
turnAround();
move();
putBeeper();
turnAround();
goToWall();
turnAround();
lastBeeperStanding();

}

//Karel moves to end walls, marking them with beepers

function markWalls() {

goToWall();
putBeeper();
turnAround();
goToWall();
putBeeper();
turnAround();
move();

}

function goToWall() {
while(frontIsClear()) {
move();
}
}

 

//Karel starts putting beepers down to find the midpoint of the world.

function putBeepersDown() {
while(noBeepersPresent()) {
move();
if (beepersPresent()) {
turnAround();
move();
putBeeper();
move();
}
}
}

function lastBeeperStanding() {
while (frontIsClear()) {
if (beepersPresent()) {
pickBeeper();
}
move();
}
pickBeeper();
}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Facebook
  • TwitThis
  • StumbleUpon
  • LinkedIn
  • Digg
  • del.icio.us
  • Squidoo
  • Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>