-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
46 lines (43 loc) · 1.86 KB
/
engine.py
File metadata and controls
46 lines (43 loc) · 1.86 KB
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
import chess.pgn
import chess
import chess.engine
from style import aggressive, defensive, positional, tactical
class ChessEngine:
def __init__(self, difficulty=1500):
self.difficulty = difficulty
self.engine = chess.engine.SimpleEngine.popen_uci([r".\lc0\lc0.exe", "-w", f"./lc0/maia-{difficulty}.pb.gz"])
def play(self, board, style):
if style == 'basic':
return self.engine.play(board, chess.engine.Limit(depth=5)).move
options = self.engine.analyse(board, chess.engine.Limit(depth=5), multipv=5, info=chess.engine.INFO_PV)
#print("Options:", options)
moveset = [move['pv'][0] for move in options if 'pv' in move]
scores = []
curr = self.engine.analyse(board, chess.engine.Limit(depth=5), info=chess.engine.INFO_SCORE)
cp = curr['score'].relative.score()
if cp is None:
cp = 0
print("No score found, defaulting to 0")
print("Moveset:", moveset)
#print("Moveset:", moveset)
for move in moveset:
if move == 'a1a1':
print("Invalid move detected, skipping")
continue
board.push(move)
analysis = self.engine.analyse(board, chess.engine.Limit(depth=5), info=chess.engine.INFO_SCORE)
scores.append((move, analysis['score'].relative.score() or cp))
board.pop()
#print("Scores:", scores)
if style == 'aggressive':
return aggressive(scores, board)
elif style == 'defensive':
return defensive(scores, board)
elif style == 'positional':
return positional(scores, board)
elif style == 'tactical':
return tactical(scores, board)
return self.engine.play(board, chess.engine.Limit(depth=5)).move
def close(self):
self.engine.quit()
self.engine.close()