Game Development Reference
In-Depth Information
16 - AI Simulation
Depending on how fast your computer is, this run might have taken a about a couple
minutes. We can see that the results of all one hundred games still evens out to about fifty-
fifty, because both X and O are using the same algorithm to win.
Comparing Different AI Algorithms
Let's add some new functions with new algorithms. But first click on File , then Save As ,
and save this file as AISim3.py . Before the print('Welcome to Reversi!') line,
add these functions:
AISim3.py
This code can be downloaded from http://inventwithpython.com/AISim3.py
If you get errors after typing this code in, compare it to the topic's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
245. def getRandomMove(board, tile):
246. # Return a random move.
247. return random.choice( getValidMoves(board, tile) )
248.
249.
250. def isOnSide(x, y):
251. return x == 0 or x == 7 or y == 0 or y ==7
252.
253.
254. def getCornerSideBestMove(board, tile):
255. # Return a corner move, or a side move, or the best
move.
256. possibleMoves = getValidMoves(board, tile)
257.
258. # randomize the order of the possible moves
259. random.shuffle(possibleMoves)
260.
261. # always go for a corner if available.
262. for x, y in possibleMoves:
263. if isOnCorner(x, y):
264. return [x, y]
265.
266. # if there is no corner, return a side move.
267. for x, y in possibleMoves:
268. if isOnSide(x, y):
269. return [x, y]
270.
271. return getComputerMove(board, tile)
272.
273.
274. def getSideBestMove(board, tile):
275. # Return a corner move, or a side move, or the best
move.
276. possibleMoves = getValidMoves(board, tile)
277.
278. # randomize the order of the possible moves
Search WWH ::




Custom Search