-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
543 lines (391 loc) · 16 KB
/
main.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import pygame
import math
import time
width = 700
height = 700
cellSize = 100
turnToPlay = "Computer"
turn = 0
playerSelectedPieceArr = []
run = True
selectedPiece = None
bestMove = None
board = [ #1's are AI's pieces and 2s are Player's
[1,0,0,0,0,0,2],
[0,0,0,0,0,0,0],
[1,0,0,0,0,0,2],
[0,0,0,0,0,0,0],
[2,0,0,0,0,0,1],
[0,0,0,0,0,0,0],
[2,0,0,0,0,0,1]
]
def drawBoard():
for i in range(0,7):
for j in range(0,7):
pygame.draw.rect(backSurface, 'black', pygame.Rect(j * cellSize, i * cellSize, cellSize, cellSize), 1)
def drawPieces():
for i, row in enumerate(board):
for j, val in enumerate(row):
if(val == 1):
pygame.draw.polygon(backSurface, 'red', ((j * cellSize + 50, i * cellSize + 25), (j * cellSize + 25, i * cellSize + 75), (j * cellSize + 75, i * cellSize + 75)))
if(val == 2):
pygame.draw.circle(backSurface, 'red', (j * cellSize + 50, i * cellSize + 50), 25)
def findPossibleMoves(selectedPiece): #Finds the possible moves for selected piece
row, column = selectedPiece[0], selectedPiece[1]
possibleMoves = findAllPossibleMoves("Player")
for piece, row, column in possibleMoves:
if piece == selectedPiece:
board[row][column] = 3
def drawPossibleMoves():
for i, row in enumerate(board):
for j, val in enumerate(row):
if(val == 3):
pygame.draw.circle(backSurface, 'gray', (j * cellSize + 50, i * cellSize + 50), 5)
def MoveSelectedPiece(selectedPiece, mouseX, mouseY):
row, column = selectedPiece
board[row][column] = 0
board[mouseY][mouseX] = 2
selectedPieceNewPos = mouseY, mouseX
playerSelectedPieceArr.append(selectedPieceNewPos)
for i, row in enumerate(board):
for j, val in enumerate(row):
if(val == 3):
board[i][j] = 0
def checkCapture(): #Checks all four directions of the pieces
deletedPieces = []
count = 0
i = 0
for x, row in enumerate(board):
for y, value in enumerate(row):
if value == 2: # Iterating for all the circles
newPositionColumn = y
newPositionRow = x
#Checking right of the piece
for i in range(newPositionColumn + 1, 8):
if i != 7:
if board[newPositionRow][i] == 2:
for j in range(newPositionColumn + 1, i):
if board[newPositionRow][j] == 1:
count += 1
break
else:
for j in range(newPositionColumn + 1, i):
if board[newPositionRow][j] == 1:
count += 1
if (i - newPositionColumn) - 1 == count:
for k in range(newPositionColumn + 1, i):
deletedPieces.append((newPositionRow, k, 1))
count = 0
#Checking left of the piece
for i in range(newPositionColumn - 1, -2, -1):
if i != -1:
if board[newPositionRow][i] == 2:
for j in range(newPositionColumn - 1, i, -1):
if board[newPositionRow][j] == 1:
count += 1
break
else:
for j in range(newPositionColumn - 1, i, -1):
if board[newPositionRow][j] == 1:
count += 1
if (newPositionColumn - i) - 1 == count:
for k in range(newPositionColumn - 1, i, -1):
deletedPieces.append((newPositionRow, k, 1))
count = 0
#Checking up of the piece
for i in range(newPositionRow - 1, -2, -1):
if i != -1:
if board[i][newPositionColumn] == 2:
for j in range(newPositionRow - 1, i, -1):
if board[j][newPositionColumn] == 1:
count += 1
break
else:
for j in range(newPositionRow - 1, i, -1):
if board[j][newPositionColumn] == 1:
count += 1
if (newPositionRow - i) - 1 == count:
for k in range(newPositionRow - 1, i, -1):
deletedPieces.append((k, newPositionColumn, 1))
count = 0
#Checking bottom of the piece
for i in range(newPositionRow + 1, 8):
if i != 7:
if board[i][newPositionColumn] == 2:
for j in range(newPositionRow + 1, i):
if board[j][newPositionColumn] == 1:
count += 1
break
else:
for j in range(newPositionRow + 1, i):
if board[j][newPositionColumn] == 1:
count += 1
if (i - newPositionRow) - 1 == count:
for k in range(newPositionRow + 1, i):
deletedPieces.append((k, newPositionColumn, 1))
count = 0
elif value == 1: # Iterating for all the triangles
newPositionColumn = y
newPositionRow = x
#Checking right of the piece
for i in range(newPositionColumn + 1, 8):
if i != 7:
if board[newPositionRow][i] == 1:
for j in range(newPositionColumn + 1, i):
if board[newPositionRow][j] == 2:
count += 1
break
else:
for j in range(newPositionColumn + 1, i):
if board[newPositionRow][j] == 2:
count += 1
if (i - newPositionColumn) - 1 == count:
for k in range(newPositionColumn + 1, i):
deletedPieces.append((newPositionRow, k, 2))
count = 0
#Checking left of the piece
for i in range(newPositionColumn - 1, -2, -1):
if i != -1:
if board[newPositionRow][i] == 1:
for j in range(newPositionColumn - 1, i, -1):
if board[newPositionRow][j] == 2:
count += 1
break
else:
for j in range(newPositionColumn - 1, i, -1):
if board[newPositionRow][j] == 2:
count += 1
if (newPositionColumn - i) - 1 == count:
for k in range(newPositionColumn - 1, i, -1):
deletedPieces.append((newPositionRow, k, 2))
count = 0
#Checking up of the piece
for i in range(newPositionRow - 1, -2, -1):
if i != -1:
if board[i][newPositionColumn] == 1:
for j in range(newPositionRow - 1, i, -1):
if board[j][newPositionColumn] == 2:
count += 1
break
else:
for j in range(newPositionRow - 1, i, -1):
if board[j][newPositionColumn] == 2:
count += 1
if (newPositionRow - i) - 1 == count:
for k in range(newPositionRow - 1, i, -1):
deletedPieces.append((k, newPositionColumn, 2))
count = 0
#Checking bottom of the piece
for i in range(newPositionRow + 1, 8):
if i != 7:
if board[i][newPositionColumn] == 1:
for j in range(newPositionRow + 1, i):
if board[j][newPositionColumn] == 2:
count += 1
break
else:
for j in range(newPositionRow + 1, i):
if board[j][newPositionColumn] == 2:
count += 1
if (i - newPositionRow) - 1 == count:
for k in range(newPositionRow + 1, i):
deletedPieces.append((k, newPositionColumn, 2))
count = 0
for row, column, deletedPiece in deletedPieces: #Deleting the captured pieces
board[row][column] = 0
return deletedPieces
def undoCapture(deletedPieces):
for row, column,deletedPiece in deletedPieces:
board[row][column] = deletedPiece
def findAllPossibleMoves(player):
possibleMoves = []
pieces = []
for i, row in enumerate(board):
for j, val in enumerate(row):
if val == 1 and player == "Computer":
pieces.append((i, j))
elif val == 2 and player == "Player":
pieces.append((i, j))
for piece in pieces:
row, column = piece
if 7 > row + 1 >= 0 and board[row+1][column] != 1 and board[row+1][column] != 2:
possibleMoves.append((piece, row+1, column))
if 0 <= row - 1 < 7 and board[row-1][column] != 1 and board[row-1][column] != 2:
possibleMoves.append((piece, row-1, column))
if 7 > column + 1 >= 0 and board[row][column+1] != 1 and board[row][column+1] != 2:
possibleMoves.append((piece, row, column+1))
if 0 <= column - 1 < 7 and board[row][column-1] != 1 and board[row][column-1] != 2:
possibleMoves.append((piece, row, column-1))
return possibleMoves
def makeMove(piece, move, player):
row, column = piece
targetRow, targetColumn = move
if player == "Computer":
board[targetRow][targetColumn] = 1
elif player == "Player":
board[targetRow][targetColumn] = 2
else:
print("Error")
board[row][column] = 0
return piece
def undoMove(piece, move, player):
row, column = piece
playedRow, playedColumn = move
if player == "Computer":
board[row][column] = 1
elif player == "Player":
board[row][column] = 2
board[playedRow][playedColumn] = 0
def checkWinner():
computerPieces = 0
playerPieces = 0
for row in board:
for val in row:
if val == 1:
computerPieces += 1
elif val == 2:
playerPieces += 1
if computerPieces == 0:
return "Player"
if playerPieces == 0:
return "Computer"
if turn >= 50:
if computerPieces == playerPieces:
return "Draw"
elif computerPieces > playerPieces:
return "Computer"
elif computerPieces < playerPieces:
return "Player"
return False
def evaluateBoard():
eval = 0
for i, row in enumerate(board):
for j, val in enumerate(row):
if i == 0:
if val == 1:
eval -= 1
elif val == 2:
eval += 1
if i == 6:
if val == 1:
eval -= 1
elif val == 2:
eval += 1
if j == 0:
if val == 1:
eval -= 1
elif val == 2:
eval += 1
if j == 6:
if val == 1:
eval -= 1
elif val == 2:
eval += 1
if val == 1:
eval += 3
elif val == 2:
eval -= 3
return eval
def minimax(depth, isMaximizingPlayer, alpha, beta, treeDepth = 0): # Maximizing player is ai
if depth == 0 or checkWinner() != False:
return evaluateBoard()
if isMaximizingPlayer:
maxEval = -math.inf
possibleMoves = findAllPossibleMoves("Computer")
for piece, row, column in possibleMoves:
makeMove(piece, (row,column), "Computer")
deletedPieces = checkCapture()
eval = minimax(depth - 1, False, alpha, beta, treeDepth + 1)
undoCapture(deletedPieces)
undoMove(piece, (row,column), "Computer")
if treeDepth == 0 and eval > maxEval:
maxEval = eval
global bestMove
bestMove = (piece, row, column)
elif eval > maxEval:
maxEval = eval
alpha = max(alpha, maxEval)
if beta <= alpha:
break
return maxEval
else:
minEval = math.inf
possibleMoves = findAllPossibleMoves("Player")
for piece, row, column in possibleMoves:
makeMove(piece, (row,column), "Player")
deletedPieces = checkCapture()
eval = minimax(depth - 1, True, alpha, beta, treeDepth + 1)
undoCapture(deletedPieces)
undoMove(piece, (row,column), "Player")
minEval = min(minEval, eval)
beta = min(minEval, beta)
if beta <= alpha:
break
return minEval
def computerMove():
global bestMove
start = time.process_time()
minimax(5, True, -math.inf, math.inf)
print("Time it took to calculate: ", time.process_time() - start)
if bestMove:
makeMove(bestMove[0], (bestMove[1], bestMove[2]), "Computer")
else:
print("No valid move")
bestMove = None
pygame.init()
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Test")
clock = pygame.time.Clock()
backSurface = pygame.Surface((width, height))
while turn <= 50:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
run = False
elif event.type == pygame.MOUSEBUTTONDOWN and turn != 0:
mouseX, mouseY = pygame.mouse.get_pos()
mouseX = mouseX // 100
mouseY = mouseY // 100
if board[mouseY][mouseX] == 2:
if len(playerSelectedPieceArr) != 0:
for piece in playerSelectedPieceArr:
if piece != (mouseY, mouseX):
selectedPiece = mouseY, mouseX
else:
selectedPiece = mouseY, mouseX
for i, row in enumerate(board):
for j, val in enumerate(row):
if(val == 3):
board[i][j] = 0
elif board[mouseY][mouseX] == 3:
MoveSelectedPiece(selectedPiece, mouseX, mouseY)
checkCapture()
turn += 1
turnToPlay = "Computer"
playerSelectedPieceArr.clear()
if turnToPlay == "Computer":
computerMove()
checkCapture()
turn += 1
computerTurnCounter = 0
turnToPlay = "Player"
selectedPiece = None
elif event.type == pygame.MOUSEBUTTONDOWN and turn == 0:
computerMove()
checkCapture()
turn += 1
turnToPlay = "Player"
backSurface.fill('white')
drawBoard()
drawPieces()
if selectedPiece:
findPossibleMoves(selectedPiece)
drawPossibleMoves()
screen.blit(backSurface, (0,0))
pygame.display.update()
print("Game over ", turn)
winner = checkWinner()
if winner != "Draw":
print("Winner of the game is", winner)
else:
print("Draw")