02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

Figure 16.4 Developing the Reversi game<br />

Evaluating the computer’s best move<br />

The evaluation function for the computer’s best move is the most complex part of the game <strong>to</strong> code<br />

efficiently. The aim of the function is <strong>to</strong> derive the computer’s best move based on a look-ahead<br />

of two moves. Because we do not want <strong>to</strong> affect the display we first copy the on-screen board in<strong>to</strong><br />

a one-dimensional array called ‘board’. This array starts in the <strong>to</strong>p left corner and scans the board<br />

one row at a time; index 8 is therefore row 1 column 0.<br />

After s<strong>to</strong>ring the board position in a format that is easily readable by the computer we then<br />

process every square of the board looking for a legal move. The function used is ‘computerScore’;<br />

this function returns −1 if the position is an illegal move and a score for the computer if the square<br />

is a legal move. We will look a little later at how this score is derived. If the move is legal then<br />

we use the position following this move that is s<strong>to</strong>red in the single dimension array ‘cBoard’ in a<br />

further function ‘playerScore’; the purpose of this function is <strong>to</strong> calculate the player’s best score<br />

from the board position s<strong>to</strong>red in the array ‘cBoard’. Then we subtract the player’s best score from<br />

the computer’s best score and work out which move will give the computer the best advantage.<br />

Finally, having discovered the best move on the basis of this simple algorithm, we calculate the<br />

row and column that the move implies. Because the arrays are single dimensional <strong>to</strong> map <strong>to</strong> row<br />

and column we use the code snippet:<br />

252<br />

row = int(best/8);<br />

col = best % 8;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!