Game Help (pop) (link) Game Help

# Tell the Computer to Choose

HTML5=JavaScript required

Score 0

Choose a (hard to guess) name:

New User

Now we can do all sorts of magic with numbers and expressions, but what's it good for, anyway? One useful thing you can do is give the right numbers to your loop iterations. Remember that spiral maze that we iterated around? We told the nibbler to go 24 steps in each direction, but toward the end there were less than a dozen pills to gobble up, and the nibbler spent unnecessary time sucking up steps it couldn't go through the wall to use. Suppose instead we used a variable to reduce the number of steps each time around:

1 spiral
2 let N = 24
3 repeat 6
4  right N
5  down N
6  left N
7  up N
8  let N = N-4
9  next
Try it. Even though the program is slightly longer, you can see how much faster it runs toward the end. Watch the value of N go down in the variables at the top edge of the game board. There are still some steps left over at the end of each run. I chose 24 based on the size of the game board, but there aren't that many pills in a row. Can you find a better starting number? Programming is like that, making some bad guesses, then fixing it. And trying again.

#### Conditionals

The most important thing you can do with variables is decide when to do a command (or group of commands) and when to do something else (or nothing). Chomp uses the same form of a conditional as many other programming languages -- including Java, which you will learn later in this course:
if (condition)
true-command
else
false-command
The black text here is part of the form, what you type in your script. The colored italics stand for whatever you want to do. The true-command is any command that you want to execute when the condition is TRUE, and not otherwise, while the false-command is any command that you want to execute when the condition is FALSE, but you can optionally omit the "else" and its false-command. Most programming languages have predefined names for TRUE and FALSE, but in Chomp you just use zero "0" for FALSE and "1" (any non-zero value works) for TRUE.

The condition is usually comparing some variable or expression to some other variable or a number or another expression, enclosed in parentheses, like "if (A>3)" or "if (X*X+Y*Y=R*R)" or you can assign the condition to a variable and then later use the variable as a condition:

let Q = (A<3)
...
if (Q)
true-command
The true-command and false-command here are any commands you like, including another If command or even a repeat...next sequence. You get exactly one command as either the true-command or the false-command but compounds like repeat...next sequence count as a single command, as do also an entire conditional including both the keyword "if" with its associated true-command and also the optional "else" with its associated false-command.

Our first example will be the familiar spiral maze so you can easily understand what is going on, even though using conditionals in this case does not actually make the program faster or smaller. Later we'll have more subtle situations where the conditionals are much more important (or even essential). Instead of doing all four sides of a complete trip around one turn of the spiral for each iteration, we will use a set of nested conditionals to decide which direction to go, then use a second inner loop to do one side at a time:

1 spiral
2 let N = 24
3 repeat 6
4   let S = 4
5   repeat S
6     if (S>3)
7       right N
8     else
9       if (S=3)
10         if (N=4)
11           exit
12         else
13           down N
14       else
15         if (S<2)
16           up N
17         else
18           left N
19     let S = S-1
20     next
21   let N = N-4
22   next
I probably need to explain some of this. First, everything inside the "repeat 6" in line 3, down to and including its "next" on line 22 is indented here one extra character space. Everything inside the "repeat S" in line 5, down to and including its "next" on line 20 is indented here another character space. Every command controlled by an "if" or "else" (but not the "else" itself) is indented another space. This means that the "if (S=3)" on line 9 down to its "else" on line 14 plus both contained "if+else" pairs with their controlled commands, all the way to and including line 18, is all one command controlled by the "else" on line 8, and indented accordingly. This example is (intentionally) pathological. Take a break, then come back and we'll work our way through it.

Line "5 repeat S" is executed when S=4 (the previous line), so even though S subsequently changes, the repeat on line 5 always runs four times. The first time through that repeat S=4, so the condition on line 6 (S>3) is TRUE, and the nibbler moves right 24 places. Because that condition was TRUE, the false-command on line 9 is skipped, but it's another conditional which ends on line 18. So the body of the "repeat S" in line 5, that is, everything between the repeat and its next, is exactly two commands, a composite "if (S>3)" on line 6 and the single "let S = S-1" to step S down from 4 to 1 on line 19. This is an important concept: although there are bunch of individual commands inside the composite "if (S>3)" on line 6, that "if" boxes them up and presents to its container a single (multi-line) command. You will see a lot of this. When computer professionals think about it abstractly, they call it abstraction, which is the notion that we can capture some kind of essence of what we are doing and give it a name, then not look inside again.

Anyway, the first time through, S is greater than 3 so the If-body (the true-command) is executed and the whole else-part is skipped. The next time through, S=3 so the If-body is skipped and the else-part is executed. It is the single composite command "if (S=3)" with its body and else-part, and the body is executed, but (line 10) N is not yet =4, so the else-part ("down N") is executed. And so on.

Do you see what I'm doing? I'm trying to think like the computer, one step at a time. It's really hard to keep track of everything, so often I will take a pencil and paper, and write down the values in each variable, then as these get updated, cross it off and write down the new value. I can do that with the current line number too, crossing it off as each line is executed. Crossing it off is better than erasing it, because you can look back on the paper and see what it was on the previous step. It helps if you use lined paper, where each line represents one step. So on line 4 of my paper, N=24 and S=4. so on line 6 I look up and see S=4 (greater than 3) so I do line 7. When I get down to line 19, I cross off the 4 and write in 3. Then next takes me back to line 6, and the condition is FALSE, so I skip line 7 and proceed to line 9, which is TRUE, so I look at line 10 (which is FALSE, because N=24 has not yet been crossed off) and we do line 13 instead. After you do this a few (or maybe many) times, you get the hang of it and you don't need the paper so often.

If you type this program into Chomp, you can add "0.5 slow" at the beginning, then it will run much slower and you can almost keep up with it in your mind as it executes. If you are still watching it when it gets to the end, unlike the previous (simple) version we did when I introduced iteration, which backed out of the dead end because it was still trying to go down and up and left on each iteration, this program will stop in the cul-de-sac. That's what the exit command does for us.

Next -- maybe I'll skip inputs for now and -- let's build our own maze...

I put a "Help" link at the top and bottom of each page to explain everything in more detail.

 Game Help (link) (ToC) Next >>

Begin Programming Page 5, 2020 February 25