-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
398 lines (349 loc) · 12.9 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Author: aqeelanwar
# Created: 13 March,2020, 9:19 PM
# Email: [email protected]
# Modified by GaIB 19 Assistants
from tkinter import *
import numpy as np
from Bot import Bot
from typing import Optional
from GameState import GameState
from RandomBot import RandomBot
from AdversarialSearchBot import AdversarialSearchBot
from LocalSearchBot import LocalSearchBot
from time import time
# == Tkinter Config
size_of_board = 600
number_of_dots = 4
symbol_size = (size_of_board / 3 - size_of_board / 8) / 2
symbol_thickness = 50
dot_color = "#7BC043"
player1_color = "#0492CF"
player1_color_light = "#67B0CF"
player2_color = "#EE4035"
player2_color_light = "#EE7E77"
Green_color = "#7BC043"
dot_width = 0.25 * size_of_board / number_of_dots
edge_width = 0.1 * size_of_board / number_of_dots
distance_between_dots = size_of_board / (number_of_dots)
BOT_TURN_INTERVAL_MS = 100
LEFT_CLICK = "<Button-1>"
class Dots_and_Boxes:
# ------------------------------------------------------------------
# Initialization functions
# ------------------------------------------------------------------
def __init__(self, bot1: Optional[Bot] = None, bot2: Optional[Bot] = None):
self.window = Tk()
self.window.title("Dots_and_Boxes")
self.canvas = Canvas(
self.window, width=size_of_board, height=size_of_board)
self.canvas.pack()
self.player1_starts = True
self.refresh_board()
self.bot1 = bot1
self.bot2 = bot2
self.play_again()
def play_again(self):
self.refresh_board()
self.board_status = np.zeros(
shape=(number_of_dots - 1, number_of_dots - 1))
self.row_status = np.zeros(shape=(number_of_dots, number_of_dots - 1))
self.col_status = np.zeros(shape=(number_of_dots - 1, number_of_dots))
self.pointsScored = False
# Input from user in form of clicks
self.player1_starts = not self.player1_starts
self.player1_turn = not self.player1_starts
self.reset_board = False
self.turntext_handle = []
self.already_marked_boxes = []
self.display_turn_text()
self.turn()
def mainloop(self):
self.window.mainloop()
# ------------------------------------------------------------------
# Logical Functions:
# The modules required to carry out game logic
# ------------------------------------------------------------------
def is_grid_occupied(self, logical_position, type):
x = logical_position[0]
y = logical_position[1]
occupied = True
if type == "row" and self.row_status[y][x] == 0:
occupied = False
if type == "col" and self.col_status[y][x] == 0:
occupied = False
return occupied
def convert_grid_to_logical_position(self, grid_position):
grid_position = np.array(grid_position)
position = (grid_position - distance_between_dots / 4) // (
distance_between_dots / 2
)
type = False
logical_position = []
if position[1] % 2 == 0 and (position[0] - 1) % 2 == 0:
x = int((position[0] - 1) // 2)
y = int(position[1] // 2)
logical_position = [x, y]
type = "row"
# self.row_status[c][r]=1
elif position[0] % 2 == 0 and (position[1] - 1) % 2 == 0:
y = int((position[1] - 1) // 2)
x = int(position[0] // 2)
logical_position = [x, y]
type = "col"
return logical_position, type
def pointScored(self):
self.pointsScored = True
def mark_box(self):
boxes = np.argwhere(self.board_status == -4)
for box in boxes:
if list(box) not in self.already_marked_boxes and list(box) != []:
self.already_marked_boxes.append(list(box))
color = player1_color_light
self.shade_box(box, color)
boxes = np.argwhere(self.board_status == 4)
for box in boxes:
if list(box) not in self.already_marked_boxes and list(box) != []:
self.already_marked_boxes.append(list(box))
color = player2_color_light
self.shade_box(box, color)
def update_board(self, type, logical_position):
x = logical_position[0]
y = logical_position[1]
val = 1
playerModifier = 1
if self.player1_turn:
playerModifier = -1
if y < (number_of_dots - 1) and x < (number_of_dots - 1):
self.board_status[y][x] = (
abs(self.board_status[y][x]) + val
) * playerModifier
if abs(self.board_status[y][x]) == 4:
self.pointScored()
if type == "row":
self.row_status[y][x] = 1
if y >= 1:
self.board_status[y - 1][x] = (
abs(self.board_status[y - 1][x]) + val
) * playerModifier
if abs(self.board_status[y - 1][x]) == 4:
self.pointScored()
elif type == "col":
self.col_status[y][x] = 1
if x >= 1:
self.board_status[y][x - 1] = (
abs(self.board_status[y][x - 1]) + val
) * playerModifier
if abs(self.board_status[y][x - 1]) == 4:
self.pointScored()
def is_gameover(self):
return (self.row_status == 1).all() and (self.col_status == 1).all()
# ------------------------------------------------------------------
# Drawing Functions:
# The modules required to draw required game based object on canvas
# ------------------------------------------------------------------
def make_edge(self, type, logical_position):
if type == "row":
start_x = (
distance_between_dots / 2 +
logical_position[0] * distance_between_dots
)
end_x = start_x + distance_between_dots
start_y = (
distance_between_dots / 2 +
logical_position[1] * distance_between_dots
)
end_y = start_y
elif type == "col":
start_y = (
distance_between_dots / 2 +
logical_position[1] * distance_between_dots
)
end_y = start_y + distance_between_dots
start_x = (
distance_between_dots / 2 +
logical_position[0] * distance_between_dots
)
end_x = start_x
if self.player1_turn:
color = player1_color
else:
color = player2_color
self.canvas.create_line(
start_x, start_y, end_x, end_y, fill=color, width=edge_width
)
def display_gameover(self):
player1_score = len(np.argwhere(self.board_status == -4))
player2_score = len(np.argwhere(self.board_status == 4))
if player1_score > player2_score:
# Player 1 wins
text = "Winner: Player 1 "
color = player1_color
elif player2_score > player1_score:
text = "Winner: Player 2 "
color = player2_color
else:
text = "Its a tie"
color = "gray"
self.canvas.delete("all")
self.canvas.create_text(
size_of_board / 2,
size_of_board / 3,
font="cmr 60 bold",
fill=color,
text=text,
)
score_text = "Scores \n"
self.canvas.create_text(
size_of_board / 2,
5 * size_of_board / 8,
font="cmr 40 bold",
fill=Green_color,
text=score_text,
)
score_text = "Player 1 : " + str(player1_score) + "\n"
score_text += "Player 2 : " + str(player2_score) + "\n"
# score_text += 'Tie : ' + str(self.tie_score)
self.canvas.create_text(
size_of_board / 2,
3 * size_of_board / 4,
font="cmr 30 bold",
fill=Green_color,
text=score_text,
)
self.reset_board = True
score_text = "Click to play again \n"
self.canvas.create_text(
size_of_board / 2,
15 * size_of_board / 16,
font="cmr 20 bold",
fill="gray",
text=score_text,
)
def refresh_board(self):
for i in range(number_of_dots):
x = i * distance_between_dots + distance_between_dots / 2
self.canvas.create_line(
x,
distance_between_dots / 2,
x,
size_of_board - distance_between_dots / 2,
fill="gray",
dash=(2, 2),
)
self.canvas.create_line(
distance_between_dots / 2,
x,
size_of_board - distance_between_dots / 2,
x,
fill="gray",
dash=(2, 2),
)
for i in range(number_of_dots):
for j in range(number_of_dots):
start_x = i * distance_between_dots + distance_between_dots / 2
end_x = j * distance_between_dots + distance_between_dots / 2
self.canvas.create_oval(
start_x - dot_width / 2,
end_x - dot_width / 2,
start_x + dot_width / 2,
end_x + dot_width / 2,
fill=dot_color,
outline=dot_color,
)
def display_turn_text(self):
text = "Next turn: "
if self.player1_turn:
text += "Player1"
color = player1_color
else:
text += "Player2"
color = player2_color
self.canvas.delete(self.turntext_handle)
self.turntext_handle = self.canvas.create_text(
size_of_board - 5 * len(text),
size_of_board - distance_between_dots / 8,
font="cmr 15 bold",
text=text,
fill=color,
)
def shade_box(self, box, color):
start_x = (
distance_between_dots / 2 + box[1] *
distance_between_dots + edge_width / 2
)
start_y = (
distance_between_dots / 2 + box[0] *
distance_between_dots + edge_width / 2
)
end_x = start_x + distance_between_dots - edge_width
end_y = start_y + distance_between_dots - edge_width
self.canvas.create_rectangle(
start_x, start_y, end_x, end_y, fill=color, outline=""
)
def display_turn_text(self):
text = "Next turn: "
if self.player1_turn:
text += "Player1"
color = player1_color
else:
text += "Player2"
color = player2_color
self.canvas.delete(self.turntext_handle)
self.turntext_handle = self.canvas.create_text(
size_of_board - 5 * len(text),
size_of_board - distance_between_dots / 8,
font="cmr 15 bold",
text=text,
fill=color,
)
def click(self, event):
if not self.reset_board:
grid_position = [event.x, event.y]
logical_position, valid_input = self.convert_grid_to_logical_position(
grid_position
)
self.update(valid_input, logical_position)
else:
self.canvas.delete("all")
self.play_again()
self.reset_board = False
def update(self, valid_input, logical_position):
if valid_input and not self.is_grid_occupied(logical_position, valid_input):
self.window.unbind(LEFT_CLICK)
self.update_board(valid_input, logical_position)
self.make_edge(valid_input, logical_position)
self.mark_box()
self.refresh_board()
self.player1_turn = (
not self.player1_turn if not self.pointsScored else self.player1_turn
)
self.pointsScored = False
if self.is_gameover():
# self.canvas.delete("all")
self.display_gameover()
self.window.bind(LEFT_CLICK, self.click)
else:
self.display_turn_text()
self.turn()
def turn(self):
current_bot = self.bot1 if self.player1_turn else self.bot2
if current_bot is None:
self.window.bind(LEFT_CLICK, self.click)
else:
self.window.after(BOT_TURN_INTERVAL_MS, self.bot_turn, current_bot)
def bot_turn(self, bot: Bot):
action = bot.get_action(
GameState(
self.board_status.copy(),
self.row_status.copy(),
self.col_status.copy(),
self.player1_turn,
)
)
self.update(action.action_type, action.position)
if __name__ == "__main__":
game_instance = Dots_and_Boxes(
None,
LocalSearchBot()
)
game_instance.mainloop()