COMP203: Lecture 12


Syllabus | Homework and Assignments | Grading Rubric | Midterm Exam | Final Project

When circumstances allow, I'll be typing up bits of my lecture notes and posting them online. These may or may not bear any resemblance to the actual lectures.

Program Organization

Your midterm project will be more complicated than anything you've done so far. Ideally, it will be a program made up of several Logo procedures working together. You've seen how trace can help you keep track of which procedures are being run with what outputs. What other tools are available to you to manage your midterm program?

One tool commonly used by programmers is called a flowchart. Using a flowchart, you can diagram what choices the computer has at each decision point (if or ifelse statement). A flowchart can include as much or as little detail as you need.

Below is an example of a simple flowchart describing the process of waking up in the morning:

Notice that actions are described in rectangular boxes, decisions appear in diamond shaped boxes, and the start and end are in circles. Arrows indicate the "flow of control" in the "program". (I don't know what the green bullet shape is for.) In the second flowchart presented in this Wikipedia article, you can see that events in which the computer displays something are indicated by parallelogram shaped boxes. You can think of this as a rough correspondence between rectangles and operations, parallelograms and commands, and diamonds and predicates.

Maze Game

Simple maze, Fall 2011. Write this procedure!

Here is a flow chart describing one possible way to write a maze game. See if you can write a maze game that prints the responses shown in the parallelograms of the flow chart and makes decisions as described in the damonds.

There is more than one way to write this maze game. Did you use two procedures or one? Did you use if or ifelse? One way to program according to a flow chart is to use an ifelse statement for every diamond in the flow chart. In this case each branch out of the diamond becomes one input to ifelse. Each of these inputs to ifelse contain all the Logo commands described in the tree up to the point where the branches merge. (In our example, this is right before the end of the program.) Notice that this leads to "nested" ifelse statements.

to maze1
 print [You are lost in a dark cave.  Will you go left or right?]
 ifelse (equalp readword "right)~
        [print [You fell in a pit!]]~
        [print [You see a light ahead.  Will you go toward or away?]
         ifelse (equalp readword "toward)~
                [print [You are saved!]]~
                [print [You are lost!]]
        ]
end
Another way to program this maze game would be use an if statement and stop the procedure when the game ends:
to maze2
 print [You are lost in a dark cave.  Will you go left or right?]
 if (equalp readword "right)
    [print [You fell in a pit!] stop]
 print [You see a light ahead.  Will you go toward or away?]
 if (equalp readword "toward)~
    [print [You are saved!] stop]
 print [You are lost!]
end
The second method looks easier than the first, but procedures that use stop make big programs a little harder to read. The real drawback of the first procedure is that putting one ifelse inside the other makes it hard to read. We can get around this by using a helper procedure:
to maze3
 print [You are lost in a dark cave.  Will you go left or right?]
 ifelse (equalp readword "right)~
        [print [You fell in a pit!]]~
        [seelight]
end

to seelight
 print [You see a light ahead.  Will you go toward or away?]
 ifelse (equalp readword "toward)~
        [print [You are saved!]]~
        [print [You are lost!]]
end
Procedure maze3 is very easy to read -- if you go right, you fall in a pit. If you do anything else, the program follows the insructions given in seelight. By moving the second input to ifelse into its own procedure, we've made the parent procedure much easier to read.

Of course we could also write a separate procedure that prints You fell in a pit! In this example that seems unnecessary, but if the game offered multiple opportunities to fall in a pit that might be a useful helper procedure to write.

Quiz

Here is a flow chart describing a quiz program. It includes two features that the maze game didn't: First, it includes commands like make "score 0 in rectangular boxes. These commands change something within the computer's memory but have no visible effect to the person running the program. Second, the branches leaving the decision node (diamond box) rejoin later; you must print the score before you end the procedure. This makes the stop command even less helpful.

See if you can write a procedure that behaves as described in the flow chart.

smallnumberp

A homework assignment asked you to write a procedure that accepted one number as input and output "true if the number was less than 1, "false if the number was greater than or equal to 1, and to print an error message and output "false if the input was not a number. Draw a flow chart describing this procedure.

hauntedhouse

Draw a flow chart describing part or all of the haunted house game. In particular, notice that there are several points in the program that return you to the procedure associated with the house's entry way.