{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

06-Karel-Climbs-Mountains

# 06-Karel-Climbs-Mountains - CS106A Handout 06 April 1st...

This preview shows pages 1–4. Sign up to view the full content.

CS106A Handout 06 Spring 2011 April 1 st , 2011 Karel Climbs Mountains Coding examples by Eric Roberts. Imagine Karel is training to climb Mount Everest, but has decided to start small and climb comparatively small foothills like those to the west of Stanford campus. We want Karel to do whatever it takes to initially climb this mountain: but then to develop a general strategy to scale all mountains, even ones as intimidating as this lifelike replica of Mount Everest: Presented here are the series of programs I’ve work through in lecture. Each program is an improvement over the previous one, in that it’s either easier to understand, makes better

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 use of programming language constructs, or advances us toward a more general solution to a more difficult problem. Karel The Mountain Climber: Take 1 The program below defines the MountainKarel1 class to contain nothing other than a run method. The run method makes use of the turnLeft , move , and putBeeper built- ins to achieve the desired result, which is to ascend, mark, and descend a mountain of height two. public class MountainKarel1 extends Karel { public void run() { turnLeft(); move(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft(); move(); turnLeft(); turnLeft(); turnLeft(); move(); putBeeper(); move(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft(); move(); turnLeft(); turnLeft(); turnLeft(); move(); turnLeft(); } } Strengths The program works without much fuss, provided you’re only concerned about climbing mountains of height 2. Weaknesses It brute-force solves a very specific problem even though mountains of height 3, 10, or 100 are as easily scaled—at least in terms of the recipe that Karel should be following to get the job done. This part coaches Karel to climb up the mountain. Recall that three consecutive calls to turnLeft imitate the currently nonexistent turnRight . After claiming (a.k.a. putBeeper ing) the mountain as one that belongs to Karel’s homeland, this sequence of method calls manages to get Karel back down to sea level.
3 The program’s narrative isn’t fully clear about what’s happening. What’s disguised via calls to turnLeft and move should really be calls to turnRight , stepUp , and stepDown . An improved program would include new methods—helper methods you write—that are framed in terms of the built-ins and previously defined helper methods to make the program that much more easily read and digested by those reading it. Karel The Mountain Climber: Take 2 Here’s the first of a series of improvements we’ll apply to the same program to make it more readable and more robust: public class MountainKarel2 extends Karel { public void run() { turnLeft(); move(); turnRight(); move(); turnLeft(); move(); turnRight(); move(); putBeeper(); move(); turnRight(); move(); turnLeft(); move(); turnRight(); move(); turnLeft(); } private void turnRight() { turnLeft(); turnLeft(); turnLeft(); } } This version elects to extend Karel’s very small vocabulary by defining a turnRight method. By doing so, the run method above can more clearly articulate the recipe that Karel must follow to scale mountains of height 2.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}