-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
373 lines (313 loc) · 13.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
import pygame as pg
import sys
import math
import numpy as np
import random
# initializa pygame modules
pg.init()
class Model:
PLAYER = 0
AI = 1
EMPTY = 0
PLAYER_PIECE = 1
AI_PIECE = 2
WINDOW_LENGTH = 4
AI_level = 1
def __init__(self, row_count, column_count):
self.COLUMN_COUNT = column_count
self.ROW_COUNT = row_count
self.board = np.zeros((self.ROW_COUNT,self.COLUMN_COUNT))
def print_board(self):
print(np.flip(self.board, 0))
def drop_piece(self, board, row, col, piece):
board[row][col] = piece
def is_valid_location(self, col):
return self.board[self.ROW_COUNT-1][col] == 0
def get_next_open_row(self, col):
for r in range(self.ROW_COUNT):
if self.board[r][col] == 0:
return r
def winning_move(self, piece):
# Check horizontal locations for win
for c in range(self.COLUMN_COUNT-3):
for r in range(self.ROW_COUNT):
if self.board[r][c] == piece and self.board[r][c+1] == piece \
and self.board[r][c+2] == piece and self.board[r][c+3] == piece:
return True
# Check vertical locations for win
for c in range(self.COLUMN_COUNT):
for r in range(self.ROW_COUNT-3):
if self.board[r][c] == piece and self.board[r+1][c] == piece \
and self.board[r+2][c] == piece and self.board[r+3][c] == piece:
return True
# Check positively sloped diaganols
for c in range(self.COLUMN_COUNT-3):
for r in range(self.ROW_COUNT-3):
if self.board[r][c] == piece and self.board[r+1][c+1] == piece \
and self.board[r+2][c+2] == piece and self.board[r+3][c+3] == piece:
return True
# Check negatively sloped diaganols
for c in range(self.COLUMN_COUNT-3):
for r in range(3, self.ROW_COUNT):
if self.board[r][c] == piece and self.board[r-1][c+1] == piece \
and self.board[r-2][c+2] == piece and self.board[r-3][c+3] == piece:
return True
def get_valid_locations(self):
valid_locations = []
for col in range(self.COLUMN_COUNT):
if self.is_valid_location(col):
valid_locations.append(col)
return valid_locations
def evaluate_window(self, window, piece):
score = 0
opp_piece = self.PLAYER_PIECE
if piece == self.PLAYER_PIECE:
opp_piece = self.AI_PIECE
if window.count(piece) == 4:
score += 100
elif window.count(piece) == 3 and window.count(self.EMPTY) == 1:
score += 5
elif window.count(piece) == 2 and window.count(self.EMPTY) == 2:
score += 2
if window.count(opp_piece) == 3 and window.count(self.EMPTY) == 1:
score -= 4
return score
def score_position(self, board_copy, piece):
score = 0
## Score center column
center_array = [int(i) for i in list(board_copy[:, self.COLUMN_COUNT//2])]
center_count = center_array.count(piece)
score += center_count * 3
## Score Horizontal
for r in range(self.ROW_COUNT):
row_array = [int(i) for i in list(board_copy[r,:])]
for c in range(self.COLUMN_COUNT-3):
window = row_array[c:c+self.WINDOW_LENGTH]
score += self.evaluate_window(window, piece)
## Score Vertical
for c in range(self.COLUMN_COUNT):
col_array = [int(i) for i in list(board_copy[:,c])]
for r in range(self.ROW_COUNT-3):
window = col_array[r:r+self.WINDOW_LENGTH]
score += self.evaluate_window(window, piece)
## Score posiive sloped diagonal
for r in range(self.ROW_COUNT-3):
for c in range(self.COLUMN_COUNT-3):
window = [board_copy[r+i][c+i] for i in range(self.WINDOW_LENGTH)]
score += self.evaluate_window(window, piece)
for r in range(self.ROW_COUNT-3):
for c in range(self.COLUMN_COUNT-3):
window = [board_copy[r+3-i][c+i] for i in range(self.WINDOW_LENGTH)]
score += self.evaluate_window(window, piece)
return score
def is_terminal_node(self):
return self.winning_move(self.PLAYER_PIECE) or self.winning_move(self.AI_PIECE) or len(self.get_valid_locations()) == 0
def minimax(self, board_copy, depth, alpha, beta, maximizingPlayer):
valid_locations = self.get_valid_locations()
is_terminal = self.is_terminal_node()
if depth == 0 or is_terminal:
if is_terminal:
if self.winning_move(self.AI_PIECE):
return (None, 100000000000000)
elif self.winning_move(self.PLAYER_PIECE):
return (None, -10000000000000)
else: # Game is over, no more valid moves
return (None, 0)
else: # Depth is zero
return (None, self.score_position(board_copy, self.AI_PIECE))
if maximizingPlayer:
value = -math.inf
column = random.choice(valid_locations)
for col in valid_locations:
row = self.get_next_open_row(col)
b_copy = board_copy.copy()
self.drop_piece(b_copy, row, col, self.AI_PIECE)
new_score = self.minimax(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_next_open_row(col)
b_copy = board_copy.copy()
self.drop_piece(b_copy, row, col, self.PLAYER_PIECE)
new_score = self.minimax(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 pick_best_move(self, piece):
"""
this function helps AI player to choose a random column
"""
valid_locations = self.get_valid_locations()
best_score = -10000
best_col = random.choice(valid_locations)
for col in valid_locations:
row = self.get_next_open_row(col)
temp_board = self.board.copy()
self.drop_piece(temp_board, row, col, piece)
score = self.score_position(temp_board, piece)
if score > best_score:
best_score = score
best_col = col
return best_col
def AI_rnd_or_minimax(self):
if self.AI_level == 1:
col, minimax_score = self.minimax(self.board, 5, -math.inf, math.inf, True)
print("minimax mode chosen")
else:
col = self.pick_best_move(self.AI_PIECE)
print("random_move mode chosen")
return col
def print_board(self):
print(np.flip(self.board, 0))
class View:
GAME_NAME = 'Connect 4 Game'
BLUE = "#142d4c"
BLACK = "#9fd3c7"
RED = "#a2a8d3"
YELLOW = "#f95959"
SQUARE_SIZE = 100
CIRCLE_RADIUS = int(SQUARE_SIZE/2 - 5)
myfont = pg.font.SysFont("monospace", 77)
def __init__(self, column_count, row_count, board, ai_piece, player_piece):
self.COLUMN_COUNT = column_count
self.ROW_COUNT = row_count
self.PLAYER_PIECE = player_piece
self.AI_PIECE = ai_piece
self.width = self.COLUMN_COUNT * self.SQUARE_SIZE
self.height = (self.ROW_COUNT+1) * self.SQUARE_SIZE
self.size = (self.width, self.height)
self.screen = pg.display.set_mode(self.size)
self.board = board
def initiate_window(self):
#drawing the screen background
for c in range(self.COLUMN_COUNT):
for r in range(self.ROW_COUNT):
pg.draw.rect(self.screen, self.BLUE, (c*self.SQUARE_SIZE, r*self.SQUARE_SIZE+ self.SQUARE_SIZE, self.SQUARE_SIZE, self.SQUARE_SIZE))
pg.draw.circle(self.screen, self.BLACK, (int(c*self.SQUARE_SIZE + self.SQUARE_SIZE/2), int(r*self.SQUARE_SIZE + self.SQUARE_SIZE + self.SQUARE_SIZE/2)), self.CIRCLE_RADIUS)
# naming the game
pg.display.set_caption(self.GAME_NAME)
pg.display.update()
def draw_board(self):
for c in range(self.COLUMN_COUNT):
for r in range(self.ROW_COUNT):
if self.board[r][c] == self.PLAYER_PIECE:
pg.draw.circle(self.screen, self.RED, (int(c*self.SQUARE_SIZE+self.SQUARE_SIZE/2), \
self.height-int(r*self.SQUARE_SIZE+self.SQUARE_SIZE/2)), self.CIRCLE_RADIUS)
elif self.board[r][c] == self.AI_PIECE:
pg.draw.circle(self.screen, self.YELLOW, (int(c*self.SQUARE_SIZE+self.SQUARE_SIZE/2), \
self.height-int(r*self.SQUARE_SIZE+self.SQUARE_SIZE/2)), self.CIRCLE_RADIUS)
pg.display.update()
def announce_winner(self, player):
player += 1
if player == 1:
label = self.myfont.render(f"Player {player} wins!!", 1, self.RED)
else:
label = self.myfont.render(f"Player {player} wins!!", 2, self.YELLOW)
print(f"player {player} is the winner")
return label
class Controller:
def __init__(self):
self.game_over = False
self.game_mode = 'pvp'
self.COLUMN_COUNT = 7
self.ROW_COUNT = 6
self.player = 0 # player 1 plays first is (the default)
self.model = Model(self.ROW_COUNT, self.COLUMN_COUNT)
self.view= View(self.COLUMN_COUNT, self.ROW_COUNT, \
self.model.board, self.model.AI_PIECE, self.model.PLAYER_PIECE)
def change_mode(self):
if self.game_mode == 'ai':
self.game_mode = 'pvp'
print("game mode is changed into pvp")
else:
self.game_mode = 'ai'
print("game mode is changed into ai")
def restart(self):
for i in range(self.ROW_COUNT):
for j in range(self.COLUMN_COUNT):
self.model.board[i][j] = 0
game.view.initiate_window()
print(self.model.board)
game.view.draw_board()
self.game_mode = 'pvp'
self.game_over = False
def ai_level_change(self):
if self.model.AI_level == 0:
self.model.AI_level = 1 # minimax
else:
self.model.AI_level = 0 # random
def key_handler(self, event):
try:
if event.key == pg.K_r:
self.restart()
elif event.key == pg.K_c:
self.change_mode()
elif event.key == pg.K_l:
self.ai_level_change()
except (KeyError, ValueError):
print('Wrong Key!')
def swap_player(self):
self.player += 1
self.player = self.player % 2
if __name__ == "__main__":
game = Controller()
game.view.initiate_window()
while not game.game_over:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.MOUSEMOTION:
pg.draw.rect(game.view.screen, game.view.BLACK, (0,0, game.view.width, game.view.SQUARE_SIZE))
posx = event.pos[0]
if game.player == 0:
pg.draw.circle(game.view.screen, game.view.RED, (posx, int(game.view.SQUARE_SIZE/2)), game.view.CIRCLE_RADIUS)
else:
pg.draw.circle(game.view.screen, game.view.YELLOW, (posx, int(game.view.SQUARE_SIZE/2)), game.view.CIRCLE_RADIUS)
pg.display.update()
if event.type == pg.MOUSEBUTTONDOWN:
pg.draw.rect(game.view.screen, game.view.BLACK, (0,0, game.view.width, game.view.SQUARE_SIZE))
posx = event.pos[0]
col = int(math.floor(posx/game.view.SQUARE_SIZE))
if game.model.is_valid_location(col):
row = game.model.get_next_open_row(col)
piece = game.player + 1
game.model.drop_piece(game.model.board, row, col, piece)
if game.model.winning_move(piece):
label = game.view.announce_winner(game.player)
game.view.screen.blit(label, (40,10))
game.game_over = True
game.model.print_board()
game.view.draw_board()
game.swap_player()
elif event.type == pg.KEYDOWN:
game.key_handler(event)
# # Ask for Player 2 Input
if game.player == game.model.AI and not game.game_over and game.game_mode == "ai":
col = game.model.AI_rnd_or_minimax()
if game.model.is_valid_location(col):
#pygame.time.wait(500)
row = game.model.get_next_open_row(col)
game.model.drop_piece(game.model.board, row, col, game.model.AI_PIECE)
if game.model.winning_move(game.model.AI_PIECE):
label = game.view.announce_winner(game.player)
game.view.screen.blit(label, (40,10))
game.game_over = True
game.model.print_board()
game.view.draw_board()
game.swap_player()
if event.type == pg.KEYDOWN:
game.key_handler(event)
pg.time.wait(3000)