09S-Section-Solution

09S-Section-Solution - private void handleBorder move...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106A Handout 09S Spring 2011 April 6 th – 8 th , 2011 Section Solution Solution 1: Karel The Robot The solution below (provided to me by Steve Cooper and Jason Ma from last quarter’s CS106A offering) is just one of many acceptable approaches. /* File: InnerBorderKarel.java */ import stanford.karel.*; public class InnerBorderKarel extends SuperKarel { public void run() { moveUpRow(); for(int i = 0; i < 4; i++) { handleBorder(); nextPosition(); } } // Assumes Karel starts one avenue before the first beeper to // be placed in this line of the border. Places beepers until // Karel reaches a wall, but does not place a beeper on the last // corner (where Karel is facing the wall).
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: private void handleBorder() { move(); while (frontIsClear()) { // We check for any existing beepers, so we don't put // two beepers on any of the "corners" of the border if (noBeepersPresent()) { putBeeper(); } move(); } } // Moves Karel up one row while keeping the same orientation private void moveUpRow() { turnLeft(); move(); turnRight(); } // Assumes Karel is facing a wall at the end of line of placed // beepers and repositions Karel to be facing in direction of next // line in the border of beepers that needs to be placed private void nextPosition() { turnRight(); move(); turnRight(); move(); turnRight(); } }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online