-
Notifications
You must be signed in to change notification settings - Fork 0
/
botV2.py
231 lines (209 loc) · 8.96 KB
/
botV2.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
import board as bd
import math
import copy
evalCalls = 0
def tuned_minimax(bstr, depth, alpha, beta, turn):
tempBoard = bd.Board(bstr)
originalBoard = bstr
optimalMove = ''
optD = ''
if tempBoard.endGame(turn):
return tempBoard.utility(turn), optimalMove
if depth == 0:
return evaluate(bstr), optimalMove
if turn == 1:
maxEval = -99
for i in range(8):
for j in range(8):
for d in ['L', 'R', '-L', '-R']:
if tempBoard.moveAllowed(i, j, d, 1):
tempBoard.move(i, j, d, 1)
eval, _ = tuned_minimax(tempBoard.getString(), depth - 1, alpha, beta, -1)
if eval > maxEval:
maxEval = eval
optimalMove = tempBoard.getString()
optD = str(i) + ' ' + str(j) + ' ' + d
tempBoard.editBoard(originalBoard)
alpha = max(alpha, eval)
if beta <= alpha:
break
return maxEval, optimalMove
else:
minEval = 99
for i in range(8):
for j in range(8):
for d in ['L', 'R', '-L', '-R']:
if tempBoard.moveAllowed(i, j, d, -1):
tempBoard.move(i, j, d, -1)
eval, _ = tuned_minimax(tempBoard.getString(), depth - 1, alpha, beta, 1)
if eval < minEval:
minEval = eval
optimalMove = tempBoard.getString()
tempBoard.editBoard(originalBoard)
beta = min(beta, eval)
if beta <= alpha:
break
return minEval, optimalMove
def stillInBoard(i, j):
return i >= 0 and i < 8 and j >= 0 and j < 8
def evaluate(string):
global evalCalls
evalCalls += 1
board = bd.Board(string)
if board.endGame(1):
return -1
if board.endGame(-1):
return 1
# Basic evaluation function base on the number of pieces
scoreW = 0.000
scoreB = 0.000
for i in range(8):
for j in range(8):
if board.board[i][j] > 0:
scoreW += board.board[i][j]
elif board.board[i][j] < 0:
scoreB += -board.board[i][j]
pieceW = scoreW
pieceB = scoreB
"""
# Heatmap evaluation function
# Center heatmap
centerW = 0.5
promoteLineW = 0.3
for i in [3, 4]:
for j in [2, 3, 4, 5]:
if board.board[i][j] > 0:
scoreW += board.board[i][j]*centerW
elif board.board[i][j] < 0:
scoreB += -board.board[i][j]*centerW
# Unpromoted pieces heatmap
for j in range(8):
if board.board[7][j] == 1:
scoreW += board.board[0][j]*promoteLineW
if board.board[0][j] == -1:
scoreB += -board.board[7][j]*promoteLineW
"""
# Try to trade pieces when ahead.
if pieceW > pieceB:
tradeW = max(0.3*(pieceW+pieceB-20),0)
for i in range(8):
for j in range(8):
if board.board[i][j] < 0:
if stillInBoard(i+1, j+1):
if board.board[i+1][j+1] > 0:
scoreW += board.board[i][j]*tradeW
if stillInBoard(i+1, j-1):
if board.board[i+1][j-1] > 0:
scoreW += board.board[i][j]*tradeW
if stillInBoard(i-1, j+1):
if board.board[i-1][j+1] > 0:
scoreW += board.board[i][j]*tradeW
if stillInBoard(i-1, j-1):
if board.board[i-1][j-1] > 0:
scoreW += board.board[i][j]*tradeW
if stillInBoard(i, j+2):
if board.board[i][j+2] > 0:
scoreW += board.board[i][j]*tradeW*0.5
if stillInBoard(i, j-2):
if board.board[i][j-2] > 0:
scoreW += board.board[i][j]*tradeW*0.5
if stillInBoard(i+2, j):
if board.board[i+2][j] > 0:
scoreW += board.board[i][j]*tradeW*0.5
if stillInBoard(i-2, j):
if board.board[i-2][j] > 0:
scoreW += board.board[i][j]*tradeW*0.5
if stillInBoard(i+2, j+2):
if board.board[i+2][j+2] > 0:
scoreW += board.board[i][j]*tradeW*0.5
if stillInBoard(i+2, j-2):
if board.board[i+2][j-2] > 0:
scoreW += board.board[i][j]*tradeW*0.5
if stillInBoard(i-2, j+2):
if board.board[i-2][j+2] > 0:
scoreW += board.board[i][j]*tradeW*0.5
if stillInBoard(i-2, j-2):
if board.board[i-2][j-2] > 0:
scoreW += board.board[i][j]*tradeW*0.5
elif pieceW < pieceB:
tradeB = max(0.3*(pieceB+pieceW-20),0)
for i in range(8):
for j in range(8):
if board.board[i][j] > 0:
if stillInBoard(i+1, j+1):
if board.board[i+1][j+1] < 0:
scoreB += -board.board[i][j]*tradeB
if stillInBoard(i+1, j-1):
if board.board[i+1][j-1] < 0:
scoreB += -board.board[i][j]*tradeB
if stillInBoard(i-1, j+1):
if board.board[i-1][j+1] < 0:
scoreB += -board.board[i][j]*tradeB
if stillInBoard(i-1, j-1):
if board.board[i-1][j-1] < 0:
scoreB += -board.board[i][j]*tradeB
if stillInBoard(i, j+2):
if board.board[i][j+2] < 0:
scoreB += -board.board[i][j]*tradeB*0.5
if stillInBoard(i, j-2):
if board.board[i][j-2] < 0:
scoreB += -board.board[i][j]*tradeB*0.5
if stillInBoard(i+2, j):
if board.board[i+2][j] < 0:
scoreB += -board.board[i][j]*tradeB*0.5
if stillInBoard(i-2, j):
if board.board[i-2][j] < 0:
scoreB += -board.board[i][j]*tradeB*0.5
if stillInBoard(i+2, j+2):
if board.board[i+2][j+2] < 0:
scoreB += -board.board[i][j]*tradeB*0.5
if stillInBoard(i+2, j-2):
if board.board[i+2][j-2] < 0:
scoreB += -board.board[i][j]*tradeB*0.5
if stillInBoard(i-2, j+2):
if board.board[i-2][j+2] < 0:
scoreB += -board.board[i][j]*tradeB*0.5
if stillInBoard(i-2, j-2):
if board.board[i-2][j-2] < 0:
scoreB += -board.board[i][j]*tradeB*0.5
"""
# Avoid piece islands in openning
islandW = 1
islandB = 1
openConsideration = 24
if pieceW + pieceB > openConsideration:
for i in range(8):
if not any(board.board[i][j] > 0 for j in range(8)):
scoreW -= islandW*((pieceW + pieceB)-openConsideration)/(32-openConsideration)
if not any(board.board[i][j] < 0 for j in range(8)):
scoreB -= islandB*((pieceW + pieceB)-openConsideration)/(32-openConsideration)
"""
return round(2*((scoreW) / (scoreB + scoreW)) - 1, 5)
def botPlay(bstr = 'A', difficulty=5, turn=1, moves=0, constantDepth = False):
if constantDepth:
depth = difficulty
else:
num_pieces = 64 - bstr.count('0')
endGameWeigth = 0.025
moveWeigth = 0.1
depth = math.floor(difficulty / (endGameWeigth * num_pieces + 0.4) + max(moveWeigth*(moves - 50), 0))
print ('Depth (bp): ', depth)
"""
with open('dict6.txt', 'r') as file:
# Each line will have 3 values: str, mstr, eval
for line in file:
if bstr == line.split()[0]:
eval = line.split()[2]
print ('Evaluation: ', eval)
return line.split()[1]
"""
eval, mstr = tuned_minimax(bstr, depth, -math.inf, math.inf, turn)
# Add str, msr, eval to the file
# Turn evaluation to string
with open('dict6.txt', 'a') as file:
file.write(bstr + ',' + mstr + ',' + str(eval) + '\n')
print ('Evaluation: ', eval)
global evalCalls
print ('Number of calculated positions: ', evalCalls)
evalCalls = 0
return mstr