Game Help (pop)   (link) Game Help

Tell the Computer to Build a Maze

Continue reading below...

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

 
New User  
 
   

The maze that comes with the commercial PacMan game is intentionally hard to get around in. They have other things to make it even harder, like those ghosts that float around and make life difficult. I guess that makes it interesting when all you can do is play the game. I think it's a lot more interesting to design your own game. I did that with the spiral command, because it made it easier to see the repeat command at work. Even better, you get to build your own maze. Or rather, you tell the computer how to build it.

We have three new commands for that: "wall" tells Chomp where to put one wall segment, "pill" tells Chomp where to put a row of pills, and "start" tells Chomp where the nibbler should be placed when the game starts. Each takes three parameters, separated by commas. I think Chomp will take whole expressions for the three parameters, but for now we'll just use variables and do the extra thinking about the numbers using "let" commands.

The first "wall" or "pill" command you type into your scripts lets Chomp know that you are building a new maze. When you have finished, you give it a "start" command and then it actually builds your maze so you can see it. I did that to make the first spiral, but then I just added it to the game engine as a command. Later on you can do that too, but it's a lot more work. So we'll do things the easy way first.

Did you notice the game board on this page? There are tiny little numbers along the top and left edges of the board. It's the same game, but I added this little help for you to see the grid coordinates, from 1 to 23 vertical columns and from 1 to 23 horizontal rows. Each grid position can be either wall or pill or nothing. It's best to do the walls as a continuous run so they get connected as a smooth wall. Anyway, I put those numbers there so you can easily count off the starting positions of each wall segment.

Our first game board will be simple, a blue wall border all the way around the game board, then a cross of pills through the middle. We'll do this one step at a time, and see what happens. After we have something we think will work, we can place the nibbler, and Chomp will build the game for us.

Two commands:

wall 1,1,22
start 12,12,0
The first line tells Chomp to make one wall segment starting in the top left corner (row 1, column 1) then go 22 (more) cells down. The second line places the nibbler near the middle and tells Chomp to build it. Try it. Oops, we didn't specify any pills at all, so it used the default. Add a pill line down the middle (column 13) top to bottom (notice that I made it a little shorter than the wall, because pretty soon we want a wall there at the bottom: I find if I try to do too much at once, I make mistakes and I don't know what caused the problem. If you add only a little at a time, then if something goes wrong, you know it was in the most recent addition, and it's easier to find the problem. You will make mistakes (we all do), there's no shame in that, but we can fix our mistakes. The nice thing about computers is they don't keep track of your mistakes: you fix the mistakes and then all there is to see is the perfect result. People should do that too, but some people are jerks. They don't have what it takes to be programmers. Don't do that to people. We all make mistakes. We fix them. Mistakes are the dirt that excellence grows in. Let me try a horizontal wall and pill row (the negative length makes it horizontal -- oh wait, I told you you don't need negative numbers? No problem, think of it as a "horizontal" dash in front of the length number) in this next cut: Do you think you can add the other two walls, to fully enclose the box? Try it. How about filling the whole box with pills? I bet you can do that. Did you make a repeat loop to do it? I put my solution on the server (log in as "Box") but don't peek until you have tried to do it yourself. What's the fewest number of program lines you can make a box and then eat all the pills?

OK, this one is a little tricky: What is the maximum score you can get by building the maze and running around it eating all the pills? Then, what is the fewest number of lines to get that score? The tricky part is, I don't know the answer! I do know that if the score goes over 999, the game dies same as if you run the nibbler off the edge of the board, so 999 is the maximum possible score, but how to get there in the fewest lines of Chomp script? You can probably beat anything I did. Spend some time on it, it's a good exercise. Programming is partly about solving puzzles like this one.

Building mazes can get tricky, and maybe we'd rather not muddle up our maze-running program building it. So the next thing we will learn is how to separate out the different parts of a larger program.

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 6, 2020 March 12