Skip to content

Instantly share code, notes, and snippets.

@luccabb
Created December 7, 2021 05:58
Show Gist options
  • Save luccabb/7f83b610e8a2c2bd653f35b2468fd8d8 to your computer and use it in GitHub Desktop.
Save luccabb/7f83b610e8a2c2bd653f35b2468fd8d8 to your computer and use it in GitHub Desktop.
Simple evaluation function
def board_value(board: chess.Board) -> float:
"""
This functions receives a board and assigns a value to it, it acts as
an evaluation function of the current state for this game. It returns
Arguments:
- board: current board state.
Returns:
- total_value(int): integer representing
current value for this board.
"""
total_value = 0
black_pieces, white_pieces = count_pieces(board)
for row in range(8):
for col in range(8):
square_index = row*8+col
square = chess.SQUARES[square_index]
piece = board.piece_at(square)
if piece:
piece_color = piece.color
# adding to totalValue the value coming from black pieces.
# When the black is evaluating it will try to find the highest possible totalValue
# in this case meaning that it has more black pieces in the table and less white pieces
if piece.symbol().lower() in PIECE_VALUES:
if piece_color == chess.BLACK:
total_value += PIECE_VALUES[piece.symbol().lower()]
else:
total_value -= PIECE_VALUES[piece.symbol().lower()]
if piece.symbol().lower() == 'p':
pawn_in_col = 0
# count pawns in column
for j in range(8):
square_index_helper = j*8+col
square_helper = chess.SQUARES[square_index_helper]
piece_helper = board.piece_at(square_helper)
if piece_helper and piece_helper.symbol().lower() == 'p' and piece_helper.color == piece_color:
pawn_in_col += 1
if pawn_in_col >= 2:
if piece_color == chess.BLACK:
total_value -= 0.5*pawn_in_col
else:
total_value += 0.5*pawn_in_col
# if we are in the beginning of the game, we want to have a few different heuristics
if white_pieces + black_pieces >= 27:
# removing points for moving queen too early in the game
if piece.symbol().lower() == 'q' and (row, col) not in QUEEN_START:
if piece_color == chess.BLACK:
total_value -= 6
else:
total_value += 6
# points for moving knight early in the game
if piece.symbol().lower() == 'n' and (row not in KNIGHT_ROW and (col != LEFT_KNIGHT_COL or col != RIGHT_KNIGHT_COL)):
if piece_color == chess.BLACK:
total_value += 3
else:
total_value -= 3
# points for moving bishop early in the game
# False
if piece.symbol().lower() == 'b' and (row not in BISHOP_ROW and (col != LEFT_BISHOP_ROW or col != RIGHT_BISHOP_ROW)):
if piece_color == chess.BLACK:
total_value += 3
else:
total_value -= 3
# points for dominating the middle squares
# 4 squares in the center of the board
if row in CENTER_ROWS and col in CENTER_ROWS:
if piece_color == chess.BLACK:
total_value += 0.07
else:
total_value -= 0.07
# points for dominating the middle rows
elif row in CENTER_ROWS and col in CENTER_COLUMNS:
if piece_color == chess.BLACK:
total_value += 0.06
else:
total_value -= 0.06
# end game heuristics
if white_pieces <= 6:
if piece.symbol().lower() == 'k':
if (row, col) in CORNERS:
if piece_color == chess.WHITE:
total_value += 9
else:
total_value -= 9
elif black_pieces <= 6:
if piece.symbol().lower() == 'k':
if (row, col) in CORNERS:
if piece_color == chess.BLACK:
total_value -= 9
else:
total_value += 9
return round(total_value, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment