Friday, February 03, 2006

Pacman

Some weeks back, I delivered a Java training for the company I work for. From the start, I wanted the training to end with each participant having actually written a complete Java application of some reasonable complexity.

We decided on writing a version of the famous Pacman game. Actually, as the tutor I had more of a say on this decision and I felt at that time that Pacman was neither too simple nor too complex a program to write. In retrospect, this was not the best of my decisions. Pacman turned out to more complex to implement (if you want to do it properly) than I initially thought.

In the end, nobody succeeded in the assignment but everybody learnt a couple of things (top of the list must be that programming is harder that it looks). It took me 7 days to write my version of Pacman which I've uploaded here:

Binary: http://www.vikash-madhow.com/files/pacman-1.0.jar
Source: http://www.vikash-madhow.com/files/sources-1.0.zip

If Java (version 5.0) is installed properly on your computer, you only need to double-click on the binary file to run it. Finding your way after this is pretty simple.

The more interesting part, however, is the source code. It has been written to demonstrate all the good Java programming practices that I was trying to deliver to the audience - with compromises where the purist's approach would have taken too long. Unlike many Pacman implementations I've seen on the web, this once actually consists of more than one class. It has about 30, in fact. All of them are reasonably small and have well-defined purposes. Someone can very easily create new characters, mazes, etc. simply by extending the proper class.

Another interesting feature in this implementation of Pacman is that the mazes are built randomly every time you start a new game, so that you never come across the same maze twice. It also has an implementation of the A* Search algorithm used by the Ghosts to calculate shortest distance in the maze to Pacman when attacking and shortest distance to their lair when fleeing.

Enjoy.

PS: I would love to hear suggestions but due to time constraints I may not be able to change the program. But please feel free to use and modify the code to suit your purposes. All I ask is that you acknowledge your source.

0 Comments:

Post a Comment

<< Home