CSc 11:
Introduction to Computing
Problem Solving Before Programming and Knobby's World
First go through the multimedia for Problem Solving Before Programming in the CIMEL multimedia (cimel.cse.lehigh.edu) for The Universal Computer.
Then
you will start solving problems in Knobby's World. In lab, do the following
exercises (taken from the book). Show me your work before the end of lab.
Note: Knobby's World has been installed on the LAN: just select Windows start, then run, then enter knobby.
(You may also download Knobby's
World using a web browser, from the Knobby's World
page under my web page. Download it to drive C:\, the root or top
directory. After you've got it in C:, unzip it into directory C:\umach. (You shouldn't need to do this if you are
working in the lab or elsewhere on campus.)
Knobby's World laboratory exercises (from The Universal Computer):
Exercise 2.58: Start up Knobby's World. By default, it should load the situation file mountain.sit. By using move and left, navigate Knobby to the other side of the mountain.
Exercise 2.59: How can you instruct Knobby to take three steps backward? When or why might you want Knobby to do this?
Exercise 2.60: How can you instruct Knobby to take three steps forward, pick up a character, and return to his original position?
Exercise 2.61: What happens if Knobby attempts to perform a write at the beginning of a task? Why? How can a Knobby program avoid this behavior?
Exercise 2.62: What happens when Knobby performs a read from a corner that appears not to have a character on it? How can you use this behavior to erase other characters?
Exercise 2.63: Now for some action! Press the reset button, so that Knobby is back where he started, ready to start a task. Knobby's goal is to pick up a copy of the flag, F', climb up to the top of the mountain (where it should find ^'), plant the new flag there, then climb down to the other side of the mountain. What problem solving strategy might you well use to solve this problem?
Exercise 2.64: There is no single command to copy a character from a corner one at 1st Street and 2nd Avenue, the other at 1st Street and 3rd Avenue.) How can he do that?
Exercise 2.66: Without actually doing it, what do you suppose would happen if you sent Knobby to the flag, had him read it, and then copied b2 into b1? Why? Now perform the experiment to see if your analysis was correct. (Don't forget to reset the world first.)
Exercise 2.67: Reset again. This time plant a copy of the flag on top of the mountain, then come back down and erase the (original) flag you've left at the bottom. How can Knobby "empty" board b1 in order to erase the flag? Hint: see exercise 2.59.
Exercise 2.69: Define instructions right and reverse, using macros. (Enter ‘d’ to define a macro-instruction; enter ‘e’ to execute it.) Then send Knobby through the task of planting a flag on a mountain, using your new instructions. (Remember, you perform a left or a move as before; you perform a right or a reverse by pressing E, then telling Knobby which new instruction to execute.)
Exercise 2.70: Now that you have defined macro-instructions right and reverse, define a single new instruction for the complete flag-moving task. (Test it to make sure that it works correctly.) In what sort of circumstances might this new instruction be part of a solution to a still larger problem?
Exercise 2.75: Load situation file morning.sit. In this scenario, Knobby wakes up every morning when a newspaper N lands loudly on the porch just outside his bedroom. Write a program so that Knobby will get out of bed, go out and pick up the paper and bring it back to bed. (The newspaper should act like a physical object, i.e., after Knobby picks it up and brings it in, it should no longer be outside!) Hints: To load a situation file: under the World menu option, select Load, then double
click on file morning.sit. To solve the problem, use the top-down decomposition, analogical and/or copy and paste problem solving strategies. To
create your program, you could copy one of the other Knobby's World programs
(ending with extension .kno) to another name, e.g. elect Windows start, then run, then enter cmd, then:
C:\cd \umach
C:\umach>copy square.kno morning.kno
Then load it into Knobby's World (under the Program menu, select
Load, then double click on morning.kno), then edit it (under the Program menu, select Edit, then mouse click on the editor window on the right). Use
decomposition to break this problem down into smaller pieces that can be
defined as instructions, which get called by the top level main instruction or
other instructions. Try creating a few utility instructions, such as turning
right, or moving a particular number of squares. Comment your code!
Regarding Knobby's World programming, a student asked:
How can you break
an infinite loop? My program was stuck in an infinite loop and the only
way I could stop it was to quit the program. Thanks.
Press ctrl-alt-del (hold down the Ctrl
key, then hold down the Alt key, then press the Del key). Windows will bring up
a list of tasks. Click on Knobby's World, then click on end task. This may not
work right away if you're in a "tight" (fast) loop, so you may have
to wait or do this more than once. BTW, this is how you can kill any task
that's not behaving well, under Windows.
Prof. Blank