Here's some thoughts on some Artificial Intelligence for our PacMan game.
Ghost AI
Ok we got four ghost's . How do we make some artificial intelligence? Ok we identify
them as numbers.
1 = blue
2 = red
3 = yellow
4 = pink
Now elsewhere we defined direction as compass points. We will use the same idea for the
ghosts.
N
E
S
W
As far as a ghost is concerned as it travels it needs to probe these places
Ahead
To the right
To the left
So if ghost is heading north the right is east and left is west. A ghost continues traveling
until it comes to an intersection. An intersection may be
A dead end
A one new direction
A two new direction
A three new direction
If we have only one choice we have to go that way.
If we reach a dead end then we have to reverse
If we reach a 2 way intersection then we roll a dice to decide new direction from the 2
choices.
If we reach a 3 way intersection then we roll a dice to decide new direction from the 3
choices.
Now this is totally random. Its possible that being random the ghosts will never be
able to "track , corall, work in coordination , hunt". To do any of these behaviours we could
make the ghost when possible move toward the shortest direction of our hero some of the time.
In our maze will be dots that get eaten. If we had 100 dots and divided them by 10 we reach a
result of 10. Alternatively if we calculate the number of eaten dots and divide it by 10 we'd
get at present 0. So these two figures are inversely related. We use one of these derived
figures to generate a "chance" that the ghost uses our hero location to base their choice of
direction. If we used the eaten dots then its possible to make the ghosts hunt more as dots get
eaten.
Lastly rather than generating each dice rolls that we need to use on the fly we could
create an array that holds a "weighted" amount of possible directions. In theory we have 3
directions to choose from at any moment. By arranging the numbers of direction as
1,2,3,1,2,3,1,2,3 and pre shuffle them then we simply read throught the array until we arrive
at a valid direction. As long as we note what subscript houses our valid number then we can
start at the next subscript the next time we need a new direction. Obviously we need to code
wrap around to arrive back at the start when we reach the end of the array. Also we can start
each ghost at different starting subscripts.
When our hero eats a power pill then the ghosts must flee. We could use the same
arrangement of tracking to calculate the greates distance AWAY from our hero.