-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect4.py
352 lines (327 loc) · 10.1 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
# coding=UTF8
# Python Connect4 game with AI using MTD(f) algorithm
# Author: Maurits van der Schee <[email protected]>
from Tkinter import Tk, Button, Frame, Canvas
from tkFont import Font
from copy import deepcopy
from time import time
class Board:
nodes = {}
def __init__(self,other=None):
self.player = 'X'
self.opponent = 'O'
self.empty = '.'
self.width = 7
self.height = 6
self.fields = {}
for y in range(self.height):
for x in range(self.width):
self.fields[x,y] = self.empty
# copy constructor
if other:
self.__dict__ = deepcopy(other.__dict__)
def move(self,x):
board = Board(self)
for y in range(board.height):
if board.fields[x,y] == board.empty:
board.fields[x,y] = board.player
break
board.player,board.opponent = board.opponent,board.player
return board
def __heuristic(self):
return self.__heuristic_score(self.player)-self.__heuristic_score(self.opponent)
def __heuristic_score(self, player):
lines = self.__winlines(player)
winpositions = self.__winpositions(lines,player)
score = 0
for x in range(self.width):
for y in range(self.height-1,0,-1):
win = winpositions.get("{0},{1}".format(x,y),False)
below = winpositions.get("{0},{1}".format(x,y-1),False)
if win and below:
score+=self.height-y*100
for line in lines:
pieces = 0
height = []
for x,y in line:
if self.fields[x,y] == player:
pieces = pieces + 1
elif self.fields[x,y] == self.empty:
height.append(y)
heightscore = self.height - int(sum(height) / float(len(height)))
score=score+pieces*heightscore
return score
def __winpositions(self, lines, player):
lines = self.__winlines(player)
winpositions = {}
for line in lines:
pieces = 0
empty = None
for x,y in line:
if self.fields[x,y] == player:
pieces = pieces + 1
elif self.fields[x,y] == self.empty:
if not empty == None:
break
empty = (x,y)
if pieces==3:
winpositions["{0},{1}".format(x,y)]=True
return winpositions
def __winlines(self, player):
lines = []
# horizontal
for y in range(self.height):
winning = []
for x in range(self.width):
if self.fields[x,y] == player or self.fields[x,y] == self.empty:
winning.append((x,y))
if len(winning) >= 4:
lines.append(winning[-4:])
else:
winning = []
# vertical
for x in range(self.width):
winning = []
for y in range(self.height):
if self.fields[x,y] == player or self.fields[x,y] == self.empty:
winning.append((x,y))
if len(winning) >= 4:
lines.append(winning[-4:])
else:
winning = []
# diagonal
winning = []
for cx in range(self.width-1):
sx,sy = max(cx-2,0),abs(min(cx-2,0))
winning = []
for cy in range(self.height):
x,y = sx+cy,sy+cy
if x<0 or y<0 or x>=self.width or y>=self.height:
continue
if self.fields[x,y] == player or self.fields[x,y] == self.empty:
winning.append((x,y))
if len(winning) >= 4:
lines.append(winning[-4:])
else:
winning = []
# other diagonal
winning = []
for cx in range(self.width-1):
sx,sy = self.width-1-max(cx-2,0),abs(min(cx-2,0))
winning = []
for cy in range(self.height):
x,y = sx-cy,sy+cy
if x<0 or y<0 or x>=self.width or y>=self.height:
continue
if self.fields[x,y] == player or self.fields[x,y] == self.empty:
winning.append((x,y))
if len(winning) >= 4:
lines.append(winning[-4:])
else:
winning = []
# return
return lines
def __iterative_deepening(self,think):
g = (3,None)
start = time()
for d in range(1,10):
g = self.__mtdf(g, d)
if time()-start>think:
break
return g;
def __mtdf(self, g, d):
upperBound = +1000
lowerBound = -1000
best = g
while lowerBound < upperBound:
if g[0] == lowerBound:
beta = g[0]+1
else:
beta = g[0]
g = self.__minimax(True, d, beta-1, beta)
if g[1]!=None:
best = g
if g[0] < beta:
upperBound = g[0]
else:
lowerBound = g[0]
return best
def __minimax(self, player, depth, alpha, beta):
lower = Board.nodes.get(str(self)+str(depth)+'lower',None)
upper = Board.nodes.get(str(self)+str(depth)+'upper',None)
if lower != None:
if lower >= beta:
return (lower,None)
alpha = max(alpha,lower)
if upper != None:
if upper <= alpha:
return (upper,None)
beta = max(beta,upper)
if self.won():
if player:
return (-999,None)
else:
return (+999,None)
elif self.tied():
return (0,None)
elif depth==0:
return (self.__heuristic(),None)
elif player:
best = (alpha,None)
for x in range(self.width):
if self.fields[x,self.height-1]==self.empty:
value = self.move(x).__minimax(not player,depth-1,best[0],beta)[0]
if value>best[0]:
best = value,x
if value>beta:
break
else:
best = (beta,None)
for x in range(self.width):
if self.fields[x,self.height-1]==self.empty:
value = self.move(x).__minimax(not player,depth-1,alpha,best[0])[0]
if value<best[0]:
best = value,x
if alpha>value:
break
if best[0] <= alpha:
Board.nodes[str(self)+str(depth)+"upper"] = best[0]
Board.nodes[self.__mirror()+str(depth)+"upper"] = best[0]
elif best[0] >= beta:
Board.nodes[str(self)+str(depth)+"lower"] = best[0]
Board.nodes[self.__mirror()+str(depth)+"lower"] = best[0]
return best
def best(self):
return self.__iterative_deepening(2)[1]
def tied(self):
for (x,y) in self.fields:
if self.fields[x,y]==self.empty:
return False
return True
def won(self):
# horizontal
for y in range(self.height):
winning = []
for x in range(self.width):
if self.fields[x,y] == self.opponent:
winning.append((x,y))
if len(winning) == 4:
return winning
else:
winning = []
# vertical
for x in range(self.width):
winning = []
for y in range(self.height):
if self.fields[x,y] == self.opponent:
winning.append((x,y))
if len(winning) == 4:
return winning
else:
winning = []
# diagonal
winning = []
for cx in range(self.width-1):
sx,sy = max(cx-2,0),abs(min(cx-2,0))
winning = []
for cy in range(self.height):
x,y = sx+cy,sy+cy
if x<0 or y<0 or x>=self.width or y>=self.height:
continue
if self.fields[x,y] == self.opponent:
winning.append((x,y))
if len(winning) == 4:
return winning
else:
winning = []
# other diagonal
winning = []
for cx in range(self.width-1):
sx,sy = self.width-1-max(cx-2,0),abs(min(cx-2,0))
winning = []
for cy in range(self.height):
x,y = sx-cy,sy+cy
if x<0 or y<0 or x>=self.width or y>=self.height:
continue
if self.fields[x,y] == self.opponent:
winning.append((x,y))
if len(winning) == 4:
return winning
else:
winning = []
# default
return None
def __mirror(self):
string = ''
for y in range(self.height):
for x in range(self.width):
string+=' '+self.fields[self.width-1-x,self.height-1-y]
string+="\n"
return string
def __str__(self):
string = ''
for y in range(self.height):
for x in range(self.width):
string+=' '+self.fields[x,self.height-1-y]
string+="\n"
return string
class GUI:
def __init__(self):
self.app = Tk()
self.app.title('Connect4')
self.app.resizable(width=False, height=False)
self.board = Board()
self.buttons = {}
self.frame = Frame(self.app, borderwidth=1, relief="raised")
self.tiles = {}
for x in range(self.board.width):
handler = lambda x=x: self.move(x)
button = Button(self.app, command=handler, font=Font(family="Helvetica", size=14), text=x+1)
button.grid(row=0, column=x, sticky="WE")
self.buttons[x] = button
self.frame.grid(row=1, column=0, columnspan=self.board.width)
for x,y in self.board.fields:
tile = Canvas(self.frame, width=60, height=50, bg="navy", highlightthickness=0)
tile.grid(row=self.board.height-1-y, column=x)
self.tiles[x,y] = tile
handler = lambda: self.reset()
self.restart = Button(self.app, command=handler, text='reset')
self.restart.grid(row=2, column=0, columnspan=self.board.width, sticky="WE")
self.update()
def reset(self):
self.board = Board()
self.update()
def move(self,x):
self.app.config(cursor="watch")
self.app.update()
self.board = self.board.move(x)
self.update()
move = self.board.best()
if move!=None:
self.board = self.board.move(move)
self.update()
self.app.config(cursor="")
def update(self):
for (x,y) in self.board.fields:
text = self.board.fields[x,y]
if (text=='.'):
self.tiles[x,y].create_oval(10, 5, 50, 45, fill="black", outline="blue", width=1)
if (text=='X'):
self.tiles[x,y].create_oval(10, 5, 50, 45, fill="yellow", outline="blue", width=1)
if (text=='O'):
self.tiles[x,y].create_oval(10, 5, 50, 45, fill="red", outline="blue", width=1)
for x in range(self.board.width):
if self.board.fields[x,self.board.height-1]==self.board.empty:
self.buttons[x]['state'] = 'normal'
else:
self.buttons[x]['state'] = 'disabled'
winning = self.board.won()
if winning:
for x,y in winning:
self.tiles[x,y].create_oval(25, 20, 35, 30, fill="black")
for x in range(self.board.width):
self.buttons[x]['state'] = 'disabled'
def mainloop(self):
self.app.mainloop()
if __name__ == '__main__':
GUI().mainloop()