-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxConnect4Game.py
More file actions
422 lines (397 loc) · 19.9 KB
/
MaxConnect4Game.py
File metadata and controls
422 lines (397 loc) · 19.9 KB
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
413
414
415
416
417
418
419
420
421
422
'''
MaxConnect4Game.py contains the minmax, alpha beta, beta aplha and eval functions. it also contains all the required function to calculate the streak of a given function
'''
import copy # using deepcopy to copy the object
import random #
import sys # importing all sys file
utilityVal = {} # storing the utility val
infinity = float('inf') # defining a infinite val
class MaxConnect4game: #
def __init__(self): # intilising the function
self.GameBoard = [[0 for i in range(7)] for j in range(6)]
self.currentMove = 0
self.pieceCount = 0
self.player1Score = 0
self.player2Score = 0
self.gameFile = None
self.computerColumn = None
self.depth = 1
def checkPieceCount(self): # checking if count the number of piece already played
self.pieceCount = sum(1 for row in self.GameBoard for piece in row if piece)
def getPieceCount(self): # returning the total piece of the borad which are occupied
return sum(1 for row in self.GameBoard for piece in row if piece)
def displayGB(self): # function for diplaying the game borad
print(' -----------------')
for i in range(6):
print(' |'),
for j in range(7):
print('%d' % int(self.GameBoard[i][j])),
print('| ')
print(' -----------------')
def printGameBoardToFile(self): # function for printing the game borad to file
for row in self.GameBoard:
self.gameFile.write(''.join(str(col) for col in row) + '\r')
self.gameFile.write('%s\r' % str(self.currentMove))
def playPiece(self, column): # function for placing the current player piece in the column
if not self.GameBoard[0][column]:
for i in range(5, -1, -1):
if not self.GameBoard[i][column]:
self.GameBoard[i][column] = self.currentMove
self.pieceCount += 1
return 1
def checkPiece(self, column, opponent): # function for checking if the piece is valid or not
if not self.GameBoard[0][column]:
for i in range(5, -1, -1):
if not self.GameBoard[i][column]:
self.GameBoard[i][column] = opponent
self.pieceCount += 1
return 1
def maxVal(self, currentNode): # function for resturing the state for the maximun value
node = copy.deepcopy(currentNode)
childNode = []
for i in range(7):
currentState = self.playPiece(i)
if currentState != None:
childNode.append(self.GameBoard)
self.GameBoard = copy.deepcopy(node)
return childNode
def minVal(self, currentNode): # function for returning the state for the minimun value
node = copy.deepcopy(currentNode)
if self.currentMove == 1:
opponent = 2
elif self.currentMove == 2:
opponent = 1
childNode = []
for i in range(7):
currentState = self.checkPiece(i, opponent)
if currentState != None:
childNode.append(self.GameBoard)
self.GameBoard = copy.deepcopy(node)
return childNode
def alphaBeta(self, currentNode, alpha, beta, depth): # function for alpha beta puring (max val)
value = -infinity
childNode = self.maxVal(currentNode)
if childNode == [] or depth == 0:
self.scoreCount()
return self.evalCalc(self.GameBoard)
else:
for node in childNode:
self.GameBoard = copy.deepcopy(node)
value = max(value, self.betaAlpha(node, alpha, beta, depth - 1))
if value >= beta:
return value
alpha = max(alpha, value)
return value
def betaAlpha(self,currentNode, alpha, beta, depth): # function for beta alpha puring (min val)
value = infinity
childNode = self.minVal(currentNode)
if childNode == [] or depth == 0:
self.scoreCount()
return self.evalCalc(self.GameBoard)
else:
for node in childNode:
self.GameBoard = copy.deepcopy(node)
value = min(value, self.alphaBeta(node, alpha, beta, depth - 1))
if value <= alpha:
return value
beta = min(beta, value)
return value
def minMax(self, depth): # plain min max algorithm
currentState = copy.deepcopy(self.GameBoard)
for i in range(7):
if self.playPiece(i) != None:
if self.pieceCount == 42 or self.depth == 0:
self.GameBoard = copy.deepcopy(currentState)
return i
else:
val = self.betaAlpha(self.GameBoard, -infinity, infinity, depth - 1)
utilityVal[i] = val
self.GameBoard = copy.deepcopy(currentState)
maxUtilityVal = max([i for i in utilityVal.values()])
for i in range(7):
if i in utilityVal:
if utilityVal[i] == maxUtilityVal:
utilityVal.clear()
return i
def verticalCheck(self, row, column, state, streak): # checking the vertical piece in the row
consecutiveCount = 0
for i in range(row, 6):
if state[i][column] == state[row][column]:
consecutiveCount += 1
else:
break
if consecutiveCount >= streak:
return 1
else:
return 0
def horizontalCheck(self, row, column, state, streak): # checking for the horizontal piece in the column
count = 0
for j in range(column, 7):
if state[row][j] == state[row][column]:
count += 1
else:
break
if count >= streak:
return 1
else:
return 0
def diagonalCheck(self, row, column, state, streak): # checking for the diagonal check in the borad
total = 0
count = 0
j = column
for i in range(row, 6):
if j > 6:
break
elif state[i][j] == state[row][column]:
count += 1
else:
break
j += 1
if count >= streak:
total += 1
count = 0
j = column
for i in range(row, -1, -1):
if j > 6:
break
elif state[i][j] == state[row][column]:
count += 1
else:
break
j += 1
if count >= streak:
total += 1
return total
def streakCalc(self, state, color, streak): # function for checking which type of connectfour goes
count = 0
for i in range(6):
for j in range(7):
if state[i][j] == color:
count += self.verticalCheck(i, j, state, streak)
count += self.horizontalCheck(i, j, state, streak)
count += self.diagonalCheck(i, j, state, streak)
return count
def playerEvalCalc(self, state): # calculating the streak for the non computer player
playerFours = self.streakCalc(state, self.currentMove, 4)
playerThrees = self.streakCalc(state, self.currentMove, 3)
playerTwos = self.streakCalc(state, self.currentMove, 2)
return (playerFours * 37044 + playerThrees * 882 + playerTwos * 21) # numbers are the permutation for each fours in a row, threes in a row and twos in a row
def evalFunc(self): # function for the which next color is
if self.currentMove == 1:
oneMoveColor = 2
elif self.currentMove == 2:
oneMoveColor = 1
return oneMoveColor
def compEvalCalc(self, state): # calculating the streak for the computer
oneMoveColor = self.evalFunc()
compFours = self.streakCalc(state, oneMoveColor, 4)
compThrees = self.streakCalc(state, oneMoveColor, 3)
compTwos = self.streakCalc(state, oneMoveColor, 2)
return (compFours * 37044 + compThrees * 882 + compTwos * 21)
def evalCalc(self, state): #function for calucating the difference in the steak of computer vs non computer
return self.playerEvalCalc(state) - self.compEvalCalc(state)
def changeMove(self): # function for changing to next player
if self.currentMove == 1:
self.currentMove = 2
elif self.currentMove == 2:
self.currentMove = 1
def aiPlay(self): # function for computing the computer move
randomCol = self.minMax(int(self.depth))
result = self.playPiece(randomCol)
if not result:
print('No Result')
else:
print('Player: %d, Column: %d\n' % (self.currentMove, randomCol + 1))
self.changeMove()
def scoreCount(self): # function for returning the score count i.e. fours in a row
self.player1Score = 0;
self.player2Score = 0;
# Check horizontally
for row in self.GameBoard:
# Check player 1
if row[0:4] == [1] * 4:
self.player1Score += 1
if row[1:5] == [1] * 4:
self.player1Score += 1
if row[2:6] == [1] * 4:
self.player1Score += 1
if row[3:7] == [1] * 4:
self.player1Score += 1
# Check player 2
if row[0:4] == [2] * 4:
self.player2Score += 1
if row[1:5] == [2] * 4:
self.player2Score += 1
if row[2:6] == [2] * 4:
self.player2Score += 1
if row[3:7] == [2] * 4:
self.player2Score += 1
# Check vertically
for j in range(7):
# Check player 1
if (self.GameBoard[0][j] == 1 and self.GameBoard[1][j] == 1 and
self.GameBoard[2][j] == 1 and self.GameBoard[3][j] == 1):
self.player1Score += 1
if (self.GameBoard[1][j] == 1 and self.GameBoard[2][j] == 1 and
self.GameBoard[3][j] == 1 and self.GameBoard[4][j] == 1):
self.player1Score += 1
if (self.GameBoard[2][j] == 1 and self.GameBoard[3][j] == 1 and
self.GameBoard[4][j] == 1 and self.GameBoard[5][j] == 1):
self.player1Score += 1
# Check player 2
if (self.GameBoard[0][j] == 2 and self.GameBoard[1][j] == 2 and
self.GameBoard[2][j] == 2 and self.GameBoard[3][j] == 2):
self.player2Score += 1
if (self.GameBoard[1][j] == 2 and self.GameBoard[2][j] == 2 and
self.GameBoard[3][j] == 2 and self.GameBoard[4][j] == 2):
self.player2Score += 1
if (self.GameBoard[2][j] == 2 and self.GameBoard[3][j] == 2 and
self.GameBoard[4][j] == 2 and self.GameBoard[5][j] == 2):
self.player2Score += 1
# Check diagonally
# Check player 1
if (self.GameBoard[2][0] == 1 and self.GameBoard[3][1] == 1 and
self.GameBoard[4][2] == 1 and self.GameBoard[5][3] == 1):
self.player1Score += 1
if (self.GameBoard[1][0] == 1 and self.GameBoard[2][1] == 1 and
self.GameBoard[3][2] == 1 and self.GameBoard[4][3] == 1):
self.player1Score += 1
if (self.GameBoard[2][1] == 1 and self.GameBoard[3][2] == 1 and
self.GameBoard[4][3] == 1 and self.GameBoard[5][4] == 1):
self.player1Score += 1
if (self.GameBoard[0][0] == 1 and self.GameBoard[1][1] == 1 and
self.GameBoard[2][2] == 1 and self.GameBoard[3][3] == 1):
self.player1Score += 1
if (self.GameBoard[1][1] == 1 and self.GameBoard[2][2] == 1 and
self.GameBoard[3][3] == 1 and self.GameBoard[4][4] == 1):
self.player1Score += 1
if (self.GameBoard[2][2] == 1 and self.GameBoard[3][3] == 1 and
self.GameBoard[4][4] == 1 and self.GameBoard[5][5] == 1):
self.player1Score += 1
if (self.GameBoard[0][1] == 1 and self.GameBoard[1][2] == 1 and
self.GameBoard[2][3] == 1 and self.GameBoard[3][4] == 1):
self.player1Score += 1
if (self.GameBoard[1][2] == 1 and self.GameBoard[2][3] == 1 and
self.GameBoard[3][4] == 1 and self.GameBoard[4][5] == 1):
self.player1Score += 1
if (self.GameBoard[2][3] == 1 and self.GameBoard[3][4] == 1 and
self.GameBoard[4][5] == 1 and self.GameBoard[5][6] == 1):
self.player1Score += 1
if (self.GameBoard[0][2] == 1 and self.GameBoard[1][3] == 1 and
self.GameBoard[2][4] == 1 and self.GameBoard[3][5] == 1):
self.player1Score += 1
if (self.GameBoard[1][3] == 1 and self.GameBoard[2][4] == 1 and
self.GameBoard[3][5] == 1 and self.GameBoard[4][6] == 1):
self.player1Score += 1
if (self.GameBoard[0][3] == 1 and self.GameBoard[1][4] == 1 and
self.GameBoard[2][5] == 1 and self.GameBoard[3][6] == 1):
self.player1Score += 1
if (self.GameBoard[0][3] == 1 and self.GameBoard[1][2] == 1 and
self.GameBoard[2][1] == 1 and self.GameBoard[3][0] == 1):
self.player1Score += 1
if (self.GameBoard[0][4] == 1 and self.GameBoard[1][3] == 1 and
self.GameBoard[2][2] == 1 and self.GameBoard[3][1] == 1):
self.player1Score += 1
if (self.GameBoard[1][3] == 1 and self.GameBoard[2][2] == 1 and
self.GameBoard[3][1] == 1 and self.GameBoard[4][0] == 1):
self.player1Score += 1
if (self.GameBoard[0][5] == 1 and self.GameBoard[1][4] == 1 and
self.GameBoard[2][3] == 1 and self.GameBoard[3][2] == 1):
self.player1Score += 1
if (self.GameBoard[1][4] == 1 and self.GameBoard[2][3] == 1 and
self.GameBoard[3][2] == 1 and self.GameBoard[4][1] == 1):
self.player1Score += 1
if (self.GameBoard[2][3] == 1 and self.GameBoard[3][2] == 1 and
self.GameBoard[4][1] == 1 and self.GameBoard[5][0] == 1):
self.player1Score += 1
if (self.GameBoard[0][6] == 1 and self.GameBoard[1][5] == 1 and
self.GameBoard[2][4] == 1 and self.GameBoard[3][3] == 1):
self.player1Score += 1
if (self.GameBoard[1][5] == 1 and self.GameBoard[2][4] == 1 and
self.GameBoard[3][3] == 1 and self.GameBoard[4][2] == 1):
self.player1Score += 1
if (self.GameBoard[2][4] == 1 and self.GameBoard[3][3] == 1 and
self.GameBoard[4][2] == 1 and self.GameBoard[5][1] == 1):
self.player1Score += 1
if (self.GameBoard[1][6] == 1 and self.GameBoard[2][5] == 1 and
self.GameBoard[3][4] == 1 and self.GameBoard[4][3] == 1):
self.player1Score += 1
if (self.GameBoard[2][5] == 1 and self.GameBoard[3][4] == 1 and
self.GameBoard[4][3] == 1 and self.GameBoard[5][2] == 1):
self.player1Score += 1
if (self.GameBoard[2][6] == 1 and self.GameBoard[3][5] == 1 and
self.GameBoard[4][4] == 1 and self.GameBoard[5][3] == 1):
self.player1Score += 1
# Check player 2
if (self.GameBoard[2][0] == 2 and self.GameBoard[3][1] == 2 and
self.GameBoard[4][2] == 2 and self.GameBoard[5][3] == 2):
self.player2Score += 1
if (self.GameBoard[1][0] == 2 and self.GameBoard[2][1] == 2 and
self.GameBoard[3][2] == 2 and self.GameBoard[4][3] == 2):
self.player2Score += 1
if (self.GameBoard[2][1] == 2 and self.GameBoard[3][2] == 2 and
self.GameBoard[4][3] == 2 and self.GameBoard[5][4] == 2):
self.player2Score += 1
if (self.GameBoard[0][0] == 2 and self.GameBoard[1][1] == 2 and
self.GameBoard[2][2] == 2 and self.GameBoard[3][3] == 2):
self.player2Score += 1
if (self.GameBoard[1][1] == 2 and self.GameBoard[2][2] == 2 and
self.GameBoard[3][3] == 2 and self.GameBoard[4][4] == 2):
self.player2Score += 1
if (self.GameBoard[2][2] == 2 and self.GameBoard[3][3] == 2 and
self.GameBoard[4][4] == 2 and self.GameBoard[5][5] == 2):
self.player2Score += 1
if (self.GameBoard[0][1] == 2 and self.GameBoard[1][2] == 2 and
self.GameBoard[2][3] == 2 and self.GameBoard[3][4] == 2):
self.player2Score += 1
if (self.GameBoard[1][2] == 2 and self.GameBoard[2][3] == 2 and
self.GameBoard[3][4] == 2 and self.GameBoard[4][5] == 2):
self.player2Score += 1
if (self.GameBoard[2][3] == 2 and self.GameBoard[3][4] == 2 and
self.GameBoard[4][5] == 2 and self.GameBoard[5][6] == 2):
self.player2Score += 1
if (self.GameBoard[0][2] == 2 and self.GameBoard[1][3] == 2 and
self.GameBoard[2][4] == 2 and self.GameBoard[3][5] == 2):
self.player2Score += 1
if (self.GameBoard[1][3] == 2 and self.GameBoard[2][4] == 2 and
self.GameBoard[3][5] == 2 and self.GameBoard[4][6] == 2):
self.player2Score += 1
if (self.GameBoard[0][3] == 2 and self.GameBoard[1][4] == 2 and
self.GameBoard[2][5] == 2 and self.GameBoard[3][6] == 2):
self.player2Score += 1
if (self.GameBoard[0][3] == 2 and self.GameBoard[1][2] == 2 and
self.GameBoard[2][1] == 2 and self.GameBoard[3][0] == 2):
self.player2Score += 1
if (self.GameBoard[0][4] == 2 and self.GameBoard[1][3] == 2 and
self.GameBoard[2][2] == 2 and self.GameBoard[3][1] == 2):
self.player2Score += 1
if (self.GameBoard[1][3] == 2 and self.GameBoard[2][2] == 2 and
self.GameBoard[3][1] == 2 and self.GameBoard[4][0] == 2):
self.player2Score += 1
if (self.GameBoard[0][5] == 2 and self.GameBoard[1][4] == 2 and
self.GameBoard[2][3] == 2 and self.GameBoard[3][2] == 2):
self.player2Score += 1
if (self.GameBoard[1][4] == 2 and self.GameBoard[2][3] == 2 and
self.GameBoard[3][2] == 2 and self.GameBoard[4][1] == 2):
self.player2Score += 1
if (self.GameBoard[2][3] == 2 and self.GameBoard[3][2] == 2 and
self.GameBoard[4][1] == 2 and self.GameBoard[5][0] == 2):
self.player2Score += 1
if (self.GameBoard[0][6] == 2 and self.GameBoard[1][5] == 2 and
self.GameBoard[2][4] == 2 and self.GameBoard[3][3] == 2):
self.player2Score += 1
if (self.GameBoard[1][5] == 2 and self.GameBoard[2][4] == 2 and
self.GameBoard[3][3] == 2 and self.GameBoard[4][2] == 2):
self.player2Score += 1
if (self.GameBoard[2][4] == 2 and self.GameBoard[3][3] == 2 and
self.GameBoard[4][2] == 2 and self.GameBoard[5][1] == 2):
self.player2Score += 1
if (self.GameBoard[1][6] == 2 and self.GameBoard[2][5] == 2 and
self.GameBoard[3][4] == 2 and self.GameBoard[4][3] == 2):
self.player2Score += 1
if (self.GameBoard[2][5] == 2 and self.GameBoard[3][4] == 2 and
self.GameBoard[4][3] == 2 and self.GameBoard[5][2] == 2):
self.player2Score += 1
if (self.GameBoard[2][6] == 2 and self.GameBoard[3][5] == 2 and
self.GameBoard[4][4] == 2 and self.GameBoard[5][3] == 2):
self.player2Score += 1