Game Development Reference
In-Depth Information
16 - AI Simulation
277. else:
278. turn = otherTile
279.
280. # Display the final score.
281. scores = getScoreOfBoard(mainBoard)
282. print('X scored %s points. O scored %s points.' %
(scores['X'], scores['O']))
283.
284. if scores['X'] > scores['O']:
285. xwins += 1
286. elif scores['X'] < scores['O']:
287. owins += 1
288. else:
289. ties += 1
290.
291. numGames = float(numGames)
292. xpercent = round(((xwins / numGames) * 100), 2)
293. opercent = round(((owins / numGames) * 100), 2)
294. tiepercent = round(((ties / numGames) * 100), 2)
295. print('X wins %s games (%s%%), O wins %s games (%s%%),
ties for %s games (%s%%) of %s games total.' % (xwins,
xpercent, owins, opercent, ties, tiepercent, numGames))
How the AISim2.py Code Works
We have added the variables xwins , owins , and ties to keep track of how many times
X wins, O wins, and when they tie. Lines 284 to 289 increment these variables at the end of
each game, before it loops back to start a brand new game.
We have removed most of the print() function calls from the program, and the calls to
drawBoard() . When you run AISim2.py , it asks you how many games you wish to run.
Now that we've taken out the call to drawBoard() and replace the while True: loop
with a for game in range(numGames): loop, we can run a number of games
without stopping for the user to type anything. Here is a sample run where we run ten games
of computer vs. computer Reversi:
Welcome to Reversi!
Enter number of games to run: 10
Game #0: X scored 40 points. O scored 23 points.
Game #1: X scored 24 points. O scored 39 points.
Game #2: X scored 31 points. O scored 30 points.
Game #3: X scored 41 points. O scored 23 points.
Game #4: X scored 30 points. O scored 34 points.
Game #5: X scored 37 points. O scored 27 points.
Game #6: X scored 29 points. O scored 33 points.
Game #7: X scored 31 points. O scored 33 points.
Game #8: X scored 32 points. O scored 32 points.
Game #9: X scored 41 points. O scored 22 points.
Search WWH ::




Custom Search