-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyss_evaluation.py
More file actions
52 lines (41 loc) · 1.5 KB
/
pyss_evaluation.py
File metadata and controls
52 lines (41 loc) · 1.5 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
"""evaluates the board heuristically"""
import chess
# Piece values
# Starting with a zero because chess.PieceType is 1-indexed
values = [ 0, 100, 320, 330, 500, 900, 20000 ]
# Center squares in index form [E4, D4, E5, D5] is [28, 27, 35, 36]
center_squares = [
27, 28,
35, 36
]
def evaluate(board):
"""evaluates the board heuristically"""
# Checkmate/stalemate
if board.is_checkmate():
return -9999 if board.turn else 9999
if board.is_stalemate():
return 0
# Material
material = 0
for piece in board.piece_map().values():
value = values[piece.piece_type]
material += value if piece.color else -value
# Mobility
mobility = 0
legal_moves = len(list(board.legal_moves))
mobility += legal_moves if board.turn else -legal_moves
# push an empty move to see the opponent's mobility
board.push(chess.Move.null())
legal_moves = len(list(board.legal_moves))
mobility += legal_moves if board.turn else -legal_moves
board.pop()
# Center control
center_control = 0
for square in center_squares:
if board.piece_at(square):
center_control += 10 if board.piece_at(square).color else -10
return material + center_control + mobility
# I did not include piece value tables because they are an example of human bias.
# The AI should not have to play how we tell it to.
# Center Control is biased as well, but I use it anyway.
# This is because it is fair to assume that center control is important in chess.