-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
213 lines (184 loc) · 6.27 KB
/
main.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
import chess
import time
from typing import *
CUTOFF_DEPTH = 3
piece_value = {
chess.PAWN: 100,
chess.ROOK: 500,
chess.KNIGHT: 320,
chess.BISHOP: 330,
chess.QUEEN: 900,
chess.KING: 20000
}
pawnEvalWhite = [
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, -20, -20, 10, 10, 5,
5, -5, -10, 0, 0, -10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0
]
pawnEvalBlack = list(reversed(pawnEvalWhite))
knightEval = [
-50, -40, -30, -30, -30, -30, -40, -50,
-40, -20, 0, 0, 0, 0, -20, -40,
-30, 0, 10, 15, 15, 10, 0, -30,
-30, 5, 15, 20, 20, 15, 5, -30,
-30, 0, 15, 20, 20, 15, 0, -30,
-30, 5, 10, 15, 15, 10, 5, -30,
-40, -20, 0, 5, 5, 0, -20, -40,
-50, -40, -30, -30, -30, -30, -40, -50
]
bishopEvalWhite = [
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 5, 0, 0, 0, 0, 5, -10,
-10, 10, 10, 10, 10, 10, 10, -10,
-10, 0, 10, 10, 10, 10, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20
]
bishopEvalBlack = list(reversed(bishopEvalWhite))
rookEvalWhite = [
0, 0, 0, 5, 5, 0, 0, 0,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
]
rookEvalBlack = list(reversed(rookEvalWhite))
queenEval = [
-20, -10, -10, -5, -5, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 5, 5, 5, 0, -10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0, -10,
-10, 0, 5, 0, 0, 0, 0, -10,
-20, -10, -10, -5, -5, -10, -10, -20
]
kingEvalWhite = [
20, 30, 10, 0, 0, 10, 30, 20,
20, 20, 0, 0, 0, 0, 20, 20,
-10, -20, -20, -20, -20, -20, -20, -10,
20, -30, -30, -40, -40, -30, -30, -20,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30,
-30, -40, -40, -50, -50, -40, -40, -30
]
kingEvalBlack = list(reversed(kingEvalWhite))
def evaluate_piece(piece: chess.Piece, square: chess.Square) -> int:
piece_type = piece.piece_type
mapping = []
if piece_type == chess.PAWN:
mapping = pawnEvalWhite if piece.color == chess.WHITE else pawnEvalBlack
if piece_type == chess.KNIGHT:
mapping = knightEval
if piece_type == chess.BISHOP:
mapping = bishopEvalWhite if piece.color == chess.WHITE else bishopEvalBlack
if piece_type == chess.ROOK:
mapping = rookEvalWhite if piece.color == chess.WHITE else rookEvalBlack
if piece_type == chess.QUEEN:
mapping = queenEval
if piece_type == chess.KING:
# use end game piece-square tables if neither side has a queen
mapping = kingEvalWhite if piece.color == chess.WHITE else kingEvalBlack
return mapping[square]
def calc_board_value(board: chess.Board) -> float:
total = 0
for square in chess.SQUARES:
piece = board.piece_at(square)
if not piece:
continue
value = piece_value[piece.piece_type] + evaluate_piece(piece, square)
total += value if piece.color == chess.WHITE else -value
return total
def evaluate_position(board: chess.Board, player: chess.Color):
if board.is_checkmate():
if player == chess.WHITE:
return -float("inf")
else:
return float("inf")
if board.is_stalemate():
return 0.0
else:
return calc_board_value(board)
def alpha_beta_search(board: chess.Board) -> chess.Move:
depth = CUTOFF_DEPTH
if board.turn == chess.WHITE:
value, move = max_value(board, depth - 1, alpha=-float("inf"), beta=float("inf"))
else:
value, move = min_value(board, depth - 1, alpha=-float("inf"), beta=float("inf"))
# print(value)
return move
def max_value(board: chess.Board, depth: int, alpha: float, beta: float) -> tuple[float, Optional[chess.Move]]:
if depth == 0:
value = evaluate_position(board, board.turn)
return value, None
v = -float("inf")
move = None
for action in board.legal_moves:
# if depth == 1:
# print(f"Trying with action: {action}")
board.push(action)
v2, _ = min_value(board, depth - 1, alpha, beta)
board.pop()
if v2 > v:
v, move = v2, action
alpha = max(alpha, v)
if alpha >= beta:
return v, move
return v, move
def min_value(board: chess.Board, depth: int, alpha: float, beta: float) -> tuple[float, Optional[chess.Move]]:
if depth == 0:
value = evaluate_position(board, board.turn)
return value, None
v = float("inf")
move = None
for action in board.legal_moves:
# if depth == 1:
# print(f"Trying with action: {action}")
board.push(action)
# print("temp_board min")
# print(temp_board)
v2, _ = max_value(board, depth - 1, alpha, beta)
board.pop()
if v2 < v:
v, move = v2, action
beta = min(beta, v)
if beta <= alpha:
return v, move
return v, move
if __name__ == '__main__':
chessBoard = chess.Board()
provideFen = input("Would you like to provide a FEN string? (y/n)\n")
provideFen.lower()
if provideFen == "y" or provideFen == "yes":
fen = input("Provide FEN string\n")
try:
chessBoard.set_board_fen(fen)
except ValueError:
print("FEN string is invalid")
# chessBoard.set_fen("4rk2/1bp3p1/5p2/p7/2B1rN2/1P4P1/P4P1P/3R2K1 w - - 1 1")
print(f"Initial state:\n{chessBoard}\n")
for i in range(0, 30):
print("White moves") if chessBoard.turn == chess.WHITE else print("Black moves")
if chessBoard.is_checkmate():
print(f"Checkmate!\n{chessBoard.fen()}")
break
if chessBoard.is_stalemate():
print("Stalemate!")
break
start = time.time()
nextMove = alpha_beta_search(chessBoard)
end = time.time()
# print(move)
chessBoard.push(nextMove)
print(f"{i + 1}: Search performed in {(end - start):.3f} seconds, move {nextMove}:\n{chessBoard}\n")