-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
327 lines (282 loc) · 9.91 KB
/
Copy pathevaluation.py
File metadata and controls
327 lines (282 loc) · 9.91 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""
Evaluation module for chess positions.
- evaluate(board, side_to_move) -> int
Returns centipawn score from White's perspective (>0 = good for White).
The evaluation currently includes:
- Material term
- Piece-square term (knights only for now)
- Pawn structure term (doubled, isolated, passed)
- King safety term (simple heuristic)
Board representation:
- board is an 8x8 list of lists with single-character strings
'.' empty, uppercase = White piece, lowercase = Black piece
'P','N','B','R','Q','K' / 'p','n','b','r','q','k'
This module is self-contained and does not depend on Pygame.
"""
from typing import List, Tuple, Optional
Board = List[List[str]]
PIECE_VALUES = {
"P": 100,
"N": 320,
"B": 330,
"R": 500,
"Q": 900,
"K": 0,
}
# Piece-square tables (from White's perspective: row 0 = 8th rank)
PAWN_TABLE: List[List[int]] = [
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 50, 50, 50, 50, 50, 50, 50, 50],
[ 10, 10, 20, 30, 30, 20, 10, 10],
[ 5, 5, 10, 25, 25, 10, 5, 5],
[ 0, 0, 0, 20, 20, 0, 0, 0],
[ 5, -5, -10, 0, 0, -10, -5, 5],
[ 5, 10, 10, -20, -20, 10, 10, 5],
[ 0, 0, 0, 0, 0, 0, 0, 0],
]
KNIGHT_TABLE: List[List[int]] = [
[-50, -40, -30, -30, -30, -30, -40, -50],
[-40, -20, 0, 5, 5, 0, -20, -40],
[-30, 5, 10, 15, 15, 10, 5, -30],
[-30, 0, 15, 20, 20, 15, 0, -30],
[-30, 5, 15, 20, 20, 15, 5, -30],
[-30, 0, 10, 15, 15, 10, 0, -30],
[-40, -20, 0, 0, 0, 0, -20, -40],
[-50, -40, -30, -30, -30, -30, -40, -50],
]
BISHOP_TABLE: List[List[int]] = [
[-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],
]
ROOK_TABLE: List[List[int]] = [
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 5, 10, 10, 10, 10, 10, 10, 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, 0, 0, 5, 5, 0, 0, -5],
[ 0, 0, 0, 0, 0, 0, 0, 0],
]
QUEEN_TABLE: List[List[int]] = [
[-20, -10, -10, -5, -5, -10, -10, -20],
[-10, 0, 5, 0, 0, 0, 0, -10],
[-10, 5, 5, 5, 5, 5, 0, -10],
[ 0, 0, 5, 5, 5, 5, 0, -5],
[ -5, 0, 5, 5, 5, 5, 0, -5],
[-10, 0, 5, 5, 5, 5, 0, -10],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-20, -10, -10, -5, -5, -10, -10, -20],
]
KING_TABLE: List[List[int]] = [
[-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],
[-20, -30, -30, -40, -40, -30, -30, -20],
[-10, -20, -20, -20, -20, -20, -20, -10],
[ 20, 20, 0, 0, 0, 0, 20, 20],
[ 20, 30, 10, 0, 0, 10, 30, 20],
]
def material_term(board: Board) -> int:
"""Material balance: White material minus Black material in centipawns."""
score = 0
for r in range(8):
for c in range(8):
ch = board[r][c]
if ch == ".":
continue
ptype = ch.upper()
value = PIECE_VALUES.get(ptype, 0)
if ch.isupper(): # white
score += value
else: # black
score -= value
return score
def piece_square_term(board: Board) -> int:
"""Positional bonuses/penalties based on piece-square tables for all pieces."""
score = 0
for r in range(8):
for c in range(8):
ch = board[r][c]
if ch == ".":
continue
ptype = ch.upper()
if ptype == "P":
if ch.isupper(): # white pawn
score += PAWN_TABLE[r][c]
else: # black pawn
score -= PAWN_TABLE[7 - r][c]
elif ptype == "N":
if ch.isupper(): # white knight
score += KNIGHT_TABLE[r][c]
else: # black knight (mirror vertically)
score -= KNIGHT_TABLE[7 - r][c]
elif ptype == "B":
if ch.isupper():
score += BISHOP_TABLE[r][c]
else:
score -= BISHOP_TABLE[7 - r][c]
elif ptype == "R":
if ch.isupper():
score += ROOK_TABLE[r][c]
else:
score -= ROOK_TABLE[7 - r][c]
elif ptype == "Q":
if ch.isupper():
score += QUEEN_TABLE[r][c]
else:
score -= QUEEN_TABLE[7 - r][c]
elif ptype == "K":
if ch.isupper():
score += KING_TABLE[r][c]
else:
score -= KING_TABLE[7 - r][c]
return score
# ----- Pawn structure helpers -----
def doubled_pawn_penalty(board: Board) -> int:
"""Penalty for doubled pawns on the same file. Negative for White's doubled, positive for Black's doubled (since good for White)."""
score = 0
for file in range(8):
white_count = 0
black_count = 0
for rank in range(8):
ch = board[rank][file]
if ch == "P":
white_count += 1
elif ch == "p":
black_count += 1
if white_count > 1:
score -= 10 * (white_count - 1)
if black_count > 1:
score += 10 * (black_count - 1)
return score
def isolated_pawn_penalty(board: Board) -> int:
"""Penalty for isolated pawns (no friendly pawn on adjacent files)."""
score = 0
for r in range(8):
for c in range(8):
ch = board[r][c]
if ch not in ("P", "p"):
continue
is_white = ch == "P"
has_neighbor = False
for dc in (-1, 1):
nc = c + dc
if 0 <= nc < 8:
for rr in range(8):
neighbor = board[rr][nc]
if is_white and neighbor == "P":
has_neighbor = True
break
if not is_white and neighbor == "p":
has_neighbor = True
break
if has_neighbor:
break
if not has_neighbor:
if is_white:
score -= 15
else:
score += 15
return score
def is_white_passed(board: Board, r: int, c: int) -> bool:
for rr in range(0, r): # rows in front of white pawn (towards 0)
for dc in (-1, 0, 1):
cc = c + dc
if 0 <= cc < 8:
if board[rr][cc] == "p":
return False
return True
def is_black_passed(board: Board, r: int, c: int) -> bool:
for rr in range(r + 1, 8): # rows in front of black pawn (towards 7)
for dc in (-1, 0, 1):
cc = c + dc
if 0 <= cc < 8:
if board[rr][cc] == "P":
return False
return True
def passed_pawn_bonus(board: Board) -> int:
score = 0
for r in range(8):
for c in range(8):
ch = board[r][c]
if ch == "P" and is_white_passed(board, r, c):
score += (7 - r) * 10 # closer to promotion (row 0) => bigger bonus
elif ch == "p" and is_black_passed(board, r, c):
score -= r * 10
return score
def pawn_structure_term(board: Board) -> int:
return (
doubled_pawn_penalty(board)
+ isolated_pawn_penalty(board)
+ passed_pawn_bonus(board)
)
# ----- King safety -----
def queens_on_board(board: Board) -> bool:
for r in range(8):
for c in range(8):
if board[r][c] in ("Q", "q"):
return True
return False
def find_kings(board: Board) -> Tuple[Optional[Tuple[int, int]], Optional[Tuple[int, int]]]:
w_king = b_king = None
for r in range(8):
for c in range(8):
if board[r][c] == "K":
w_king = (r, c)
elif board[r][c] == "k":
b_king = (r, c)
return w_king, b_king
def king_safety_term(board: Board) -> int:
score = 0
if queens_on_board(board):
w_king, b_king = find_kings(board)
# Assume white starts at (7,4), black at (0,4)
if w_king == (7, 4):
score -= 25
if b_king == (0, 4):
score += 25
return score
def evaluate(board: Board, side_to_move: str) -> int:
"""
Static evaluation from White's perspective (centipawns).
Args:
board: 8x8 matrix of '.', 'P','p', ... uppercase=White, lowercase=Black
side_to_move: 'w' or 'b' (currently unused, but kept for future tweaks)
Returns:
int: score in centipawns (>0 better for White)
"""
score = 0
# 1) Material
score += material_term(board)
# 2) Piece-square
score += piece_square_term(board)
# 3) Pawn structure
score += pawn_structure_term(board)
# 4) King safety
score += king_safety_term(board)
# (optional) later tweaks: tempo bonus, phase blending, mobility, etc.
return score
def evaluate_pov(board: Board, pov: str) -> int:
"""Return evaluation from the specified side's perspective.
pov: 'w' or 'b'. Internally, base evaluation is from White's POV.
"""
base = evaluate(board, 'w')
if pov.lower().startswith('w'):
return base
return -base
__all__ = [
"evaluate",
"evaluate_pov",
"material_term",
"piece_square_term",
"pawn_structure_term",
"king_safety_term",
]