-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchessguess.py
295 lines (259 loc) · 11.5 KB
/
chessguess.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
import sys
from subprocess import call
import chess
import chess.engine
import chess.pgn
import itertools
import math
# arg1:val1 arg2:val2
def parseArguments(args):
arguments = {}
for index in range(0, len(args)):
tupleArgs = args[index].split(":")
if len(tupleArgs) == 2:
arguments[tupleArgs[0]] = tupleArgs[1]
return arguments
def parseWinner(game):
if game.headers["Result"] == "1-0":
return chess.WHITE
elif game.headers["Result"] == "0-1":
return chess.BLACK
else:
return None
def queuePrint(str):
printQueue.append(str)
def printHeader(game):
queuePrint("―――――――――――――――――――――――――――――")
if game.headers["Event"] != None and game.headers["Event"] != "":
queuePrint(
(game.headers["Event"][:26] + "...")
if len(game.headers["Event"]) > 26
else game.headers["Event"]
)
if game.headers["Site"] != None and game.headers["Site"] != "":
queuePrint(
(game.headers["Site"][:26] + "...")
if len(game.headers["Site"]) > 26
else game.headers["Site"]
)
if game.headers["Round"] != None and game.headers["Round"] != "":
queuePrint(
"Round: " + (game.headers["Round"][:19] + "...")
if len(game.headers["Round"]) > 19
else "Round: " + game.headers["Round"]
)
if game.headers["White"] != None and game.headers["White"] != "":
queuePrint(
("W: " + game.headers["White"][:23] + "...")
if len(game.headers["White"]) > 23
else ("W: " + game.headers["White"])
)
if game.headers["Black"] != None and game.headers["Black"] != "":
queuePrint(
("B: " + game.headers["Black"][:23] + "...")
if len(game.headers["Black"]) > 23
else ("B: " + game.headers["Black"])
)
if game.headers["Date"] != None and game.headers["Date"] != "":
queuePrint(
("Date: " + game.headers["Date"][:20] + "...")
if len(game.headers["Date"]) > 20
else ("Date: " + game.headers["Date"])
)
if game.headers["Result"] != None and game.headers["Result"] != "":
queuePrint("Result: " + game.headers["Result"])
if game.headers["ECO"] != None and game.headers["ECO"] != "":
queuePrint("ECO: " + game.headers["ECO"])
queuePrint("Winner Points: totalHeroPoints")
queuePrint("―――――――――――――――――――――――――――――")
queuePrint("")
def getMovePrefix(cycleNum):
if cycleNum % 2 != 0:
return str(int(cycleNum / 2 + 0.5)) + "..."
else:
return str(int(cycleNum / 2 + 1)) + ". "
def getPointValues(info, heroMove, board, winner):
pointDict = {}
# Taking first pass through engine scorings to compose and extract scores
for i in range(0, len(info)):
engineMove = board.san(info[i]["pv"][0])
score = None
if info[i]["score"].relative.score() != None:
score = (
info[i]["score"].relative.score() / 100.0
if info[i]["score"].turn
else info[i]["score"].relative.score() / -100.0
)
else:
score = (
"#" + str(info[i]["score"].relative.mate())
if info[i]["score"].turn
else "#" + str(info[i]["score"].relative.mate() * -1)
)
score = (
(1000 - float(score[1:]))
if winner == chess.WHITE
else (-1000 - float(score[1:]))
)
pointDict[i] = {"score": score, "points": None, "engineMove": engineMove}
# Making second pass through engine scorings, only using the iterator as a mechanism to backfill missing point assignments in pointDict
for i in range(0, len(info)):
if pointDict[i]["points"] == None:
if i == 0:
pointDict[i]["points"] = 3
else:
scoreDiff = pointDict[0]["score"] - pointDict[i]["score"]
if winner == chess.BLACK:
if scoreDiff >= -0.25:
pointDict[i]["points"] = 3
elif scoreDiff >= -0.75:
pointDict[i]["points"] = 2
elif scoreDiff >= -1.25:
pointDict[i]["points"] = 1
else:
pointDict[i]["points"] = 0
else:
if scoreDiff <= 0.25:
pointDict[i]["points"] = 3
elif scoreDiff <= 0.75:
pointDict[i]["points"] = 2
elif scoreDiff <= 1.25:
pointDict[i]["points"] = 1
else:
pointDict[i]["points"] = 0
# Making third and final pass through engine scorings, with the sole purpose of updating our global tally of how the hero performed
global totalHeroPoints
for i in range(0, len(info)):
if pointDict[i]["engineMove"] == heroMove:
totalHeroPoints += pointDict[i]["points"]
return pointDict
def main():
# Start chess engine process (optionally from local install such as: /usr/local/bin/stockfish)
engine = chess.engine.SimpleEngine.popen_uci("./bin/stockfish-13")
engine.configure({"Threads": 2, "Hash": 4096})
pgn = open(scriptArguments["pgn"])
game = chess.pgn.read_game(pgn)
board = game.board()
winner = parseWinner(game)
cycleNum = 0
mainLineMoves = []
for move in game.mainline_moves():
mainLineMoves.append(move)
if winner != None:
printHeader(game)
for move in mainLineMoves:
board.push(move)
if not board.is_game_over():
if (
(winner == chess.WHITE and cycleNum == 0)
or (winner == chess.WHITE and cycleNum % 2 != 0)
or (winner == chess.BLACK and cycleNum % 2 == 0)
):
# If it's whites' first move for a white winner game
if winner == chess.WHITE and cycleNum == 0:
queuePrint(
getMovePrefix(cycleNum) + " " + board.san(board.pop())
)
# Else If it's whites' turn for a black winner game
elif winner == chess.BLACK and cycleNum % 2 == 0:
if cycleNum > 0:
queuePrint("")
if (
cycleNum % diagramEveryXMoves == 0
): # Controls how frequently diagrams appear
board.pop()
queuePrint("|" + board.fen())
queuePrint("")
board.push(move)
queuePrint(
getMovePrefix(cycleNum) + " " + board.san(board.pop())
)
board.push(move)
if cycleNum < len(mainLineMoves) - 1:
queuePrint(
getMovePrefix(cycleNum + 1)
+ board.san(mainLineMoves[cycleNum + 1])
)
# Else If it's blacks' turn for a white winner game
elif winner == chess.WHITE and cycleNum % 2 != 0:
queuePrint(getMovePrefix(cycleNum) + board.san(board.pop()))
board.push(move)
if cycleNum < len(mainLineMoves) - 1:
queuePrint("")
if (
cycleNum + 1
) % diagramEveryXMoves == 0: # Controls how frequently diagrams appear
queuePrint("|" + board.fen())
queuePrint("")
queuePrint(
getMovePrefix(cycleNum + 1)
+ " "
+ board.san(mainLineMoves[cycleNum + 1])
)
# If we are past the opening cycleNum plys, it's time to start analyzing top X moves from here onward
if (cycleNum > 8 and winner == chess.WHITE) or (
cycleNum > 9 and winner == chess.BLACK
):
# Analyze the board for the next best move
info = engine.analyse(
board, chess.engine.Limit(depth=24), multipv=analyzeXMoves
)
if cycleNum < len(mainLineMoves):
pointDict = getPointValues(
info,
board.san(mainLineMoves[cycleNum + 1]),
board,
winner,
)
for i in range(0, len(info)):
if info[i]["score"].relative.score() != None:
score = (
info[i]["score"].relative.score() / 100.0
if info[i]["score"].turn
else info[i]["score"].relative.score() / -100.0
)
else:
score = (
"#" + str(info[i]["score"].relative.mate())
if info[i]["score"].turn
else "#"
+ str(info[i]["score"].relative.mate() * -1)
)
trimmedVariations = itertools.islice(
info[i]["pv"], 1
) # Increase 1 if you want to show more moves beyond candidate move.
move = (
board.variation_san(trimmedVariations)
if cycleNum % 2 == 0
else board.variation_san(trimmedVariations).replace(
" ", " "
)
)
queuePrint(
"{:―<12s}▸ {:<9s} {:<5s}".format(
move + " ",
"(" + str(score) + ")",
"[" + str(pointDict[i]["points"]) + "]",
)
)
if winner == chess.WHITE and cycleNum == 0:
board.push(move)
cycleNum += 1
queuePrint("")
# Shutdown engine
engine.quit()
# Update printQueue with totalHeroPoints and then print results
for i in range(0, len(printQueue)):
if "totalHeroPoints" in printQueue[i]:
printQueue[i] = printQueue[i].replace(
"totalHeroPoints", str(totalHeroPoints)
)
print(printQueue[i])
# Globals
scriptArguments = parseArguments(sys.argv)
printQueue = []
totalHeroPoints = 0
diagramEveryXMoves = 2
analyzeXMoves = 5
# Start program
main()