Game Development Reference
In-Depth Information
Displaying the Statistics
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))
The code at the bottom of our program will show the user how many wins X and O had,
how many ties there were, and how what percentages these make up. Statistically, the more
games you run, the more accurate your percentages will be. If you only ran ten games, and
X won three of them, then it would seem that X's algorithm only wins 30% of the time.
However, if you run a hundred, or even a thousand games, then you may find that X's
algorithm wins closer to 50% (that is, half) of the games.
To find the percentages, we divide the number of wins or ties by the total number of
games. We convert numGames to a float to ensure we do not use integer division in our
calculation. Then we multiply the result by 100 . However, we may end up with a number
like 66.66666666666667 . So we pass this number to the round() function with the
second parameter of 2 to limit the precision to two decimal places, so it will return a float
like 66.67 instead (which is much more readable).
Let's try another experiment. Run AISim2.py again, but this time have it run a hundred
games:
Sample Run of AISim2.py
Welcome to Reversi!
Enter number of games to run: 100
Game #0: X scored 42 points. O scored 18 points.
Game #1: X scored 26 points. O scored 37 points.
Game #2: X scored 34 points. O scored 29 points.
Game #3: X scored 40 points. O scored 24 points.
...skipped for brevity...
Game #96: X scored 22 points. O scored 39 points.
Game #97: X scored 38 points. O scored 26 points.
Game #98: X scored 35 points. O scored 28 points.
Game #99: X scored 24 points. O scored 40 points.
X wins 46 games (46.0%), O wins 52 games (52.0%),
ties for 2 games (2.0%) of 100.0 games total.
Search WWH ::




Custom Search