-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
282 lines (272 loc) · 14.8 KB
/
board.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from tkinter import *
import numpy as np
import PIL.Image
import PIL.ImageTk
import math
from othello import Othello
from agent import Agent
from constants import WHITE, BLACK, VALID_MOVE, WHITE_IMG, BLACK_IMG, NEXT_MOVE_IMG, BLACK_TURN_TEXT, WHITE_TURN_TEXT, \
BLACK_WON_TEXT, WHITE_WON_TEXT, DRAW_TEXT, BLACK_LOADING_TEXT, WHITE_LOADING_TEXT, GAME_IN_PROGRESS, BLACK_WON, \
WHITE_WON, DRAW, LOG_FILE, LAST_MOVE
class Board(Frame):
def __init__(self,
parent,
n,
size,
color,
black_player_type,
white_player_type,
black_hints,
white_hints,
black_depth,
white_depth,
black_evaluation_fn,
white_evaluation_fn,
black_move_ordering,
white_move_ordering):
open(LOG_FILE, "w")
# Initialize agents
self.black = Agent(BLACK,
black_player_type,
black_hints,
black_depth,
black_evaluation_fn,
black_move_ordering)
self.white = Agent(WHITE,
white_player_type,
white_hints,
white_depth,
white_evaluation_fn,
white_move_ordering)
if self.black.agent_type == "computer":
with open(LOG_FILE, "a") as f:
f.write("Black is initialized with the following parameters:\n"
"Depth: {}\nEvaluation Function Type: {}\nMove Ordering: {}".format(
self.black.depth, self.black.evaluation_fn, self.black.move_ordering
))
f.write("\n\n_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_\n\n")
if self.white.agent_type == "computer":
with open(LOG_FILE, "a") as f:
f.write("White is initialized with the following parameters:\n"
"Depth: {}\nEvaluation Function Type: {}\nMove Ordering: {}".format(
self.white.depth, self.white.evaluation_fn, self.white.move_ordering
))
f.write("\n\n_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_\n\n")
# Initialize game object
self.game = Othello(n)
# Pass turn to black as black always starts first
self.current_player = self.black
# Initialize board parameters
n = 2 ** math.ceil(math.log2(n))
self.n = n
self.size = size
self.color = color
# Initialize images
self.image_size = math.floor(size * 0.75)
image = PIL.Image.open(WHITE_IMG)
image = image.resize((self.image_size, self.image_size))
self.white_img = PIL.ImageTk.PhotoImage(image)
image = PIL.Image.open(BLACK_IMG)
image = image.resize((self.image_size, self.image_size))
self.black_img = PIL.ImageTk.PhotoImage(image)
image = PIL.Image.open(NEXT_MOVE_IMG)
image = image.resize((self.image_size, self.image_size))
self.next_move_img = PIL.ImageTk.PhotoImage(image)
# Initialize widgets (board, scoreboard)
Frame.__init__(self, parent, bg="gray")
self.black_score_var = IntVar(value=self.game.black_score)
self.white_score_var = IntVar(value=self.game.white_score)
if self.current_player.agent_type == "computer":
self.game_info_var = StringVar(value=BLACK_LOADING_TEXT)
else:
self.game_info_var = StringVar(value=BLACK_TURN_TEXT)
self.canvas = Canvas(self, borderwidth=0, highlightthickness=0, width=n * size, height=n * size, bg="gray")
self.score_board = Canvas(self, width=n * size, height=60, bg="gray", highlightthickness=0)
self.black_score_widget = Label(self.score_board, compound=LEFT, image=self.black_img,
text=self.game.black_score, bg="gray", padx=25,
textvariable=self.black_score_var, font='System 30 bold')
self.white_score_widget = Label(self.score_board, compound=RIGHT, image=self.white_img,
text=self.game.white_score, bg="gray", padx=25,
textvariable=self.white_score_var, font='System 30 bold')
self.info_widget = Label(self.score_board, compound=RIGHT, text=BLACK_TURN_TEXT, bg="gray", font='System 15',
textvariable=self.game_info_var)
self.black_score_widget.image = self.black_img
self.white_score_widget.image = self.white_img
self.moves_btns = []
# Render widgets
self.canvas.pack(side="top", fill="both", expand=True, padx=4, pady=4)
self.score_board.pack(side="bottom", fill="both", expand=True, padx=4, pady=4)
self.black_score_widget.pack(side="left")
self.info_widget.pack(side="left", expand=True)
self.white_score_widget.pack(side="right")
self.canvas.bind("<Destroy>", self.quit)
self.window_destroyed = False
self.initialize_board()
if self.current_player.agent_type == "computer":
self.canvas.after(1000, self.run_player_move)
else:
self.run_player_move()
def set_game_info_text(self, event=GAME_IN_PROGRESS):
if event == GAME_IN_PROGRESS:
if self.current_player.identifier == WHITE and self.current_player.agent_type == "computer":
self.game_info_var.set(WHITE_LOADING_TEXT)
if self.current_player.identifier == BLACK and self.current_player.agent_type == "computer":
self.game_info_var.set(BLACK_LOADING_TEXT)
if self.current_player.identifier == WHITE and self.current_player.agent_type == "human":
self.game_info_var.set(WHITE_TURN_TEXT)
if self.current_player.identifier == BLACK and self.current_player.agent_type == "human":
self.game_info_var.set(BLACK_TURN_TEXT)
elif event == BLACK_WON:
self.game_info_var.set(BLACK_WON_TEXT)
with open(LOG_FILE, "a") as f:
f.write("\n_*_*_*_*_*_*_*_*_*_*_*_* BLACK WON *_*_*_*_*_*_*_*_*_*_*_*_\n\n")
elif event == WHITE_WON:
self.game_info_var.set(WHITE_WON_TEXT)
with open(LOG_FILE, "a") as f:
f.write("\n_*_*_*_*_*_*_*_*_*_*_*_* WHITE WON *_*_*_*_*_*_*_*_*_*_*_*_\n\n")
elif event == DRAW:
self.game_info_var.set(DRAW_TEXT)
with open(LOG_FILE, "a") as f:
f.write("\n_*_*_*_*_*_*_*_*_*_*_*_* DRAW *_*_*_*_*_*_*_*_*_*_*_*_\n\n")
if event == BLACK_WON or event == WHITE_WON or event == DRAW:
if self.black.agent_type == "computer":
with open(LOG_FILE, "a") as f:
f.write("BLACK average branching factor = {}\n"
.format(self.black.total_branching_factor / self.black.turns))
f.write("BLACK average effective branching factor = {}\n"
.format(self.black.total_effective_branching_factor / self.black.turns))
f.write("BLACK average execution time = {}\n"
.format(self.black.total_execution_time / self.black.turns))
f.write("\n_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_\n\n")
f.write("WHITE average branching factor = {}\n"
.format(self.white.total_branching_factor / self.white.turns))
f.write("WHITE average effective branching factor = {}\n"
.format(self.white.total_effective_branching_factor / self.white.turns))
f.write("WHITE average execution time = {}\n"
.format(self.white.total_execution_time / self.white.turns))
def run_player_move(self, move=None):
pass_turn_to_computer = False
if self.current_player.agent_type == "human":
if move is not None:
self.game.apply_move(self.current_player.identifier, move)
self.current_player = self.black if self.current_player.identifier == WHITE else self.white
event = self.game.status()
if event == GAME_IN_PROGRESS:
if self.current_player.agent_type == "human":
moves = self.game.move_generator(self.current_player.identifier)
if len(moves) == 0: # If a player doesn't have a move, pass the play to the other player
self.current_player = self.black if self.current_player.identifier == WHITE else self.white
moves = self.game.move_generator(self.current_player.identifier)
if len(moves) == 0:
self.current_player = self.black if self.current_player.identifier == WHITE else self.white
event = self.game.status()
elif self.current_player.agent_type == "computer":
pass_turn_to_computer = True
self.black_score_var.set(self.game.black_score)
self.white_score_var.set(self.game.white_score)
self.set_game_info_text(event)
self.refresh()
if pass_turn_to_computer and event == GAME_IN_PROGRESS:
self.canvas.after(0, self.run_player_move)
elif self.current_player.agent_type == "computer":
player_move = self.current_player.get_move(self.game, self.current_player.identifier)
if player_move is not None:
self.game.apply_move(self.current_player.identifier, player_move)
self.current_player = self.black if self.current_player.identifier == WHITE else self.white
event = self.game.status()
if event == GAME_IN_PROGRESS:
if self.current_player.agent_type == "human":
moves = self.game.move_generator(self.current_player.identifier)
if len(moves) == 0: # If a player doesn't have a move, pass the play to the other player
self.current_player = self.black if self.current_player.identifier == WHITE else self.white
pass_turn_to_computer = True
elif self.current_player.agent_type == "computer":
pass_turn_to_computer = True
self.black_score_var.set(self.game.black_score)
self.white_score_var.set(self.game.white_score)
self.set_game_info_text(event)
self.refresh()
if pass_turn_to_computer and event == GAME_IN_PROGRESS:
self.canvas.after(0, self.run_player_move)
def add_piece(self, kind, row, column, hints=False):
x0 = (column * self.size) + int(self.size / 2)
y0 = (row * self.size) + int(self.size / 2)
if kind == WHITE:
self.canvas.create_image(x0, y0, image=self.white_img, tags="piece", anchor=CENTER)
elif kind == BLACK:
self.canvas.create_image(x0, y0, image=self.black_img, tags="piece", anchor=CENTER)
elif kind == VALID_MOVE:
move_btn = Button(self, bg=self.color, activebackground=self.color, relief=FLAT, overrelief=FLAT,
command=lambda: self.run_player_move([row, column]), anchor=CENTER)
if hints:
move_btn.configure(image=self.next_move_img)
self.moves_btns.append(move_btn)
self.canvas.create_window(x0, y0, anchor=CENTER, window=move_btn, height=self.size - 1, width=self.size - 1,
tags="move")
elif kind == LAST_MOVE:
self.canvas.create_oval(x0-5, y0-5, x0+5, y0+5, fill="red", tags="last_move", )
def update_images(self):
self.image_size = math.floor(self.size * 0.75)
image = PIL.Image.open(WHITE_IMG)
image = image.resize((self.image_size, self.image_size))
self.white_img = PIL.ImageTk.PhotoImage(image)
image = PIL.Image.open(BLACK_IMG)
image = image.resize((self.image_size, self.image_size))
self.black_img = PIL.ImageTk.PhotoImage(image)
image = PIL.Image.open(NEXT_MOVE_IMG)
image = image.resize((self.image_size, self.image_size))
self.next_move_img = PIL.ImageTk.PhotoImage(image)
def refresh(self):
if self.window_destroyed:
return
self.canvas.delete("last_move")
self.canvas.delete("piece")
self.canvas.delete("move")
for btn in self.moves_btns:
btn.destroy()
del btn
white_pieces_indices = np.argwhere(self.game.state == WHITE)
black_pieces_indices = np.argwhere(self.game.state == BLACK)
next_move_indices = np.argwhere(self.game.state == VALID_MOVE)
last_move_index = None
if self.game.last_move is not None:
last_move_index = self.game.last_move
for index in white_pieces_indices:
self.add_piece(WHITE, index[0], index[1])
for index in black_pieces_indices:
self.add_piece(BLACK, index[0], index[1])
if self.current_player.agent_type == "human":
for index in next_move_indices:
self.add_piece(VALID_MOVE, index[0], index[1], self.current_player.hints)
if last_move_index is not None:
self.add_piece(LAST_MOVE, last_move_index.x, last_move_index.y)
self.canvas.tag_raise("move")
self.canvas.tag_raise("piece")
self.canvas.tag_raise("last_move")
self.canvas.tag_lower("square")
self.canvas.update()
def initialize_board(self):
for row in range(self.n):
for col in range(self.n):
x1 = (col * self.size)
y1 = (row * self.size)
x2 = x1 + self.size
y2 = y1 + self.size
self.canvas.create_rectangle(x1, y1, x2, y2, outline="black", fill=self.color, tags="square")
white_pieces_indices = np.argwhere(self.game.state == WHITE)
black_pieces_indices = np.argwhere(self.game.state == BLACK)
next_move_indices = np.argwhere(self.game.state == VALID_MOVE)
for index in white_pieces_indices:
self.add_piece(WHITE, index[0], index[1])
for index in black_pieces_indices:
self.add_piece(BLACK, index[0], index[1])
if self.current_player.agent_type == "human":
for index in next_move_indices:
self.add_piece(VALID_MOVE, index[0], index[1], self.current_player.hints)
self.canvas.tag_raise("move")
self.canvas.tag_raise("piece")
self.canvas.tag_lower("square")
self.canvas.update()
def quit(self, event=None):
self.window_destroyed = True
self.destroy()