Game Development Reference
In-Depth Information
Value:20 Row:3 Col:2
Value:24 Row:4 Col:4
Finally, we remove any moves that are below the
minValue
set for the level (but only if it leaves one
move to make at the end). For this level (level 3), the
currentLevel.minValue
is
7
. This means that
the computer will always choose a move that scores
7
or more (if one exists. With the values under
currentLevel.minValue
removed, we are left with four moves for the computer to choose from:
if (dieSets[dieSets.length-1][0] > currentLevel.minValue) {
for (ii = dieSets.length-1; ii >= 0; ii--) {
if (dieSets[ii][0] < currentLevel.minValue) {
dieSets.splice(ii,1);
}
}
}
Here is the traced output of
dieSets
array after we have removed all the values below
minValue
(with labels added to each value for illustration):
minValue removed Moves:4
Value:8 Row:2 Col:3
Value:10 Row:3 Col:3
Value:20 Row:3 Col:2
Value:24 Row:4 Col:4
Now we get a random value from 0-99. In this case, it is 28. We test to see
if (dieSets > 1)
,
because we don't need to figure out which move to make if there is only one.
var aiVal:Number = 0;
if (dieSets.length > 1) {
var pChance:Number = Math.floor(Math.random() * 100);
Here is the traced output of
pChance
(with a label added for illustration):
pChance without aiBonus:28
We now add the
aiBonus
for this level to that value. The higher the number, the better move the
computer will make. The
aiBonus
for level 3 is
20
so we add that to
pChance
to make
48
.
pChance += currentLevel.aiBonus;
Here is the traced output
pChance with aiBonus added
(with a label added for illustration):
pChance with AIbonus:48
Now, we simply divide 99 by the number of moves we have:
Math.floor(99/dieSets.length)
, or
99 / 4, which makes 24. We put that in to
pVal
to get the multiplier for each move. We then divide
pChance
by
pVal
to get the move the computer will make and put that value into
AIval
. In this
case, it would be 2. However, since our array is zero-based, the move we have chosen for the
computer would be at index 1.
var pVal:Number = Math.floor(99/dieSets.length);
aiVal = Math.floor(pChance/pVal);
if (aiVal > dieSets.length-1) {
aiVal = dieSets.length-1;
}
} else {
aiVal = 0;
}