Skip to content

Commit

Permalink
Add passed pawns and pawn shield
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcticXWolf committed Feb 27, 2021
1 parent 4501428 commit 7eec7f5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
103 changes: 102 additions & 1 deletion engines/axwchessbot/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def evaluate_material_score_phase(self, is_endgame: bool) -> int:
score += color_score
else:
score -= color_score

if not is_endgame:
score += self.evaluate_king_shield(chess.WHITE)
score += self.evaluate_king_shield(chess.BLACK)

return score

def evaluate_gamephase(self):
Expand Down Expand Up @@ -131,7 +136,103 @@ def evaluate_passed_pawns(self):
return score

def is_passed_pawn(self, square: chess.Square, color: chess.Color) -> bool:
return False
piece = self.board.piece_type_at(square)
if self.board.piece_type_at(square) != chess.PAWN:
return False

ranks_to_go = range(chess.square_rank(square) + 1, 8)
if color == chess.BLACK:
ranks_to_go = range(0, chess.square_rank(square))

amount_pawns_on_same_file = sum(
[
self.board.piece_type_at(chess.square(chess.square_file(square), i))
== chess.PAWN
for i in ranks_to_go
]
)
if amount_pawns_on_same_file > 0:
return False

amount_enemy_pawns_on_adjacent_files = 0
if chess.square_file(square) - 1 >= 0:
amount_enemy_pawns_on_adjacent_files += sum(
[
self.board.piece_at(chess.square(chess.square_file(square) - 1, i))
== chess.Piece(chess.PAWN, not color)
for i in ranks_to_go
]
)
if chess.square_file(square) + 1 <= 8:
amount_enemy_pawns_on_adjacent_files += sum(
[
self.board.piece_at(chess.square(chess.square_file(square) + 1, i))
== chess.Piece(chess.PAWN, not color)
for i in ranks_to_go
]
)

if amount_enemy_pawns_on_adjacent_files > 0:
return False

return True

def evaluate_king_shield(self, color: chess.Color):
score = 0
rank_2_to_check = 2
rank_3_to_check = 3
king_position = self.board.pieces(chess.KING, color).pop()

if color == chess.BLACK:
rank_2_to_check = 7
rank_3_to_check = 6

if chess.square_file(king_position) > 4:
pawn_count_2 = len(
[
self.board.piece_at(chess.square(i, rank_2_to_check))
== chess.Piece(chess.PAWN, color)
for i in range(5, 8)
]
)
pawn_count_3 = len(
[
self.board.piece_at(chess.square(i, rank_3_to_check))
== chess.Piece(chess.PAWN, color)
for i in range(5, 8)
]
)
score += (
pawn_count_2 * score_tables.additional_modifiers["king_shield_rank_2"]
)
score += (
pawn_count_3 * score_tables.additional_modifiers["king_shield_rank_3"]
)
elif chess.square_file(king_position) < 3:
pawn_count_2 = len(
[
self.board.piece_at(chess.square(i, rank_2_to_check))
== chess.Piece(chess.PAWN, color)
for i in range(0, 3)
]
)
pawn_count_3 = len(
[
self.board.piece_at(chess.square(i, rank_3_to_check))
== chess.Piece(chess.PAWN, color)
for i in range(0, 3)
]
)
score += (
pawn_count_2 * score_tables.additional_modifiers["king_shield_rank_2"]
)
score += (
pawn_count_3 * score_tables.additional_modifiers["king_shield_rank_3"]
)

if color == chess.BLACK:
return -score
return score

def attacked_by_inferior_piece(
self, move: chess.Move, evaluate_to_square: bool
Expand Down
3 changes: 3 additions & 0 deletions engines/axwchessbot/score_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"open_rook": 10,
"half_rook": 5,
"tempo": 10,
"king_shield_rank_2": 10,
"king_shield_rank_3": 5,
"king_no_shield_penalty": 10,
}

additional_settings = {
Expand Down

0 comments on commit 7eec7f5

Please sign in to comment.