def _get_king_movement_area(self, board, sq1, king_sq): # Determines the number of squares the (enemy) king is limited to sq1_file_index = chess.file_index(sq1) sq1_rank_index = chess.rank_index(sq1) king_file_index = chess.file_index(king_sq) king_rank_index = chess.rank_index(king_sq) if (sq1_file_index == king_file_index or sq1_rank_index == king_rank_index): return float("inf") if (king_file_index < sq1_file_index): if (king_rank_index < sq1_rank_index): # King SW of rook return (sq1_file_index * sq1_rank_index) else: # King NW of rook return (sq1_file_index * (7 - sq1_rank_index)) else: if (king_rank_index < sq1_rank_index): # King SE of rook return ((7 - sq1_file_index) * sq1_rank_index) else: # King NE of rook return ((7 - sq1_file_index) * (7 - sq1_rank_index))
def _distance(self, board, sq1, sq2): assert (sq1 != sq2) sq1_file_index = chess.file_index(sq1) sq1_rank_index = chess.rank_index(sq1) sq2_file_index = chess.file_index(sq2) sq2_rank_index = chess.rank_index(sq2) return (abs(sq1_file_index - sq2_file_index) + abs(sq1_rank_index - sq2_rank_index))
def _is_piece_protected_by_king(self, board, sq1, king_sq): assert (sq1 != king_sq) sq1_file_index = chess.file_index(sq1) sq1_rank_index = chess.rank_index(sq1) king_file_index = chess.file_index(king_sq) king_rank_index = chess.rank_index(king_sq) return (abs(sq1_file_index - king_file_index) <= 1 and abs(sq1_rank_index - king_rank_index) <= 1)
def _is_knight_distance_away(self, board, sq1, sq2): assert (sq1 != sq2) sq1_file_index = chess.file_index(sq1) sq1_rank_index = chess.rank_index(sq1) sq2_file_index = chess.file_index(sq2) sq2_rank_index = chess.rank_index(sq2) file_diff = abs(sq1_file_index - sq2_file_index) rank_diff = abs(sq1_rank_index - sq2_rank_index) return (file_diff == 1 and rank_diff == 2) or (file_diff == 2 and rank_diff == 1)
def _both_kings_same_quadrant(self, board, queen_sq, king_sq, enemy_king_sq): queen_file_index = chess.file_index(queen_sq) queen_rank_index = chess.rank_index(queen_sq) king_file_index = chess.file_index(king_sq) king_rank_index = chess.rank_index(king_sq) enemy_king_file_index = chess.file_index(enemy_king_sq) enemy_king_rank_index = chess.rank_index(enemy_king_sq) QUAD_SW = 1 QUAD_SE = 2 QUAD_NW = 3 QUAD_NE = 4 quad_king = None quad_enemy_king = None if (king_file_index < queen_file_index): if (king_rank_index < queen_rank_index): # King SW of rook quad_king = QUAD_SW elif (king_rank_index > queen_rank_index): # King NW of rook quad_king = QUAD_NW elif (king_file_index > queen_file_index): if (king_rank_index < queen_rank_index): # King SE of rook quad_king = QUAD_SE elif (king_rank_index > queen_rank_index): # King NE of rook quad_king = QUAD_NE if (enemy_king_file_index < queen_file_index): if (enemy_king_rank_index < queen_rank_index): # Enemy king SW of rook quad_enemy_king = QUAD_SW elif (enemy_king_rank_index > queen_rank_index): # Enemy king NW of rook quad_enemy_king = QUAD_NW elif (enemy_king_file_index > queen_file_index): if (enemy_king_rank_index < queen_rank_index): # Enemy king SE of rook quad_enemy_king = QUAD_SE elif (enemy_king_rank_index > queen_rank_index): # Enemy king NE of rook quad_enemy_king = QUAD_NE return quad_king and quad_enemy_king and quad_king == quad_enemy_king
def _is_king_picking(self, board, sq1, king_sq, enemy_king_sq): sq1_file_index = chess.file_index(sq1) sq1_rank_index = chess.rank_index(sq1) king_file_index = chess.file_index(king_sq) king_rank_index = chess.rank_index(king_sq) enemy_king_file_index = chess.file_index(enemy_king_sq) enemy_king_rank_index = chess.rank_index(enemy_king_sq) if (enemy_king_file_index < sq1_file_index): if (enemy_king_rank_index < sq1_rank_index): # King SW of rook if (king_file_index < sq1_file_index and king_rank_index == sq1_rank_index): return True if (king_rank_index < sq1_rank_index and king_file_index == sq1_file_index): return True else: # King NW of rook if (king_file_index < sq1_file_index and king_rank_index == sq1_rank_index): return True if (king_rank_index > sq1_rank_index and king_file_index == sq1_file_index): return True else: if (enemy_king_rank_index < sq1_rank_index): # King SE of rook if (king_file_index > sq1_file_index and king_rank_index == sq1_rank_index): return True if (king_rank_index < sq1_rank_index and king_file_index == sq1_file_index): return True else: # King NE of rook if (king_file_index > sq1_file_index and king_rank_index == sq1_rank_index): return True if (king_rank_index > sq1_rank_index and king_file_index == sq1_file_index): return True return False
def _get_finish_move_endgame_1(self, board, rook_sq, king_sq, enemy_king_sq): rook_file_index = chess.file_index(rook_sq) rook_rank_index = chess.rank_index(rook_sq) king_file_index = chess.file_index(king_sq) king_rank_index = chess.rank_index(king_sq) enemy_king_file_index = chess.file_index(enemy_king_sq) enemy_king_rank_index = chess.rank_index(enemy_king_sq) move = None if (king_rank_index == enemy_king_rank_index): if (abs(king_file_index - enemy_king_file_index) == 2): if (abs(rook_rank_index - king_rank_index) == 1): move = chess.SQUARE_NAMES[rook_sq] + chess.SQUARE_NAMES[ rook_rank_index * 8 + king_file_index] if (king_file_index == enemy_king_file_index): if (abs(king_rank_index - enemy_king_rank_index) == 2): if (abs(rook_file_index - king_file_index) == 1): move = chess.SQUARE_NAMES[rook_sq] + chess.SQUARE_NAMES[ king_rank_index * 8 + rook_sq % 8] return move
def _get_finish_move_endgame_2(self, board, queen_sq, king_sq, enemy_king_sq): queen_file_index = chess.file_index(queen_sq) queen_rank_index = chess.rank_index(queen_sq) king_file_index = chess.file_index(king_sq) king_rank_index = chess.rank_index(king_sq) enemy_king_file_index = chess.file_index(enemy_king_sq) enemy_king_rank_index = chess.rank_index(enemy_king_sq) move = None if (queen_sq == chess.E2) and (king_sq == chess.E1) and (enemy_king_sq == chess.G1): return "e2d2" elif (queen_sq == chess.D2) and (king_sq == chess.D1) and (enemy_king_sq == chess.B1): return "d2e2" elif (queen_sq == chess.B4) and (king_sq == chess.A4) and (enemy_king_sq == chess.A2): return "b4b5" elif (queen_sq == chess.B5) and (king_sq == chess.A5) and (enemy_king_sq == chess.A7): return "b5b4" elif (queen_sq == chess.D7) and (king_sq == chess.D8) and (enemy_king_sq == chess.B8): return "d7e7" elif (queen_sq == chess.E7) and (king_sq == chess.E8) and (enemy_king_sq == chess.G8): return "e7d7" elif (queen_sq == chess.G5) and (king_sq == chess.H5) and (enemy_king_sq == chess.H7): return "g5g4" elif (queen_sq == chess.G4) and (king_sq == chess.G4) and (enemy_king_sq == chess.H2): return "g4g5" if (queen_sq == chess.E2) and (king_sq == chess.E1) and (enemy_king_sq == chess.H1): return "e2g4" elif (queen_sq == chess.D2) and (king_sq == chess.D1) and (enemy_king_sq == chess.A1): return "d2b4" elif (queen_sq == chess.B4) and (king_sq == chess.A4) and (enemy_king_sq == chess.A1): return "b4d2" elif (queen_sq == chess.B5) and (king_sq == chess.A5) and (enemy_king_sq == chess.A8): return "b5d7" elif (queen_sq == chess.D7) and (king_sq == chess.D8) and (enemy_king_sq == chess.A8): return "d7b5" elif (queen_sq == chess.E7) and (king_sq == chess.E8) and (enemy_king_sq == chess.H8): return "e7g5" elif (queen_sq == chess.G5) and (king_sq == chess.H5) and (enemy_king_sq == chess.H8): return "g5e7" elif (queen_sq == chess.G4) and (king_sq == chess.H4) and (enemy_king_sq == chess.H1): return "g4e2" if (queen_sq == chess.D2) and (king_sq == chess.E1) and (enemy_king_sq == chess.H1): return "e1f2" elif (queen_sq == chess.E2) and (king_sq == chess.D1) and (enemy_king_sq == chess.A1): return "d1c2" elif (queen_sq == chess.B5) and (king_sq == chess.A4) and (enemy_king_sq == chess.A1): return "a4b3" elif (queen_sq == chess.B4) and (king_sq == chess.A5) and (enemy_king_sq == chess.A8): return "a5b6" elif (queen_sq == chess.E7) and (king_sq == chess.D8) and (enemy_king_sq == chess.A8): return "d8c7" elif (queen_sq == chess.D7) and (king_sq == chess.E8) and (enemy_king_sq == chess.H8): return "e8f7" elif (queen_sq == chess.G4) and (king_sq == chess.H5) and (enemy_king_sq == chess.H8): return "h5g6" elif (queen_sq == chess.G5) and (king_sq == chess.H4) and (enemy_king_sq == chess.H1): return "h4g3" return move