Source code for Pacman
Lots of progress on this one. This time it took a lot of effort.
First I updated the maze generation code to prevent dead-ends. This makes better mazes, but it also broke the mirror effect in some special cases. I haven't fixed that yet, but I left the mirror option in, so if you grab the source you can turn it on.
Next I wanted to improve the AI for the ghost. The day 6 ghost gets stuck easily in any U-shaped portion of the maze. This ghost is smarter. In order to accomplish this I built another structure that is generated from the maze data. This is the graph. It consists of nodes that are connected to each other by edges. A node is an intersection (not simply a turn) in the maze. The code to generate the graph is very recursive, but was interesting to write.
The ghost looks at all the paths that it can travel that are two nodes deep and chooses the on that will bring it closest (as the crow flies) to the player. It only makes decisions when it reaches a node. Then it will follow the choosen path until it reaches another node. This means the ghost doesn't always make the "best" decision and in some situations it can end up moving back and forth, but in general it is much better than the old ai. It will be improved in the next version. I could easily have the ghost make the perfect decision, but I will probably just add some code to discourage it from going back to the last node to prevent the back and forth situation.
As part of debugging the ghost AI, I made it paint its previous location as a yellow square and then paint the current location on top of that. That way I could stop the program prior to the AI routines and still see the location of the ghost. It is a nice effect so I left it in.
I added simple collision detection, so now if the ghost gets you, you die. Well, sometimes you die. If you head straight for the ghost you can run right past it on occasion. This will be fixed later. I was going to have collision detection wait for later, since it is easy to do, uninteresting, and can make testing other things harder, but everyone who sees the demo asks why the ghost doesn't kill you, so it is in there now.
I am always asked where the tunnel is, but I am not adding that yet. Mainly because I have used the structure of the maze to prevent having to do bounds checking and adding tunnels would throw that off. It will happen eventually, but not just yet.
Finally, I made the game rotate between three difference sizes of mazes and speeds of pacman and the ghost. Playing the different sizes of mazes helps me to think of creative (non-traditional) power-ups that could make the game more fun. Current ideas are little baby pacmans that run off helping you clear the board, the ability to clear pellets to either side of pacman, and a bomb that would clear pellets.
Just a reminder, I have started to use Eclipse (well, ok WSAD, same thing) as my development environment instead of VisualAge for Java. Both Eclipse and VAJ are excellent tools. Note that the applet no longer runs if your Java is old. You can easily update it by downloading a new JRE at http://java.sun.com/.
Next: Day 8.