Game Help (pop)   (link) Game Help

What You Can Tell the Computer to Do

Continue reading below...

 
HTML5=JavaScript required
 
Score 0
 
   
 
Choose a (hard to guess) name:
 

 
New User  
 
   

When the computer does just what you tell it to do in a straight sequence, well, that's what computers do, but it's a lot of unnecessary typing when what you really want is for the computer to do the same thing again. One of the important tools in the programmer's toolbox is called iteration (I think that's Latin for "do it again"). Everybody knows how to iterate. When you go someplace, you put one foot in front of the other, then you bring that other foot around and put it in front, and then you do it again. and again. Notice I did not tell you the second time to put one foot in front of the other (and so on), I just said "do it again," and you knew what I meant. Computers know how to "do it again." Every programmer knows how to iterate. In Chomp you have two commands that outline the sequence of commands being iterated. At the front you put the repeat command with a number of times to repeat, and at the back side is a next command telling the computer to do that part again, the next iteration. Whatever is in the middle, between the repeat command and the next command, gets repeated that many times.

For example, suppose you wanted to tell Chomp to go right 24 steps, then down 24 steps, then left 24 steps, then finally up 24 steps. Depending on the maze, that might get you back to (or maybe only near) your starting point. Boring. But suppose you had a spiral maze, and you did those four steps a half-dozen times, you could go through the whole maze in 24 lines. Whew. Here's the first time around: You could type that into the game board above and run it. It would take a lot of typing to put all 24 lines in. Don't.

Instead, we do this the programmer way and insert 1.5 repeat 6 at the front and next at the end: Now when you run it, the nibbler chomps its way all around the maze to the middle. In the lower left corner you can see the current repeat info, at first "+6/3+" which means that it has 6 iterations to do, starting on line 3. After the "7 next " line lights up green, that info changes to +5/3+" and it restarts on line 3, and so on down to one, then it stops.

Note that the part of your script that is being repeated is indented slightly to the right on each line. This outline-like presentation is customary and usually done for you by the script programming environment.

If you looked at the "Nibbler Navigation" section of the ChompMan Commands Reference page, you may have noticed a couple navigation commands I didn't tell you about. Can you use one of those to make a shorter script that does the same thing?

There is one more command "exit" associated with iteration, and it exits the loop immediately, skipping over everything else before the "next" command, and aborting any remaining iterations. It doesn't make much sense without a conditional to control it, so we'll discuss it there.

Click here to learn more about sequence control.

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 3, 2020 April 1