The ghosts use the A-Star algorithm to calculate the path to follow. Each cell is calculated to see if the ghost can go to the next cell: Up, Down, Left, and Right. The location the ghost wants to get to is marked with the number 1, then it checks to see if it can move up. It will then add a 2. Next, it checks right, down, and finally left. Each time, add a number if it can move in that direction.
Once the possible movement is mapped, the current cell value is used to calculate the way back to the target. If the ghost is on 42, then it checks if there is a 41 nearby. Then 40, and so on, until it reaches one.
Each time the ghost changes to another cell or the target moves. The entire routine has to be updated.
