There is an opportunity: for all the steps that a ghost can take, calculate whether this step will bring Pacman closer. This can be done using the Manhattan distance , which in the 2nd grid is equal to the distance x + y . Then arbitrarily choose a step with a higher probability assigned to those steps that will actually bring it closer.
If you have an array of steps with the first steps n_closing_in representing the steps that will bring the ghost closer to Pacman, you can assign them the overall probability prob_closing_in with
double total_probility = 1.; for (int i=0; i<n_closing_in; i++) { step_prob[i] = prob_closing_in / n_closing_in; total_probability -= prob_closing_in / n_closing_in; }
Then similarly distribute what remains of total_probability into steps that distract the ghost further from Pacman.
Step random_step(Step[] possible_steps, double[] step_prob) { double r = Math.random(); int i; for (i=0; i<possible_steps.length(); i++) { if (r < step_prob[i]) break; r -= step_prob[i]; } return possible_steps[i]; }
If the maze is not too complicated, and the probability of closure in it is >.5 , ghosts will haunt after Pacman, but randomly.
Raising prob_closing_in to 1. will make the game more difficult.
(All of the above code is untested and may contain errors. I'm not very good at Java.)
Fred foo
source share