《《斯坦福大学开放课程:编程方法》讲义》由会员分享,可在线阅读,更多相关《《斯坦福大学开放课程:编程方法》讲义(8页珍藏版)》请在金锄头文库上搜索。
1、Mehran SahamiCS 106AHandout #9September 28, 2007Example Karel ProblemsPortions of this handout by Eric RobertsRunning a steeple chaseIn class on Wednesday, we examined a SteepleChase program that allows Karel to run aSteeple Chase (like a hurdles race, but with arbitrarily large hurdles) where:Karel
2、 starts at position (1, 1), facing East.The steeple chase is guaranteed to be 9 avenues long.There can be arbitrarily many hurdles that can be of arbitrary size, locatedbetween any two avenues in the world.Karel should jump each hurdle one at a time.For example, if you were to execute the SteepleCha
3、se program, you would seesomething like the following before-and-after diagram:BeforeAfterBelow is the program listing of the SteepleChase program, which provides an exampleof various control structures in Karel, program decomposition, and comments (includingthe specification of pre- and post-condit
4、ions). 2 /* File: SteepleChase.java* -* Karel runs a steeple chase the is 9 avenues long.* Hurdles are of arbitrary height and placement.*/import stanford.karel.*;public class SteepleChase extends SuperKarel /* To run a race that is 9 avenues long, we need to move* forward or jump hurdles 8 times.*/
5、public void run() for (int i = 0; i 8; i+) if (frontIsClear() move(); else jumpHurdle();/* Pre-condition:Facing East at bottom of hurdle* Post-condition: Facing East at bottom in next avenue*after hurdle*/private void jumpHurdle() ascendHurdle();move();descendHurdle();/* Pre-condition:Facing East at
6、 bottom of hurdle* Post-condition: Facing East immediately above hurdle*/private void ascendHurdle() turnLeft();while (rightIsBlocked() move();turnRight(); 3 /* Pre-condition:Facing East above and immediately after hurdle* Post-condition: Facing East at bottom of hurdle*/private void descendHurdle()
7、 turnRight();moveToWall();turnLeft();/* Pre-condition:none* Post-condition: Facing first wall in whichever direction* Karel was facing previously*/private void moveToWall() while (frontIsClear() move();Creating a line of beepersConsider the problem of writing a method createBeeperLine, which creates
8、 a line ofbeepers beginning at Karels current corner and proceeding forward to the next wall. Forexample, if you were to execute createBeeperLine in an empty world, you would seesomething like the following before-and-after diagram:4321Before4321After123456123456The problem is slightly harder than i
9、t looks. Unless you think carefully about theproblem, it is easy to find yourself making a common programming error that keeps theprogram from working as youd like. For example, we might initially be inclined to solvethe problem as follows:private void createBeeperLine() while (frontIsClear() putBee
10、per();move(); 4 The problem here (as indicated by the bug picture next to the code), is that Karel willnot place a beeper on the last corner he encounters. When he reaches the final corner ofthe row hes in, his front will no longer be clear, so the while loop will immediately exitbefore a beeper is
11、placed on that final corner. This is perhaps easier to see when lookingat the Karels world after he executes the code above:Note that in the picture above, there is no beeper on the corner that Karel is standing on.To solve this example of a fence-post problem (such problems are further discussed in
12、the Karel course reader), we must make one more putBeeper() method call than calls tomove(), as shown below:private void createBeeperLine() while (frontIsClear() putBeeper();move();putBeeper();Cleaning up scattered beepersUnfortunately, sometimes Karels world gets a little messy, with beepers strewn
13、 around atvarious corners. We want to help Karel clean up his world by writing a program that hasKarel go through the world and pick up any beepers that may be scattered about. Weassume that:Karel starts at corner (1, 1) facing EastEach corner of Karels world may either be empty or contain at most o
14、ne beeper,and when Karel is done there should be no more beepers on any cornerKarel can finish his task at any location and orientationIf you were to execute your program, you would see something like the following before-and-after diagram on the next page. 5 BeforeAfterBelow is the program listing of