Game Development Reference
In-Depth Information
value and the same color. By creating a collection of all of these sets of dice (moves), we would
know every move the computer could make on any one turn.
The next problem, knowing the value of all the moves the computer can make, is related to the
previous one. By having a collection of all the possible moves, we would also know the values of
those moves. We could then sort those moves to find out which move was worth the most (max),
second worst, third worst, and so on.
The last two problems, know the computer's ability to choose the best (max) move to avoid a terrible
(min) move, were both addressed with the
DifficultyLevel
properties
aiBonus
and
minValue
.
With all of the questions tentatively answered, we moved onto create some code that could make
all this a reality.
Discussing the Dice Battle AI
To illustrate how the computer AI chooses a move, let's take a look at the sample screen in
Figure 9-6. From the information provided on the screen, you can tell that it is the computer's
turn, and the player has reached level 3.
Figure 9-6.
When it's the computer's turn, it calculates the AI move.
At this point,
createAIMove
is called, and the code will perform the following functions.
First, we find all the possible moves that the computer can make. We do this first by looping
through all the
Die
objects on the board with two nested
for
loops. We take each
Die
on the
board and call
findLikeColoredDice(tempDie)
to find the set of
Die
objects that match the
Die
tested. We consider these sets moves, because if we pretend like the computer clicked the
tempDie
, the set of
Die
objects returned would constitute a move the computer would make.
These moves only include each
Die
once. We make sure of this by using this test before we drop
into the code that calls
findLikecoloredDice
:
if (diceTested.indexOf(tempDie) == -1) {