-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI_project.py
377 lines (320 loc) · 11.6 KB
/
AI_project.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
####### Fatemeh Mirzaei Kalani#######
import random
import pygame
import numpy as np
pygame.init()
pygame.font.init()
WIDTH, HEIGHT = 560, 700
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Connect4")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
font = pygame.font.SysFont('Comic Sans MS', 35)
ROWS = 6
COLS = 7
FPS = 60
board = np.full((ROWS, COLS), -1)
player = 0 # 0 for Yellow(Human) and 1 for Blue(AI)
q_table = np.load('Q_table_random.npy')
# q_table = np.load('Q_table_minimax.npy') # uncomment this if you wanna test it
def draw_circles(board):
for row in range(ROWS):
for col in range(COLS):
if board[row][col] == -1:
color = WHITE
elif board[row][col] == 0:
color = YELLOW
else:
color = BLUE
pygame.draw.rect(WIN, RED, [col * 80, row * 80, 80, 80])
pygame.draw.circle(WIN, color, [int((col + 0.5) * 80), int((row + 0.5) * 80)], int(80 * 0.4), 0)
def get_empty_row(board, column):
for i in range(ROWS - 1, -1, -1):
if board[i][column] == -1:
return i
return -1
def check_win(board):
for i in range(ROWS):
for j in range(COLS - 3):
if board[i][j] != -1 and board[i][j] == board[i][j + 1] == board[i][j + 2] == board[i][j + 3]:
return board[i][j]
for i in range(ROWS - 3):
for j in range(COLS):
if board[i][j] != -1 and board[i][j] == board[i + 1][j] == board[i + 2][j] == board[i + 3][j]:
return board[i][j]
return -1
def check_draw(board):
for col in range(COLS):
if board[0][col] == -1:
return False
return True
def evaluate(board, player):
score = 0
for row in range(ROWS):
for col in range(COLS - 3):
circles = [board[row][col], board[row][col+1], board[row][col+2], board[row][col+3]]
score += count_score(circles, player, abs(player - 1))
for row in range(ROWS - 3):
for col in range(COLS):
circles = [board[row][col], board[row+1][col], board[row+2][col], board[row+3][col]]
score += count_score(circles, player, abs(player - 1))
for row in range(ROWS):
if board[row][3] == 1: # 3 is middle of 7
score += 6
elif board[row][3] == 0:
score -= 4
return score
def count_score(circles, player, opponent):
score = 0
if circles.count(player) == 4:
score += 1000
elif circles.count(opponent) == 4:
score -= 1000
elif circles.count(player) == 3 and circles.count(-1) == 1:
score += 30
elif circles.count(opponent) == 3 and circles.count(-1) == 1:
score -= 28
elif circles.count(player) == 2 and circles.count(-1) == 2:
score += 10
elif circles.count(opponent) == 2 and circles.count(-1) == 1:
score -= 8
return score
def get_valid_columns(board):
valid = []
for i in range(COLS):
if board[0][i] == -1:
valid.append(i)
return valid
def minimax(board, depth, max_turn):
winner = check_win(board)
if winner == 1:
return None, 10000
elif winner == 0:
return None,-10000
elif check_draw(board):
return None, 0
if depth == 4:
return None, evaluate(board, 1)
valid_columns = get_valid_columns(board)
if max_turn == 1: # AI is maximizing
best_value = -10000
column = random.choice(valid_columns)
for col in valid_columns:
row = get_empty_row(board, col)
b_temp = np.copy(board)
b_temp[row][col] = 1
new_value = minimax(b_temp, depth+1, 0)[1]
if new_value > best_value:
best_value = new_value
column = col
return column, best_value
else:
best_value = 10000
column = random.choice(valid_columns)
for col in valid_columns:
row = get_empty_row(board, col)
b_temp = np.copy(board)
b_temp[row][col] = 0
new_value = minimax(b_temp, depth+1, 1)[1]
if new_value < best_value:
best_value = new_value
column = col
return column, best_value
def play_minimax(board):
col = minimax(board,0, 1)[0]
row = get_empty_row(board, col)
board[row][col] = 1
def alphabeta(board, alpha, beta, depth, max_turn):
winner = check_win(board)
if winner == 1:
return None, 10000
elif winner == 0:
return None,-10000
elif check_draw(board):
return None, 0
if depth == 6:
return None, evaluate(board, 1)
valid_columns = get_valid_columns(board)
if max_turn == 1: # AI is maximizing
value = -10000
column = random.choice(valid_columns)
for col in valid_columns:
row = get_empty_row(board, col)
b_temp = np.copy(board)
b_temp[row][col] = 1
new_score = alphabeta(b_temp, alpha, beta, depth+1, 0)[1]
if new_score > value:
value = new_score
column = col
alpha = max(alpha, value)
if alpha >= beta:
break
return column, value
else:
value = 10000
column = random.choice(valid_columns)
for col in valid_columns:
row = get_empty_row(board, col)
b_temp = np.copy(board)
b_temp[row][col] = 0
new_score = alphabeta(b_temp, alpha, beta, depth+1, 1)[1]
if new_score < value:
value = new_score
column = col
beta = min(beta, value)
if alpha >= beta:
break
return column, value
def play_alphabeta(board):
col = alphabeta(board,-10_000, 10_000, 0 , 1)[0]
row = get_empty_row(board, col)
board[row][col] = 1
def choose_action(board, current_cell):
row = current_cell[0]
col = current_cell[1]
max_action = float('-inf')
valid = get_valid_columns(board)
actions = q_table[row][col][:]
action_index = float('-inf')
for index, action in enumerate(actions):
if action > max_action and index in valid:
max_action = action
action_index = index
return action_index
def play_rl(board, current_cell):
col = choose_action(board, current_cell)
row = get_empty_row(board, col)
board[row][col] = 1
return [row, col]
def main_page(board, player):
logo = pygame.image.load("logo1.png")
button_image1 = pygame.image.load("alpha.png")
button_image2 = pygame.image.load("minimax.png")
button_image3 = pygame.image.load("rl.png")
button_rect = logo.get_rect()
button_rect1 = button_image1.get_rect()
button_rect2 = button_image2.get_rect()
button_rect3 = button_image3.get_rect()
button_rect.centerx = WIN.get_width() // 2
button_rect.bottom = WIN.get_height() - 500
button_rect1.centerx = WIN.get_width() // 2
button_rect1.bottom = WIN.get_height() - 400
button_rect2.centerx = WIN.get_width() // 2
button_rect2.bottom = WIN.get_height() - 300
button_rect3.centerx = WIN.get_width() // 2
button_rect3.bottom = WIN.get_height() - 200
WIN.fill(WHITE)
WIN.blit(logo, button_rect)
WIN.blit(button_image1, button_rect1)
WIN.blit(button_image2, button_rect2)
WIN.blit(button_image3, button_rect3)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.MOUSEBUTTONDOWN and button_rect1.collidepoint(event.pos):
main(board, player, "AlphaBeta")
elif event.type == pygame.MOUSEBUTTONDOWN and button_rect2.collidepoint(event.pos):
main(board, player, "MiniMax")
elif event.type == pygame.MOUSEBUTTONDOWN and button_rect3.collidepoint(event.pos):
main(board, player, "RL")
def main(board, player, game_type):
current_cell = [5, random.choice(list(range(7)))]
if game_type == "RL":
board[current_cell[0]][current_cell[1]] = 1
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if reset_rect.collidepoint(event.pos):
board = np.full((ROWS, COLS), -1)
if game_type == "RL":
player = 1
else:
player = 0
break
if player == 0: # 0 for human
column = int(pygame.mouse.get_pos()[0] / 80)
row = get_empty_row(board, column)
if row != -1:
board[row][column] = 0
player = 1
WIN.fill(WHITE)
draw_circles(board)
## reset Button
reset = pygame.image.load("reset.png")
reset_rect = reset.get_rect()
reset_rect.centerx = WIN.get_width() // 2
reset_rect.bottom = WIN.get_height()
WIN.blit(reset, reset_rect)
##
winner = check_win(board)
if winner == 0:
run = False
text = font.render(f"You won!", True, BLACK)
text_rect = text.get_rect()
text_rect.centerx = WIN.get_width() // 2
text_rect.bottom = WIN.get_height() - 80
WIN.blit(text, text_rect)
pygame.display.update()
pygame.time.delay(3000)
continue
elif check_draw(board):
run = False
text = font.render(f"Draw!", True, BLACK)
text_rect = text.get_rect()
text_rect.centerx = WIN.get_width() // 2
text_rect.bottom = WIN.get_height() - 80
WIN.blit(text, text_rect)
pygame.display.update()
pygame.time.delay(3000)
continue
pygame.display.update()
if player == 1:
if game_type == "AlphaBeta":
play_alphabeta(board)
elif game_type == "MiniMax":
play_minimax(board)
elif game_type == "RL":
res = play_rl(board, current_cell)
current_cell = res
player = 0
WIN.fill(WHITE)
draw_circles(board)
## reset button
reset = pygame.image.load("reset.png")
reset_rect = reset.get_rect()
reset_rect.centerx = WIN.get_width() // 2
reset_rect.bottom = WIN.get_height()
WIN.blit(reset, reset_rect)
##
winner = check_win(board)
if winner == 1:
run = False
text = font.render(f"AI won!", True, BLACK)
text_rect = text.get_rect()
text_rect.centerx = WIN.get_width() // 2
text_rect.bottom = WIN.get_height() - 80
WIN.blit(text, text_rect)
pygame.display.update()
pygame.time.delay(3000)
elif check_draw(board):
run = False
text = font.render(f"Draw!", True, BLACK)
text_rect = text.get_rect()
text_rect.centerx = WIN.get_width() // 2
text_rect.bottom = WIN.get_height() - 80
WIN.blit(text, text_rect)
pygame.display.update()
pygame.time.delay(3000)
pygame.display.update()
pygame.quit()
main_page(board, player)