forked from stlucasgarcia/connect-four
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
424 lines (313 loc) · 12.6 KB
/
utilities.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import pygame as pg
import numpy as np
import random
import math
from ending_screens import EndingScreen
from datetime import timedelta
from settings import Settings
class UtilitiesMain:
def __init__(self, screen: pg.Surface):
self.config = Settings()
self.screen = screen
self.COLUMN_AMOUNT = 7
self.ROW_AMOUNT = 6
self.WINDOWS_LENGHT = 4
self.background_image = self.config.bg_image
self.chip_1 = self.config.chip_1
self.chip_2 = self.config.chip_2
self.width = self.config.width
self.font = self.config.font
self.zero = 0
self.player_chip = 1
self.ai_chip = 2
def create_matrix(self) -> np.ndarray:
matrix = np.zeros((self.ROW_AMOUNT, self.COLUMN_AMOUNT))
return matrix
def drop_piece(self, matrix: np.ndarray, row: int, column: int, piece: int) -> None:
matrix[row][column] = piece
def is_available(self, matrix: np.ndarray, column: int) -> bool:
return matrix[self.ROW_AMOUNT - 1][column] == 0
def location_X(self, click_loc: int) -> int:
# First location (X)
if self.width == 1280:
if 240 < click_loc < 364:
return 1
elif 364 < click_loc < 473:
return 2
elif 473 < click_loc < 582:
return 3
elif 582 < click_loc < 688:
return 4
elif 688 < click_loc < 798:
return 5
elif 798 < click_loc < 907:
return 6
elif 907 < click_loc < 1040:
return 7
else:
return 0
else:
if 357 < click_loc < 551:
return 1
elif 551 < click_loc < 714:
return 2
elif 714 < click_loc < 879:
return 3
elif 879 < click_loc < 1038:
return 4
elif 1038 < click_loc < 1203:
return 5
elif 1203 < click_loc < 1362:
return 6
elif 1362 < click_loc < 1423:
return 7
else:
return 0
def draw_board(self, matrix: np.ndarray, start_time, usernames) -> None:
self.screen.blit(self.background_image, (0, 0))
self.timer(start_time)
self.print_names(usernames)
for column in range(self.COLUMN_AMOUNT):
for row in range(self.ROW_AMOUNT):
if matrix[row][column] != 0:
if self.width == 1280:
X_CONST = 21
Y_CONST = 10
x = 269 + ((X_CONST * column) + (87.75 * (column)))
y = 616 - (Y_CONST * (row) + (87.5 * (row)))
else:
X_CONST = 140
Y_CONST = 128.5
x = 402.25 + ((X_CONST * column) + (22.78 * (column)))
y = 923.75 - (Y_CONST * (row) + (17.43 * (row)))
if matrix[row][column] == 1:
self.screen.blit(self.chip_1, (x, y))
elif matrix[row][column] == 2:
self.screen.blit(self.chip_2, (x, y))
def get_open_row(self, matrix: np.ndarray, column: int) -> int:
for row in range(self.ROW_AMOUNT):
if matrix[row][column] == 0:
return row
def get_available_list(self, matrix):
available_slots = []
for column in range(self.COLUMN_AMOUNT):
if self.is_available(matrix, column):
available_slots.append(column)
return available_slots
def is_victory(self, matrix: np.ndarray, chip: int) -> bool:
# Check horizontal
for column in range(self.COLUMN_AMOUNT - 3):
for row in range(self.ROW_AMOUNT):
if (
matrix[row][column] == chip
and matrix[row][column + 1] == chip
and matrix[row][column + 2] == chip
and matrix[row][column + 3] == chip
):
return True
# Check Vertical
for column in range(self.COLUMN_AMOUNT):
for row in range(self.ROW_AMOUNT - 3):
if (
matrix[row][column] == chip
and matrix[row + 1][column] == chip
and matrix[row + 2][column] == chip
and matrix[row + 3][column] == chip
):
return True
# Main diagonal
for column in range(self.COLUMN_AMOUNT - 3):
for row in range(self.ROW_AMOUNT - 3):
if (
matrix[row][column] == chip
and matrix[row + 1][column + 1] == chip
and matrix[row + 2][column + 2] == chip
and matrix[row + 3][column + 3] == chip
):
return True
# MainC diagonal
for column in range(self.COLUMN_AMOUNT - 3):
for row in range(3, self.ROW_AMOUNT):
if (
matrix[row][column] == chip
and matrix[row - 1][column + 1] == chip
and matrix[row - 2][column + 2] == chip
and matrix[row - 3][column + 3] == chip
):
return True
def is_tie(self, matrix: np.ndarray):
return (matrix[:][:] != 0).all()
def is_valid(
self,
column: int,
is_position_available: bool,
turn: int,
sound_chip_1,
sound_chip_2,
option,
):
chip = None
if column != 0 and is_position_available:
turn += 1
if turn % 2 == 0:
chip = self.chip_2 if option == "Player vs AI" else self.chip_1
turn = 2
sound_chip_1.play()
return turn, chip
elif turn % 2 == 1:
chip = self.chip_1 if option == "Player vs AI" else self.chip_2
turn = 1
sound_chip_2.play()
return turn, chip
def is_terminal(self, matrix):
return (
self.is_victory(matrix, self.player_chip)
or self.is_victory(matrix, self.ai_chip)
or len(self.get_available_list(matrix)) == 0 # TODO self.is_tie()
)
def timer(self, start_time):
hour, minutes, seconds = str(
timedelta(milliseconds=pg.time.get_ticks() - start_time)
).split(":")
seconds = seconds.split(".")[0]
time = f"{minutes} : {seconds}"
y = 41 if self.width != 1280 else 27
text_time = self.font.render(time, True, (255, 255, 255))
self.screen.blit(text_time, (self.width // 2 - 80, y))
def playersTurn(
self,
column: int,
matrix,
player_turn,
sound_chip_1,
sound_chip_2,
usernames,
start_time,
scores,
option,
):
turn = player_turn
play_again = None
chip = None
column -= 1
is_pos_available = self.is_available(matrix, column)
if is_pos_available:
turn, chip = self.is_valid(
column + 1, is_pos_available, turn, sound_chip_1, sound_chip_2, option
)
row = self.get_open_row(matrix, column)
self.drop_piece(matrix, row, column, turn)
if self.is_victory(matrix, turn) or self.is_tie(matrix):
scores[0] += 1 if turn % 2 == 0 else scores[0]
scores[1] += 1 if turn % 2 == 1 else scores[1]
self.draw_board(matrix, start_time, usernames)
pg.display.update()
pg.time.wait(1500)
data = {usernames[0]: scores[0], usernames[1]: scores[1]}
ending = EndingScreen(
self.screen,
data,
res=self.config.size,
pg_res=self.config.win_pg,
sm_res=self.config.win_sm,
quit_res=self.config.win_quit,
lb_res=self.config.win_ld,
)
play_again = ending.scores()
return play_again, turn, chip, scores
def evaluate_window(self, window, chip):
score = 0
opp_chip = self.player_chip
if chip == self.player_chip:
opp_chip = self.ai_chip
if window.count(chip) == 4:
score += 100
elif window.count(chip) == 3 and window.count(self.zero) == 1:
score += 5
elif window.count(chip) == 2 and window.count(self.zero) == 2:
score += 2
if window.count(opp_chip) == 3 and window.count(self.zero) == 1:
score -= 4
return score
def score_position(self, matrix, chip):
score = 0
## Score center column
center_array = [int(i) for i in list(matrix[:, self.COLUMN_AMOUNT // 2])]
center_count = center_array.count(chip)
score += center_count * 3
## Score Horizontal
for r in range(self.ROW_AMOUNT):
row_array = [int(i) for i in list(matrix[r, :])]
for c in range(self.COLUMN_AMOUNT - 3):
window = row_array[c : c + self.WINDOWS_LENGHT]
score += self.evaluate_window(window, chip)
## Score Vertical
for c in range(self.COLUMN_AMOUNT):
col_array = [int(i) for i in list(matrix[:, c])]
for r in range(self.ROW_AMOUNT - 3):
window = col_array[r : r + self.WINDOWS_LENGHT]
score += self.evaluate_window(window, chip)
## Score positive sloped diagonal
for r in range(self.ROW_AMOUNT - 3):
for c in range(self.COLUMN_AMOUNT - 3):
window = [matrix[r + i][c + i] for i in range(self.WINDOWS_LENGHT)]
score += self.evaluate_window(window, chip)
for r in range(self.ROW_AMOUNT - 3):
for c in range(self.COLUMN_AMOUNT - 3):
window = [matrix[r + 3 - i][c + i] for i in range(self.WINDOWS_LENGHT)]
score += self.evaluate_window(window, chip)
return score
def minimaxTree(self, matrix, depth, alpha, beta, maximizingPlayer):
valid_locations = self.get_available_list(matrix)
is_terminal = self.is_terminal(matrix)
if depth == 0 or is_terminal:
if is_terminal:
if self.is_victory(matrix, self.ai_chip):
return (None, 100000000000000)
elif self.is_victory(matrix, self.player_chip):
return (None, -10000000000000)
else: # Game is over, no more valid moves
return (None, 0)
else: # Depth is zero
return (None, self.score_position(matrix, self.ai_chip))
if maximizingPlayer:
value = -math.inf
column = random.choice(valid_locations)
for col in valid_locations:
row = self.get_open_row(matrix, col)
b_copy = matrix.copy()
self.drop_piece(b_copy, row, col, self.ai_chip)
new_score = self.minimaxTree(b_copy, depth - 1, alpha, beta, False)[1]
if new_score > value:
value = new_score
column = col
alpha = max(alpha, value)
if alpha >= beta:
break
return column, value
else: # Minimizing player
value = math.inf
column = random.choice(valid_locations)
for col in valid_locations:
row = self.get_open_row(matrix, col)
b_copy = matrix.copy()
self.drop_piece(b_copy, row, col, self.player_chip)
new_score = self.minimaxTree(b_copy, depth - 1, alpha, beta, True)[1]
if new_score < value:
value = new_score
column = col
beta = min(beta, value)
if alpha >= beta:
break
return column, value
def print_names(self, usernames):
name_1 = usernames[0]
name_2 = "AI" if len(usernames) == 1 else usernames[1]
name_1 = name_1[:10] if len(name_1) > 10 else name_1
name_2 = name_2[:10] if len(name_2) > 10 else name_2
x_2 = 1610 if self.width != 1280 else 1000
y = 41 if self.width != 1280 else 30
name_text_1 = self.font.render(name_1, True, (255, 255, 255))
name_text_2 = self.font.render(name_2, True, (255, 255, 255))
self.screen.blit(name_text_1, (43, y))
self.screen.blit(name_text_2, (x_2, y))