Pacman Ghost AI

ComputerLocus picture ComputerLocus · Nov 15, 2011 · Viewed 16.3k times · Source

I'm currently making a pacman game in java. I have a question about the ghosts though.

I understand that the ghosts do not all have the same style of attack. I first want to work on the basics of getting the ghost to go after the pacman and not worry about there differences yet.

My question to you smart people out there is what would be the best way to make the ghosts chase the pacman but sometimes randomly divert paths. I'm currently using a 21 by 21 2D array for telling where walls are and such so I was thinking make it more try and head for the current grid location of pacman. (for example go to 10,14) Of course while avoiding going through walls like pacman. I'm wondering how I could make it do this and also have the ghosts sometimes stop and go another direction or something so that it's not always a constant chase and pacman has a chance to get away. Maybe some of you have programmed a pacman game or just know a good way for this. Any help would be greatly appreciated.

(Please note I'm currently in a Grade 11 Computer Science course and halfway through the first semester ever of learned java.)

Answer

Pascal Cuoq picture Pascal Cuoq · Nov 15, 2011

If you just want the ghosts not all to behave the same, each time they encounter an intersection, make their decision a random mix of some reasonable chasing default (such as continuing the way with the shortest distance to Pacman — Use Dijkstra's algorithm on all successors to pick the best one) and a random choice.