-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect4.py
413 lines (299 loc) · 8.64 KB
/
connect4.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
import time
# todo need to introduce 'y', for now the board is a square
board = []
sides = [0, 2, 1]
boardSize, line = 5, 3
# currPos strings reflect the positions and are used as a key in tt
currPos = "0" * (boardSize*boardSize)
tt = {}
colsOrdered = []
nodes = 0
'''
for an efficient minimax search, move ordering is important, here
it should check the moves starting from the cantral columns going towards
the edges so for example
for a range 0-4 it should be e.g. [2,1,3,0,4]
for a range 0-5 it should be e.g. [2,3,1,4,0,5]
'''
def setUpMoveOrdering():
global colsOrdered
col = -1
if boardSize % 2 == 1:
col = int(boardSize/2)
colsOrdered.append(col)
for i in range(1, int(boardSize/2)+1):
colsOrdered.append(col+i)
colsOrdered.append(col-i)
else:
col1 = int(boardSize/2) - 1
col2 = int(boardSize/2)
colsOrdered.append(col)
for i in range(1, int(boardSize/2)):
colsOrdered.append(col1-i)
colsOrdered.append(col2+i)
pass
'''
on the fly build a string that reflects the current
position. useful for debugging.
'''
def buildPositionString():
global board
s = ""
for row in range(boardSize):
for col in range(boardSize):
s = s + str(board[row][col])
return s
pass
def printBoard():
global board
for row in board:
for e in row:
print (e, end=" ")
print()
pass
def genMoves():
global board
moves = []
for col in range(boardSize):
if board[0][col] == 0:
row = 0
while row < boardSize:
if board[row][col] != 0:
break
row += 1
row -= 1
moves.append([row, col])
printBoard()
print(moves)
return moves
pass
'''
look at the square [row,col], what color is on it (1,2 or 0), then check if
a line has been formed going through [row,col], made up of discs of this color
'''
def checkForLine(row, col):
global board, line
side = board[row][col]
# horizontal
x, y = 0, row
aligned = 0
while x < boardSize:
if board[y][x] == side:
aligned += 1
if aligned >= line:
return True
else:
aligned = 0
x += 1
# vertical
x, y = col, 0
aligned = 0
while y < boardSize:
if board[y][x] == side:
aligned += 1
if aligned >= line:
return True
else:
aligned = 0
y += 1
# diagonals
x, y = col, row
while x+1 < boardSize and y+1 < boardSize:
x += 1
y += 1
aligned = 0
while x >= 0 and y >= 0:
if board[y][x] == side:
aligned += 1
if aligned >= line:
return True
else:
aligned = 0
x -= 1
y -= 1
x, y = col, row
while x+1 < boardSize and y > 0:
x += 1
y -= 1
aligned = 0
while x >= 0 and y < boardSize:
if board[y][x] == side:
aligned += 1
if aligned >= line:
return True
else:
aligned = 0
x -= 1
y += 1
return False
pass
def scanBoardForLine(side):
global board
for i in range(boardSize):
for j in range(boardSize):
if board[i][j] == side and checkForLine(i, j):
return True
return False
pass
def miniMax(side, depth):
global board, sides, currPos, nodes, colsOrdered
nodes += 1
sideOpp = sides[side]
if currPos + str(side) in tt:
return tt[currPos + str(side)]
elif currPos + str(sideOpp) in tt:
return -(tt[currPos + str(sideOpp)])
moveMade = 0
bestScore = -10000
for col in colsOrdered:
if board[0][col] == 0:
moveMade = 1
row = 0
while row < boardSize:
if board[row][col] != 0:
break
row += 1
row -= 1
board[row][col] = side
i = boardSize*row + col
currPos = currPos[:i] + str(side) + currPos[i+1:]
score = 0
if checkForLine(row, col):
score = 10000 - depth
else:
score = -miniMax(sideOpp, depth+1)
board[row][col] = 0
currPos = currPos[:i] + "0" + currPos[i+1:]
if score > bestScore:
bestScore = score
if bestScore > 9000:
break
if moveMade == 0:
return 0
ttPos = currPos + str(side)
tt[ttPos] = bestScore
assert (buildPositionString() == currPos)
return bestScore
pass
def miniMaxMain(side, depth):
global board, sides, currPos, nodes, colsOrdered
nodes += 1
sideOpp = sides[side]
moveMade = 0
bestScore = -10000
bestMove = [-1, -1]
for col in colsOrdered:
print('trying a move in main minimax')
if board[0][col] == 0:
moveMade = 1
row = 0
while row < boardSize:
if board[row][col] != 0:
break
row += 1
row -= 1
board[row][col] = side
i = boardSize * row + col
currPos = currPos[:i] + str(side) + currPos[i+1:]
score = 0
if checkForLine(row, col):
score = 10000 - depth
else:
score = -miniMax(sideOpp, depth+1)
board[row][col] = 0
currPos = currPos[:i] + "0" + currPos[i+1:]
if score > bestScore:
bestScore = score
bestMove = [row, col]
# optimization, not perfect as might miss some faster wins
if bestScore > 9000:
break
print("max score is ", bestScore)
if moveMade == 0:
#print("game over. it's a draw.")
pass
return bestMove
pass
def getMove():
global board
row = 0
while True:
col = input("enter a move. number to indicate the column ")
try:
col = int(col)
except ValueError:
print("That's not an int!")
continue
# make sure its in the interval 0 - boardSize-1
if col < 0 or col >= boardSize:
print("really need to enter number between 0 and ", boardSize-1)
continue
# if the column all filled, prompt again
if board[0][col] != 0:
print("looks like this column is filled")
continue
while row < boardSize:
if board[row][col] != 0:
break
row += 1
row -= 1
break
return [row, col]
pass
def playConnect4():
global board, sides, currPos, nodes
setUpMoveOrdering()
humSide, compSide = 1, 2
sideToPlay = 2
while True:
try:
sideToPlay = int(input("pick who starts. 1 - human, 2 - computer"))
except ValueError:
print("not an int!")
continue
if sideToPlay == 1 or sideToPlay == 2:
break
else:
print("not a correct value!")
while True:
print("--------------------------------")
if sideToPlay == humSide:
mv = getMove()
board[mv[0]][mv[1]] = humSide
i = boardSize*(mv[0]) + mv[1]
currPos = currPos[: i] + str(humSide) + currPos[i+1:]
else:
start = time.time()
nodes = 0
mv = miniMaxMain(compSide, 0)
board[mv[0]][mv[1]] = compSide
i = boardSize*(mv[0]) + mv[1]
currPos = currPos[:i] + str(compSide) + currPos[i+1:]
end = time.time()
print("time elapsed ", end - start)
print("nm of nodes", nodes)
printBoard()
if scanBoardForLine(sideToPlay):
break
sideToPlay = sides[sideToPlay]
pass
board = [[0]*boardSize for _ in range(boardSize)]
playConnect4()
'''
testing checkForLine on a 5x5 board
'''
def mytest(pos, coords, rets):
for i in range(len(pos)):
for j in range(len(pos[0])):
board[i][j] = int(pos[i][j])
for i in range(len(coords)):
assert(checkForLine(coords[i][0], coords[i][1]) == rets[i])
pass
newpos = ['10000', '01000', '00100', '00020', '11121']
coords = [[4, 1], [4, 3], [0, 0], [1, 1], [2, 2], [3, 3]]
rets = [True, False, True, True, True, False]
#mytest(newpos, coords, rets)
newpos = ['11221', '22112', '11221', '22112', '11221']
coords = [[j, i] for j in range(5) for i in range(5)]
rets = (5*5) * [False]
#mytest(newpos, coords, rets)