-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate.py
183 lines (158 loc) · 5.54 KB
/
evaluate.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
import chess
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))
kingEvalEndGameWhite = [
50, -30, -30, -30, -30, -30, -30, -50,
-30, -30, 0, 0, 0, 0, -30, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 20, 30, 30, 20, -10, -30,
-30, -20, -10, 0, 0, -10, -20, -30,
-50, -40, -30, -20, -20, -30, -40, -50
]
kingEvalEndGameBlack = list(reversed(kingEvalEndGameWhite))
def move_value(board: chess.Board, move: chess.Move, endgame: bool) -> float:
if move.promotion is not None:
return -float("inf") if board.turn == chess.BLACK else float("inf")
_piece = board.piece_at(move.from_square)
if _piece:
_from_value = evaluate_piece(_piece, move.from_square, endgame)
_to_value = evaluate_piece(_piece, move.to_square, endgame)
position_change = _to_value - _from_value
else:
raise Exception(f"A piece was expected at {move.from_square}")
capture_value = 0.0
if board.is_capture(move):
capture_value = evaluate_capture(board, move)
current_move_value = capture_value + position_change
if board.turn == chess.BLACK:
current_move_value = -current_move_value
return current_move_value
def evaluate_capture(board: chess.Board, move: chess.Move) -> float:
if board.is_en_passant(move):
return piece_value[chess.PAWN]
_to = board.piece_at(move.to_square)
_from = board.piece_at(move.from_square)
if _to is None or _from is None:
raise Exception(
f"Pieces were expected at _both_ {move.to_square} and {move.from_square}"
)
return piece_value[_to.piece_type] - piece_value[_from.piece_type]
def evaluate_piece(piece: chess.Piece, square: chess.Square, end_game: bool) -> 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:
if end_game:
mapping = (
kingEvalEndGameWhite
if piece.color == chess.WHITE
else kingEvalEndGameBlack
)
else:
mapping = kingEvalWhite if piece.color == chess.WHITE else kingEvalBlack
return mapping[square]
def evaluate_board(board: chess.Board) -> float:
total = 0
end_game = check_end_game(board)
for square in chess.SQUARES:
piece = board.piece_at(square)
if not piece:
continue
value = piece_value[piece.piece_type] + evaluate_piece(piece, square, end_game)
total += value if piece.color == chess.WHITE else -value
return total
def check_end_game(board: chess.Board) -> bool:
queens = 0
minors = 0
for square in chess.SQUARES:
piece = board.piece_at(square)
if piece and piece.piece_type == chess.QUEEN:
queens += 1
if piece and (
piece.piece_type == chess.BISHOP or piece.piece_type == chess.KNIGHT
):
minors += 1
if queens == 0 or (queens == 2 and minors <= 1):
return True
return False