-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessbrain.py
More file actions
228 lines (184 loc) · 9.7 KB
/
Copy pathchessbrain.py
File metadata and controls
228 lines (184 loc) · 9.7 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
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
import os
import chess
import numpy
import time
import sys
# This is the way we evaluate the board, If the addition of all piecs on the board with these values is negative black is winning, vise versa
piece_values = {'P': 10, 'N': 35, 'B': 35, 'R': 52.5, 'Q': 100, 'K': 1000, 'p': -10, 'n': -35, 'b': -35, 'r': -52.5, 'q': -100, 'k': -1000}
searched = 0
timeCalcPos = 0.0
known_piece_map_list = []
# Found this great stackoverflow question with position evaluations for each piece along with the piece_values, which I editted
# a bit according to https://en.wikipedia.org/wiki/Chess_piece_relative_value
# here is the stackoverflow question: https://stackoverflow.com/questions/59039152/python-chess-minimax-algorithm-how-to-play-with-black-pieces-bot-has-white
infinity = 99999999
position_values = {
'P' : numpy.array([ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
[1.0, 1.0, 2.0, 3.0, 3.0,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 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[ 0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0]]),
'Q' : numpy.array([[-2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0],
[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
[-1.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0],
[-0.5, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5],
[-0.5, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5],
[-1.0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0],
[-1.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, -1.0],
[-2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0]]),
'K' : numpy.array([[ -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[ -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[ -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[ -3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[ -2.0, -3.0, -3.0, -4.0, -4.0, -3.0, -3.0, -2.0],
[ -1.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -1.0],
[ 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0 ],
[ 2.0, 3.0, 1.0, 0.0, 0.0, 1.0, 3.0, 2.0 ]])}
flipped_pos_vales = {}
# this function was also obtained from the same stackoverflow question
def calculatePos(board, piece_values=piece_values, position_values = position_values, infinity=infinity):
global timeCalcPos
# 1/7th of time it takes to generate a move is this board.piece_map() function
pieces = board.piece_map()
eval = 0
for i in pieces:
file = chess.square_file(i)
rank = chess.square_rank(i)
piece_type = str(pieces[i])
if piece_type.isupper():
tempArr = flipped_pos_vales[piece_type.upper()]
eval += piece_values[piece_type] + tempArr[rank, file]
else:
positionArray = position_values[piece_type.upper()]
eval += piece_values[piece_type] - positionArray[rank, file]
return eval
## Curently scrapped idea, not really optimised for alpha beta pruning and minimax search. Keeping it in here just in case I ever come back to it.
def GetMoveWithThreading(board, depth, initialDepth, player, useAlphaBeta, color, alpha, beta):
threads = []
move_list = list(board.generate_legal_moves())
for i in move_list:
# Generate new instance of board
temp = chess.Board(board.fen())
temp.push_san(str(i))
# thread = threading.Thread(target=lambda q, arg1: q.put(getMove(arg1)), args=(que, temp, depth - 1, initialDepth, tempPlayer, useAlphaBeta, color, alpha, beta))
thread = ThreadWithResult(target=getMove, args=(temp, depth - 1, initialDepth, not player, useAlphaBeta, color, alpha, beta, True, ))
print("a")
thread.start()
threads.append([thread, str(i)])
for i in range(len(threads)):
threads[i][0].join()
best_move = threads[0][1]
best_move_numerical = threads[0][0].result
for index, tuple in enumerate(threads):
if tuple[0].result > best_move_numerical:
best_move_numerical = tuple[0].result
best_move = tuple[1]
return best_move
# simple minimax algorithm with alpha beta pruning. The useAlphaBeta is mainly there for testing purposes.
def getMove(board, depth, initialDepth, player, useAlphaBeta, color, alpha, beta):
global searched
global timeCalcPos
searched += 1
# base case, if depth = 0 or the node is a terminal node aka game is over
if depth == 0 or board.is_game_over():
if color == 'WHITE':
if board.is_checkmate():
return -infinity
if board.is_stalemate():
return 0
# start_time = time.time()
f = calculatePos(board)
# timeCalcPos += (time.time() - start_time)
return f
elif color == 'BLACK':
if board.is_checkmate():
return infinity
if board.is_stalemate():
return 0
# start_time = time.time()
f = -calculatePos(board)
# timeCalcPos += (time.time() - start_time)
return f
# Black
best_move = None
if player:
max = -infinity
best_value_move = -infinity
move_list = list(board.generate_legal_moves())
for move in move_list:
# Create a separate board
temp = chess.Board(board.fen())
temp.push_san(str(move))
# get the current value of board
curr_eval = getMove(temp, depth-1, initialDepth, False, useAlphaBeta, color, alpha, beta)
max = numpy.maximum(max, curr_eval)
# Alpha-beta pruning pseudo code can be found at https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning
if useAlphaBeta and max != infinity:
alpha = numpy.maximum(alpha, max)
if alpha >= beta:
break
if max > best_value_move:
best_value_move = max
best_move = (str(move))
if depth < initialDepth:
return max
# White
else:
minimum = infinity
best_value_move = infinity
move_list = list(board.generate_legal_moves())
for move in move_list:
# Create a separate board
temp = chess.Board(board.fen())
temp.push_san(str(move))
# get the current value of board
curr_eval = getMove(temp, depth-1, initialDepth, True, useAlphaBeta, color, alpha, beta)
minimum = numpy.minimum(minimum, curr_eval)
# Alpha-beta pruning pseudo code can be found at https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning
if useAlphaBeta and minimum != -infinity:
beta = numpy.minimum(beta, minimum)
if beta <= alpha:
break
# if the new min is less than min_move set min_move to min and set best_min to the current best move
# because the new value of min is less than min_move it means it is the new best move.
if minimum < best_value_move:
best_value_move = minimum
best_move = str(move)
if depth < initialDepth:
return minimum
# If checkmate is the only move available best_move will never be set so just return the first legal move
if best_move == None:
print("forced mate on every search or something went wrong")
return list(board.generate_legal_moves())[0]
print(f"Time spent calculating evaluation: {timeCalcPos}")
return best_move
if __name__ != '__main__':
for i in position_values:
flipped_pos_vales[i] = numpy.flip(position_values[i], axis=0)