-
Notifications
You must be signed in to change notification settings - Fork 0
/
engineCommunication.py
143 lines (138 loc) · 6.71 KB
/
engineCommunication.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
import chess
from time import time
from math import inf, isnan
import chess.polyglot
from protochessOOP import Engine
# Calculates how many moves before computer sees mate
def mateInXMoves(board, score):
# uses the move score to calculate the depth to mate
half_moves_to_mate = (-(abs(score) - 99999) // (999.99))
if not isnan(half_moves_to_mate):
moves_to_mate = int((half_moves_to_mate - 1) // 2) + 1
if (board.turn and score > 0) or (not board.turn and score < 0):
sideMated = 'white'
else:
sideMated = 'black'
score = 'Mate in {} for {}'.format(str(moves_to_mate), sideMated)
return score
# Allows engines of differing depths to play themselves
def playEngines(engine1Depth, engine2Depth, fen='', printBoard=True):
if fen:
board = chess.Board(fen)
else:
board = chess.Board()
whiteEngine = Engine(engine1Depth, 1, board)
blackEngine = Engine(engine2Depth, -1, board)
movesToPlay = int(input('# of moves for each computer to play (type 0 for continuous play): ')) * 2
if printBoard:
print(board)
while (not board.is_game_over()) and (movesToPlay > len(board.move_stack) or movesToPlay == 0):
if board.turn:
computerMove, value, book, timeTaken, positions, positionsPerSecond = whiteEngine.moveWithStats()
else:
computerMove, value, book, timeTaken, positions, positionsPerSecond = blackEngine.moveWithStats()
if abs(value) > 50000:
value = mateInXMoves(board, value)
print('Best Move is: {} \nPositon advantage is calculated as {} from {} positions in {} seconds ({} pos/s)'.format(computerMove, value, positions, timeTaken, positionsPerSecond))
else:
if not book:
print('Best Move is: {}\nPosition advantage is calclulated as: {} for {} from {} positions in {} seconds ({} pos/s)'.format(computerMove, value, 'white' if board.turn else 'black', positions, timeTaken, positionsPerSecond))
else:
print('Best Move is: {} (Book Move)'.format(computerMove))
board.push(computerMove)
whiteEngine.updateBoard(board)
blackEngine.updateBoard(board)
if printBoard:
print(board)
if board.is_game_over():
print('Game Over! Result: {}'.format(board.result()))
else:
print('Finished {} half moves. program terminated'.format(len(board.move_stack)))
def playAgainstEngine(engineDepth, fen='', printBoard=True):
if fen:
board = chess.Board(fen)
else:
board = chess.Board()
playerColor = input('What color would you like to play as? (w)hite or (b)lack? ')
if playerColor.lower()[0] == 'w':
playerColor = 1
else:
playerColor = -1
computerEngine = Engine(engineDepth, -playerColor, board)
while not board.is_game_over():
if board.turn == (playerColor == 1):
user_input = input('Make Move (or type e to export the FEN of the position): ')
if user_input.lower() == 'e':
print('Board FEN: {}'.format(board.fen()))
try:
board.push_uci(user_input)
print('Player move: {}'.format(user_input))
if printBoard:
print(board)
computerEngine.updateBoard(board)
except ValueError:
continue
else:
computerMove, value, book, timeTaken, positions, positionsPerSecond = computerEngine.moveWithStats()
if abs(value) > 50000:
value = mateInXMoves(board, value)
print('Best Move is: {} \nPositon advantage is calculated as {} from {} positions in {} seconds ({} pos/s)'.format(computerMove, value, positions, timeTaken, positionsPerSecond))
else:
if not book:
print('Best Move is: {}\nPosition advantage is calclulated as: {} for {} from {} positions in {} seconds ({} pos/s)'.format(computerMove, value, 'white' if board.turn else 'black', positions, timeTaken, positionsPerSecond))
else:
print('Best Move is: {} (Book Move)'.format(computerMove))
board.push(computerMove)
computerEngine.updateBoard(board)
if printBoard:
print(board)
print('Game Over! Result: {}'.format(board.result()))
def analyzeWithEngine(engineDepth, fen=''):
if fen:
board = chess.Board(fen)
else:
board = chess.Board()
engineColor = 1 if board.turn else -1
analysisEngine = Engine(engineDepth, engineColor, board)
computerMove, value, book, timeTaken, positions, positionsPerSecond = analysisEngine.moveWithStats()
if abs(value) > 50000:
value = mateInXMoves(board, value)
print('Best Move is: {} \nPositon advantage is calculated as {} from {} positions in {} seconds ({} pos/s)'.format(computerMove, value, positions, timeTaken, positionsPerSecond))
else:
if not book:
print('Best Move is: {}\nPosition advantage is calclulated as: {} for {} from {} positions in {} seconds ({} pos/s)'.format(computerMove, value, 'white' if board.turn else 'black', positions, timeTaken, positionsPerSecond))
else:
print('Best Move is: {} (Book Move)'.format(computerMove))
def extendedEnginePlay(engine1Depth, engine2Depth, timesToPlay=10):
engine1Wins, engine2Wins, draws = 0, 0, 0
for x in range(timesToPlay):
board = chess.Board()
if x % 2 == 0:
whiteEngine = Engine(engine1Depth, 1, board)
blackEngine = Engine(engine2Depth, -1, board)
else:
whiteEngine = Engine(engine2Depth, 1, board)
blackEngine = Engine(engine1Depth, -1, board)
while not board.is_game_over():
if board.turn:
bestMove, value, book = whiteEngine.move()
else:
bestMove, value, book = blackEngine.move()
board.push(bestMove)
whiteEngine.updateBoard(board)
blackEngine.updateBoard(board)
gameResult = board.result()
if (gameResult == '1-0' and x % 2 == 0) or (gameResult == '0-1' and x % 2 != 0):
engine1Wins += 1
result = 'Engine 1 won!'
elif (gameResult == '1-0' and x % 2 != 0) or (gameResult == '0-1' and x % 2 == 0):
engine2Wins += 1
result = 'Engine 2 won!'
else:
draws += 1
result = 'Draw!'
print('Move List: \n')
print(', '.join([x.uci() for x in board.move_stack]))
print()
print('Game Over! Result: {} ({})'.format(board.result(), result))
print('Engine 1 record: {}-{}-{}, Engine 2 record: {}-{}-{}'.format(engine1Wins, engine2Wins, draws, engine2Wins, engine1Wins, draws))