-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMaxStrategy.py
158 lines (141 loc) · 5.55 KB
/
MinMaxStrategy.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
from AbstractStrategy import AbstractStrategy
from Config import DEPTH, DIMENSION, CROSS, CIRCLE, EMPTY
from Coordinates import Coordinates
class MinMaxStrategy(AbstractStrategy):
def __init__(self, mapMatrixP):
super().__init__(mapMatrixP)
self.movesDone = 0
self.nodesCheck = 0
def countMovesDone(self):
moves = 0
for i in range(DIMENSION):
for j in range(DIMENSION):
if self.mapMatrix.matrix[i][j] is not EMPTY:
moves += 1
return moves
def doBestMove(self, currentMapMatrix, turn='ai'):
self.mapMatrix = currentMapMatrix
self.movesDone = self.countMovesDone()
with open(f'TreeViews/moveTree{self.movesDone}.txt', 'w') as file:
bestmove, bestscore = self.minMaxPrintNodes(DEPTH - self.movesDone, "", file, -float('Inf'), float('Inf'), turn)
self.nodesCheck = 0
temp, temp1 = self.minMax(DEPTH - self.movesDone)
self.nodesCheck = 0
self.mapMatrix.markCircleOn(bestmove)
return bestmove
def calcPossibleMoves(self):
possibleMoves = []
if self.hasWon(CIRCLE) or self.hasWon(CROSS):
return possibleMoves
for i in range(DIMENSION):
for j in range(DIMENSION):
if self.mapMatrix.matrix[i][j] == EMPTY:
temp = Coordinates(i, j)
possibleMoves.append(temp)
return possibleMoves
def minMax(self, ply, turn='ai'):
possibleMoves = self.calcPossibleMoves()
bestMove = None
bestScore = None
if turn == 'ai':
bestScore = float('Inf')
elif turn == 'player':
bestScore = -float('Inf')
if ply == 0 or not possibleMoves:
bestScore = self.evaluate()
else:
for move in possibleMoves:
self.mapMatrix.markOn(move, turn)
self.nodesCheck += 1
if turn == 'ai':
temp, score = self.minMax(ply - 1, 'player')
if bestScore is None or score < bestScore:
bestScore = score
bestMove = move
elif turn == 'player':
temp, score = self.minMax(ply - 1, 'ai')
if bestScore is None or score > bestScore:
bestScore = score
bestMove = move
self.mapMatrix.markAsEmpty(move)
return bestMove, bestScore
def minMaxPrintNodes(self, ply, indent, fileToWrite, alpha, beta, turn='ai'):
indent = indent + "++"
possibleMoves = self.calcPossibleMoves()
bestMove = None
bestScore = None
if turn == 'ai':
bestScore = float('Inf')
elif turn == 'player':
bestScore = -float('Inf')
if ply == 0 or not possibleMoves:
bestScore = self.evaluate()
else:
for move in possibleMoves:
if beta <= alpha:
fileToWrite.write('UCINAM\n')
break
self.mapMatrix.markOn(move, turn)
self.nodesCheck += 1
if turn == 'ai':
temp, score = self.minMaxPrintNodes(ply - 1, indent, fileToWrite, alpha, beta, 'player')
fileToWrite.write(f'{indent}{score}A\n')
if bestScore is None or score < bestScore:
bestScore = score
bestMove = move
beta = min(beta, bestScore)
elif turn == 'player':
temp, score = self.minMaxPrintNodes(ply - 1, indent, fileToWrite, alpha, beta, 'ai')
fileToWrite.write(f'{indent}{score}P\n')
if bestScore is None or score > bestScore:
bestScore = score
bestMove = move
alpha = max(alpha, bestScore)
self.mapMatrix.markAsEmpty(move)
return bestMove, bestScore
def evaluate(self):
score = 0
score += self.evaluateLine(0, 0, 0, 1, 0, 2)
score += self.evaluateLine(1, 0, 1, 1, 1, 2)
score += self.evaluateLine(2, 0, 2, 1, 2, 2)
score += self.evaluateLine(0, 0, 1, 0, 2, 0)
score += self.evaluateLine(0, 1, 1, 1, 2, 1)
score += self.evaluateLine(0, 2, 1, 2, 2, 2)
score += self.evaluateLine(0, 0, 1, 1, 2, 2)
score += self.evaluateLine(0, 2, 1, 1, 2, 0)
return score
def evaluateLine(self, row1, cel1, row2, cel2, row3, cel3):
score = 0
if self.mapMatrix.matrix[row1][cel1] == CROSS:
score = 1
elif self.mapMatrix.matrix[row1][cel1] == CIRCLE:
score = -1
if self.mapMatrix.matrix[row2][cel2] == CROSS:
if score > 0:
score *= 10
elif score == 0:
score = 1
else:
return 0
elif self.mapMatrix.matrix[row2][cel2] == CIRCLE:
if score < 0:
score *= 10
elif score == 0:
score = -1
else:
return 0
if self.mapMatrix.matrix[row3][cel3] == CROSS:
if score > 0:
score *= 10
elif score == 0:
score = 1
else:
return 0
elif self.mapMatrix.matrix[row3][cel3] == CIRCLE:
if score < 0:
score *= 10
elif score == 0:
score = -1
else:
return 0
return score