forked from fcarsten/tic-tac-toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
92 lines (77 loc) · 3.24 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from IPython.display import HTML, display
from tic_tac_toe.Player import Player
from tic_tac_toe.Board import Board, GameResult, CROSS, NAUGHT
import tensorflow as tf
def print_board(board):
display(HTML("""
<style>
.rendered_html table, .rendered_html th, .rendered_html tr, .rendered_html td {
border: 1px black solid !important;
color: black !important;
}
</style>
""" + board.html_str()))
def play_game(board: Board, player1: Player, player2: Player):
player1.new_game(CROSS)
player2.new_game(NAUGHT)
board.reset()
finished = False
while not finished:
result, finished = player1.move(board)
if finished:
if result == GameResult.DRAW:
final_result = GameResult.DRAW
else:
final_result = GameResult.CROSS_WIN
else:
result, finished = player2.move(board)
if finished:
if result == GameResult.DRAW:
final_result = GameResult.DRAW
else:
final_result = GameResult.NAUGHT_WIN
# noinspection PyUnboundLocalVariable
player1.final_result(final_result)
# noinspection PyUnboundLocalVariable
player2.final_result(final_result)
return final_result
def battle(player1: Player, player2: Player, num_games: int = 100000, silent: bool = False):
board = Board()
draw_count = 0
cross_count = 0
naught_count = 0
for _ in range(num_games):
result = play_game(board, player1, player2)
if result == GameResult.CROSS_WIN:
cross_count += 1
elif result == GameResult.NAUGHT_WIN:
naught_count += 1
else:
draw_count += 1
if not silent:
print("After {} game we have draws: {}, Player 1 wins: {}, and Player 2 wins: {}.".format(num_games, draw_count,
cross_count,
naught_count))
print("Which gives percentages of draws: {:.2%}, Player 1 wins: {:.2%}, and Player 2 wins: {:.2%}".format(
draw_count / num_games, cross_count / num_games, naught_count / num_games))
return cross_count, naught_count, draw_count
def evaluate_players(p1: Player, p2: Player, games_per_battle=100, num_battles=100,
writer: tf.summary.FileWriter = None, silent: bool = False):
p1_wins = []
p2_wins = []
draws = []
game_number = []
game_counter = 0
for i in range(num_battles):
p1win, p2win, draw = battle(p1, p2, games_per_battle, silent)
p1_wins.append(p1win)
p2_wins.append(p2win)
draws.append(draw)
game_counter = game_counter + 1
game_number.append(game_counter)
if writer is not None:
summary = tf.Summary(value=[tf.Summary.Value(tag='Player 1 Win', simple_value=p1win),
tf.Summary.Value(tag='Player 2 Win', simple_value=p2win),
tf.Summary.Value(tag='Draw', simple_value=draw)])
writer.add_summary(summary, game_counter)
return game_number, p1_wins, p2_wins, draws