-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardMiniMax.py
98 lines (71 loc) · 3.49 KB
/
boardMiniMax.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
import board
import utils
import constants as const
class BoardMiniMax (board.Board):
def __init__(self, player ,x = 0, y = 0, matrix = [], parent=None, successors = []):
super().__init__(player, x, y, matrix, parent, successors)
def addMove(self, x, y): # returneaza un nou board cu mutarea adaugata
return self.genSuccessor(x, y)
def genSuccessor(self, x, y):
if self.successors[x * const.COLS + y] is not None:
return self.successors[x * const.COLS + y]
newBoard = BoardMiniMax(self.nextPlayer(), x, y, self.matrix, self, successors=[])
newBoard.matrix[x][y] = self.player
newBoard.nrFullCells = self.nrFullCells + 1
self.successors[x* const.COLS + y] = newBoard
return newBoard
def genSuccessors(self):
if len([1 for i in self.successors if i is not None]) != 0:
# print(self.x, self.y, len([1 for i in self.successors if i is not None]))
return self.successors
if self.isFull():
return [None for _ in range(const.ROWS * const.COLS)]
if self.winner() != None:
return [None for _ in range(const.ROWS * const.COLS)]
for i in range(const.ROWS):
for j in range(const.COLS):
if self.canAddMove(i, j) and self.hasNeighbor(i, j):
# print("In GenSuccessors: " + str(i) + " " + str(j))
newBoard = BoardMiniMax(self.nextPlayer(), i, j, self.matrix, self, successors=[])
newBoard.matrix[i][j] = self.player
newBoard.nrFullCells = self.nrFullCells + 1
self.successors[i* const.COLS + j] = newBoard
return self.successors
def AIMakeMove(self):
_, bestMove = self.minimax(const.DEPTH, True)
newBoard = self.successors[bestMove[0] * const.COLS + bestMove[1]]
return newBoard
def minimax(self, depth, maximizingPlayer, alpha = float('-inf'), beta = float('inf')):
successors = self.genSuccessors()
# child = self.genNextSuccessor()
if depth == 0 or len([1 for i in successors if i is not None]) == 0:
score = utils.transTable.lookup(hash(self))
if score is None:
score = self.score()
utils.transTable.set(hash(self), score)
return score, None
if maximizingPlayer:
value = float('-inf')
for child in successors:
if child is None:
continue
eval, _ = child.minimax(depth - 1, False, alpha, beta)
if eval > value:
value = eval
bestMove = (child.x, child.y)
alpha = max(alpha, value)
if beta <= alpha:
break
else:
value = float('inf')
for child in successors:
if child is None:
continue
eval, _ = child.minimax(depth - 1, True, alpha, beta)
if eval < value:
value = eval
bestMove = (child.x, child.y)
beta = min(beta, value)
if beta <= alpha:
break
return value, bestMove