-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil2.py
219 lines (185 loc) · 7.75 KB
/
util2.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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 self_train(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
if type(player1) == type(player2):
# noinspection PyUnboundLocalVariable
player1.final_result(final_result, board)
# noinspection PyUnboundLocalVariable
player2.final_result(final_result, board)
return final_result
def play_train(board: Board, player1: Player, player2: Player): ## Added function
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,board)
player1.symetry(board)
# noinspection PyUnboundLocalVariable
player2.final_result(final_result, board)
player2.symetry(board)
return final_result
def play_eval(board: Board, player1: Player, player2: Player): ## Adeed function
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
return final_result
def battle(player1: Player, player2: Player, v,w,y,z, num_games_train: int = 100, num_games_eval: int = 100000, silent: bool = False): ## Added function
board = Board()
draw_count = 0
cross_count = 0
naught_count = 0
for _ in range(num_games_train):
if type(player1) == type(v):
play_train(board, player1, v)
if type(player2) == type(v):
play_train(board, v, player2)
if type(player1) == type(w):
play_train(board, player1, w)
if type(player2) == type(w):
play_train(board, w, player2)
if type(player1) == type(y):
play_train(board, player1, y)
if type(player2) == type(y):
play_train(board, y, player2)
if type(player1) == type(z):
play_train(board, player1, z)
if type(player2) == type(z):
play_train(board, z, player2)
for _ in range(num_games_eval):
result = play_eval(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_eval, 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_eval, cross_count / num_games_eval, naught_count / num_games_eval))
return cross_count, naught_count, draw_count
def battle_1(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.compat.v1.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