Game Development Reference
In-Depth Information
16 - AI Simulation
How the AISim3.py Code Works
A lot of these functions are very similar to one another, and some of them use the new
isOnSide() function. Here's a review of the new algorithms we've made:
Table 17-1: Functions used for our Reversi AI.
Function
Description
getRandomMove()
Randomly choose a valid move to make.
Take a corner move if available. If there is no corner,
take a space on the side. If no sides are available, use
the regular getComputerMove() algorithm.
getCornerSideBestMove
()
Take a side space if there is one available. If not, then
use the regular getComputerMove() algorithm (side
spaces are chosen before corner spaces).
getSideBestMove()
Take the space that will result in the fewest tiles being
flipped.
getWorstMove()
Take a corner space, if available. If not, use the
getWorstMove() algorithm.
getCornerWorstMove()
Comparing the Random Algorithm Against the Regular Algorithm
Now the only thing to do is replace one of the getComputerMove() calls in the main
part of the program with one of the new functions. Then we can run several games and see
how often one algorithm wins over the other. First, let's replace O's algorithm with the one
in getComputerMove() with getRandomMove() on line 351:
351. x, y = getRandomMove(mainBoard, 'O')
When we run the program with a hundred games now, it may look something like this:
Welcome to Reversi!
Enter number of games to run: 100
Game #0: X scored 25 points. O scored 38 points.
Game #1: X scored 32 points. O scored 32 points.
Game #2: X scored 15 points. O scored 0 points.
Game #3: X scored 50 points. O scored 14 points.
...skipped for brevity...
Game #96: X scored 31 points. O scored 33 points.
Game #97: X scored 41 points. O scored 23 points.
Search WWH ::




Custom Search