-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectn-debug3.py
382 lines (313 loc) · 9.07 KB
/
connectn-debug3.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
import numpy as np
import random
import pygame
import sys
import math
BLUE = (0,0,255)
BLACK = (0,0,0)
RED = (255,0,0)
YELLOW = (255,255,0)
PLAYER = 0
AI = 1
EMPTY = 0
PLAYER_PIECE = 1
AI_PIECE = 2
N = 4
ROW_COUNT = 6
COLUMN_COUNT = 7
WINDOW_LENGTH = N
board = np.zeros((ROW_COUNT,COLUMN_COUNT))
# highest empty row at that column
board_col = [ROW_COUNT-1]*COLUMN_COUNT
def drop_piece(board, board_col, row, col, piece):
board[row][col] = piece
board_col[col] -= 1
def is_valid_location(board, col):
return board[0][col] == 0
def get_next_open_row(board, col):
return board_col[col]
def print_board(board):
print(board)
def checkDiagonally1(board, board_col, playerNumber):
returnMap = {i: 0 for i in range(1, N + 1)}
for j in range(COLUMN_COUNT):
count = 0
top_row = int(board_col[j]) - 1
if top_row == -1:
continue
for i in range(top_row, ROW_COUNT, 1):
if j+top_row-i == -1 :
if count > 0:
returnMap[count] += 1
break
if board[i][j+top_row-i] == playerNumber:
count += 1
if count == N or i == 0:
returnMap[count] += 1
break
else:
if count > 0:
returnMap[count] += 1
break
return returnMap
def checkDiagonally2(board, board_col, playerNumber):
returnMap = {i: 0 for i in range(1, N + 1)}
for j in range(COLUMN_COUNT):
count = 0
top_row = board_col[j] - 1
if top_row == -1:
continue
for i in range(top_row, ROW_COUNT, 1):
if j+i-top_row == COLUMN_COUNT:
if count > 0:
returnMap[count] += 1
break
if board[i][j+i-top_row] == playerNumber:
count += 1
if count == N or i == 0:
returnMap[count] += 1
break
else:
if count > 0:
returnMap[count] += 1
break
return returnMap
def checkHorizontally(board, board_col, playerNumber):
returnMap = {i: 0 for i in range(1, N + 1)}
for j in range(COLUMN_COUNT):
count = 0
top_row = board_col[j]
if top_row == -1:
continue
# Check to the right
for j2 in range(j + 1, min(COLUMN_COUNT, j + N)):
if board[top_row][j2] == playerNumber:
count += 1
else:
break
# Check to the left
for j2 in range(j - 1, max(-1, j - N), -1):
if board[top_row][j2] == playerNumber:
count += 1
else:
break
if count > 0 :
if count >= N:
returnMap[N] += 1
else:
returnMap[count] += 1
return returnMap
def checkVertically(board, board_col, playerNumber):
returnMap = {i: 0 for i in range(1, N + 1)}
for j in range(COLUMN_COUNT):
count = 0
top_row = board_col[j] + 1
if top_row == -1:
continue
for i in range(top_row, ROW_COUNT, 1):
if board[i][j] == playerNumber:
count += 1
if count == N or i == ROW_COUNT:
returnMap[count] += 1
break
else:
if count > 0:
returnMap[count] += 1
break
return returnMap
def getPlays(board, board_col, playerNumber):
returnMap = {}
player_h = checkHorizontally(board, board_col, playerNumber)
player_v = checkVertically(board, board_col, playerNumber)
player_d1 = checkDiagonally1(board, board_col, playerNumber)
player_d2 = checkDiagonally2(board, board_col, playerNumber)
for i in range(1, N+1):
returnMap[i] = player_h[i] + player_v[i] + player_d1[i] + player_d2[i]
return returnMap
def calcHeuristic(board, board_col, MyPlayerPiece):
h = 0
constant1 = 100 / N
constant2 = 100
all_single_plays = True
myPlays = getPlays(board, board_col, MyPlayerPiece)
opp_piece = PLAYER_PIECE
if MyPlayerPiece == PLAYER_PIECE:
opp_piece = AI_PIECE
opponentPlays = getPlays(board, board_col, opp_piece)
for a in range(1, N+1):
if a > 1:
all_single_plays = not (myPlays[a] > 0 or opponentPlays[a] > 0)
h += (constant2 * a) * math.pow(myPlays[a], a) - (constant1 * a) * math.pow(opponentPlays[a], a)
'''
if a == N-1 :
h += 5*myPlays[a]
h -= 4*opponentPlays[a]
elif a == N-3 :
h += 2*myPlays[a]
'''
if myPlays[N] > 0 or (myPlays[1] > opponentPlays[1] and all_single_plays):
h = math.inf
if opponentPlays[N] > 0:
h = -math.inf
return h
def winning_move(board, piece):
# Check horizontal locations for win
for c in range(COLUMN_COUNT-N+1):
for r in range(ROW_COUNT):
for i in range(N):
if board[r][c+i] != piece:
break
else:
return True
# Check vertical locations for win
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT-N+1):
for i in range(N):
if board[r+i][c] != piece:
break
else:
return True
# Check positively sloped diagonals
for c in range(COLUMN_COUNT-N+1):
for r in range(ROW_COUNT-N+1):
for i in range(N):
if board[r+i][c+i] != piece:
break
else:
return True
# Check negatively sloped diagonals
for c in range(COLUMN_COUNT-N+1):
for r in range(N-1, ROW_COUNT):
for i in range(N):
if board[r-i][c+i] != piece:
break
else:
return True
# No winning move
return False
def minimax(board, board_col, depth, alpha, beta, maximizingPlayer):
valid_locations = get_valid_locations(board)
player_wins = winning_move(board, PLAYER_PIECE)
ai_wins = winning_move(board, AI_PIECE)
is_terminal = player_wins or ai_wins or len(valid_locations) == 0
if depth == 0 or is_terminal:
if is_terminal:
if ai_wins:
return (None, math.inf)
elif player_wins:
return (None, -math.inf)
else: # Game is over, no more valid moves
return (None, 0)
else: # Depth is zero
return (None, calcHeuristic(board, board_col, AI_PIECE))
if maximizingPlayer:
value = -math.inf
column = random.choice(valid_locations)
for col in valid_locations:
row = get_next_open_row(board, col)
b_copy = board.copy()
bcol_copy = board_col.copy()
drop_piece(b_copy, bcol_copy, row, col, AI_PIECE)
new_score = minimax(b_copy, bcol_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 = get_next_open_row(board, col)
b_copy = board.copy()
bcol_copy = board_col.copy()
drop_piece(b_copy, board_col, row, col, PLAYER_PIECE)
new_score = minimax(b_copy, board_col, 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 get_valid_locations(board):
valid_locations = []
for col in range(COLUMN_COUNT):
if is_valid_location(board, col):
valid_locations.append(col)
return valid_locations
def draw_board(board):
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
pygame.draw.rect(screen, BLUE, (c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE, SQUARESIZE, SQUARESIZE))
pygame.draw.circle(screen, BLACK, (int(c*SQUARESIZE+SQUARESIZE/2), int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)), RADIUS)
for c in range(COLUMN_COUNT):
for r in range(ROW_COUNT):
if board[r][c] == PLAYER_PIECE:
pygame.draw.circle(screen, RED, (int(c*SQUARESIZE+SQUARESIZE/2), int((r*SQUARESIZE+SQUARESIZE/2) + SQUARESIZE)), RADIUS)
elif board[r][c] == AI_PIECE:
pygame.draw.circle(screen, YELLOW, (int(c*SQUARESIZE+SQUARESIZE/2), int((r*SQUARESIZE+SQUARESIZE/2) + SQUARESIZE)), RADIUS)
pygame.display.update()
print_board(board)
game_over = False
pygame.init()
# Get the screen info
screen_info = pygame.display.Info()
screen_height = screen_info.current_h
SQUARESIZE = screen_height/(ROW_COUNT+3)
width = COLUMN_COUNT * SQUARESIZE
height = (ROW_COUNT+1) * SQUARESIZE
size = (width, height)
RADIUS = int(SQUARESIZE/2 - 5)
screen = pygame.display.set_mode(size)
draw_board(board)
pygame.display.update()
myfont = pygame.font.SysFont("monospace", 75)
turn = random.randint(PLAYER, AI)
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))
posx = event.pos[0]
if turn == PLAYER:
pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE/2)), RADIUS)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))
#print(event.pos)
# Ask for Player 1 Input
if turn == PLAYER:
posx = event.pos[0]
col = int(math.floor(posx/SQUARESIZE))
if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, board_col, row, col, PLAYER_PIECE)
if winning_move(board, PLAYER_PIECE):
label = myfont.render("Player 1 wins!!", 1, RED)
screen.blit(label, (40,10))
game_over = True
turn += 1
turn = turn % 2
print_board(board)
draw_board(board)
# Ask for Player 2 Input
if turn == AI and not game_over:
col, minimax_score = minimax(board, board_col, 5, -math.inf, math.inf, True)
if is_valid_location(board, col):
#pygame.time.wait(500)
row = get_next_open_row(board, col)
drop_piece(board, board_col, row, col, AI_PIECE)
if winning_move(board, AI_PIECE):
label = myfont.render("Player 2 wins!!", 1, YELLOW)
screen.blit(label, (40,10))
game_over = True
print_board(board)
draw_board(board)
turn += 1
turn = turn % 2
if game_over:
pygame.time.wait(3000)