|Game Help (pop)||(link) Game Help|
Continue reading below...
We have learned of five different kinds of things the computer can do under program control: (1) we can sequence commands to execute in order, (2) we can repeat some steps any number of times, (3) we can calculate values and save the results for later use, (4) we can make decisions based on those calculated values, to do or skip some steps in a program, and (5) we can name a piece of program and save it away, then do it later from inside another program. Now the final piece of the puzzle, (6) we can get input from outside the program, and send output from the program out to the real world.
The output we have been doing all along: the game board is something the Chomp engine (under your control) draws on your computer screen, and then a person like you (not a computer, but a computer could too) looks at the result. Our Chomp input is a little hokey, but your script has no way to know where the pills are. The Game engine obviously does, because it draws them on the screen, but that information is not available to your script except using the See and Smell commands.
You might imagine that the nibbler has eyes looking forward in whichever direction it is facing. Either it sees a pill (perhaps off in the distance), or it sees a wall blocking further travel in that direction, or maybe there's a hole in the wall and if it went that direction it would run off the edge of the game board (there's such a hole near the middle of each side on the original game board, like above unless you replaced it). Anyway, if the nibbler sees a pill, and you tell it to put its answer in a variable, then that variable gets the distance to that pill, how far the nibbler must move to eat that one pill, so you could write a script fragment like this: and it would eat just that one pill. Notice that you (the programmer) do not need to look at the screen to know where the nibbler is nor where the pills are, you can program the nibbler to find out for itself.
Similarly, if the nibbler sees wall, then the variable gets a negative number, but it's still the distance to the wall to bump into it. The negative of a negative is positive, so you can tell the nibbler to figure out which direction it can go without actually bumping into walls. This script is a little more complicated, but it has the nibbler look around to see which direction it can safely go. If it sees a pill, it advances to eat it. If it's up against a wall (or nothing), it turns and tries again. Otherwise a distant wall, it advances up to (but not bumping) the wall. Convince yourself that's what it does:
The Smell command helps you find pills the nibbler cannot see. If there are pills in front of the nibbler (so it can see them first) then Smell tells you how far the nibbler can go to eat all the pills in that one direction, without bumping into a wall.
If there are no pills visible, then smell gets a whiff of all the pills it might get to by stepping off in the direction it faces then wandering around looking for pills. The more pills there are, and the closer they are, the higher the resulting score. This is probably easiest to see with an example. Let's set up a test maze, a few pills here and there, some behind pieces of wall partly surrounding some of them, and the nibbler not exactly facing any of them: The five pills in a row, not quite in front of the nibbler cannot be seen, but they would add to the smell result, the first is two steps forward, then one down, so the score is 256 divided by the three steps, which is 85. The next pill would take four steps, but otherwise be the same, so its score value adds 64, for a running total of 149. The third pill adds 51 (integer division discards the fraction each time), and the fourth and fifth pills add 42 and 36, for a total of 278.
The five pills inside the box surrounding the backside of the nibbler can only be reached by the nibbler turning before moving, so they are not counted, but the two pills below that box (in row 6) can be smelled, because the nibbler could take two steps forward, three down, then two or three to the left, so they add 36 and 32 to the sum, now at 346.
The three pills directly in front of the nibbler (columns 11 to 13) are behind a wall, so they cannot be seen, but the nibbler could go around the wall in either direction to reach them. Going down then up is slightly shorter (14 steps to the rightmost pill) so these three pills add another 51 (18+17+16) to the sum, plus a +25 offset (so the sum will never look like the smell of visible pills) for a total result in variable N of 422.
Experiment with different numbers of pills and different placement until you get a good feeling for how the Smell command works, then try your hand a writing a script that can eat all the pills in a maze without bumping into any walls. If that sounds a little too daunting, you can look at (my) "TomsEater" for ideas. It's not particularly fast, so you might be able to improve on it. If you can run the maze fast without bumping into walls, and if you build up a score higher than 99, you will be considered an "expert" with extra skills. But experts get their score reduced for unnecessarily bumping into walls, so you must be good. Just to see if it could be done, I wrote a maze "HiScore" that (if you eat all the pills) will earn you a maximum score of 999. What happens if you go over 999? Try making your own maze that does that, and see.
Then, when you are ready to move on, we'll make the transition to Java...
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 8, 2020 March 12