Game Development Reference
In-Depth Information
The
Array.indexOf
function will find the
Die
in the
diceTested
array if it exists. If it does, we will not
make the test. For example, if a three is touching another three, only the first three will be added to
the set of moves, because the clicking the second three would create the exact same move. By
doing this, we significantly cut down the number of moves we need to calculate and handle.
public function createAIMove():void {
var dieSets:Array = new Array();
var diceTested:Array = new Array();
var testSet:Array = new Array();
var testValue:Number = 0;
for (var r:int = 0; r < board.length; r++) {
for (var c:int = 0; c < board[r].length; c++) {
tempDie = board[r][c];
if (diceTested.indexOf(tempDie) == -1) {
testSet = findLikeColoredDice(tempDie);
testValue = getDiceValue(testSet);
dieSets.push([Number(testValue),tempDie]);
while(testSet.length > 0) {
diceTested.push(testSet.pop())
}
}
}
}
The dieSets array is created as multidimensional array. The first dimension is the value of the
move (the value of the dice). The second dimension is the Die object that will give us this set if we
decide it will be our move.
If we printed out the values and
Row,Col
of the first die in each set, we would get the following list
(with labels added to each value for illustration):
Moves:18
Value:5 Row:0 Col:0
Value:20 Row:3 Col:2
Value:6 Row:0 Col:2
Value:4 Row:0 Col:3
Value:6 Row:0 Col:4
Value:6 Row:1 Col:4
Value:6 Row:1 Col:0
Value:8 Row:2 Col:3
Value:1 Row:1 Col:3
Value:24 Row:4 Col:4
Value:4 Row:2 Col:0
Value:1 Row:2 Col:1
Value:10 Row:3 Col:3
Value:3 Row:3 Col:0
Value:24 Row:4 Col:3
Value:6 Row:4 Col:5
Value:2 Row:4 Col:0
Value:5 Row:4 Col:1
Now, we sort all the moves from lowest to highest value. The higher the value, the better the
move. Sorting the values in the array will make it much easier for us to decide which move the
computer should make. The
sortOn
function will work on multidimensional arrays if the numeric