def knight_safety(self): if not self._player: their_knight = self._board.generic_find("n") their_king = self._board.generic_find("k") if their_knight: knight_piece = Knight(self._board, not self._player, their_knight) if knight_piece.knight_movement().count("N") == 1: return False if their_king: from Pieces.King import King king_piece = King(self._board, not self._player, their_king) if king_piece.king_movement().count("N") == 1: return False return True else: max_knight = self._board.generic_find("n") max_king = self._board.generic_find("k") max_rook = self._board.generic_find("r") if max_knight: knight_piece = Knight(self._board, not self._player, max_knight) what_piece = knight_piece.knight_movement() if knight_piece.knight_movement().count("N") == 1: return False if max_king: from Pieces.King import King king_piece = King(self._board, not self._player, max_king) what_piece = king_piece.king_movement() if king_piece.king_movement().count("N") == 1: return False if max_rook: from Pieces.Rook import Rook rook_piece = Rook(self._board, not self._player, max_rook) what_piece = rook_piece.rook_piece_check() if rook_piece.rook_piece_check().count("N") == 1: return False return True
def king_safety(self): your_king = self._board.generic_find("K") #MAXIMIZE if your_king is not None and not self._player: knight = self._board.generic_find("n") king = self._board.generic_find("k") if king: their_king = King(self._board, not self._player, king) #piece = their_king.king_movement() #print(piece) if their_king.king_movement().count("K") == 1: return False if knight: their_knight = Knight(self._board, not self._player, knight) if their_knight.knight_movement().count("K") == 1: return False return True #MINIMIZE else: rook = self._board.generic_find("r") #print("CHECKING") if rook: from .Rook import Rook their_rook = Rook(self._board, self._player, rook) #piece = their_rook.rook_piece_check() if their_rook.rook_piece_check().count("K") == 1: return False knight = self._board.generic_find("n") if knight: from .Knight import Knight their_knight = Knight(self._board, self._player, knight) #piece = their_knight.knight_movement() if their_knight.knight_movement().count("K") == 1: return False king = self._board.generic_find("k") if king: their_king = King(self._board, self._player, king) #piece = their_king.king_movement() if their_king.king_movement().count("K") == 1: return False return True
def knight_safety(self): max_knight = self._board.generic_find("n") max_king = self._board.generic_find("k") max_rook = self._board.generic_find("r") if max_knight: knight_piece = Knight(self._board, max_knight) if knight_piece.knight_movement().count("N") == 1: return False if max_king: from Pieces.King import King king_piece = King(self._board, max_king) if king_piece.king_movement().count("N") == 1: return False if max_rook: from Pieces.Rook import Rook rook_piece = Rook(self._board, max_rook) if rook_piece.rook_piece_check().count("N") == 1: return False return True
def heuristicX(position): if position.is_checkmate(): return 800000 king_index = position.generic_find("K") rook_index = position.generic_find("R") knight_index = position.generic_find("N") t_king_index = position.generic_find("k") t_knight_index = position.generic_find("n") if king_index: king = King(position, king_index) if rook_index: rook = Rook(position, rook_index) else: return 0 if knight_index: knight = Knight(position, knight_index) else: knight = None if t_king_index: their_king = King(position, t_king_index) if t_knight_index: their_knight = Knight(position, t_knight_index) if king.king_movement().count("k") == 1 and king.king_safety(): return 99999 if rook.rook_piece_check().count( "k") == 1 and rook.rook_safety() and not king.king_safety(): return 999 #if minimize player can eliminate an opponent piece do it if position.check_board() and rook.rook_safety(): return 99999 #if an opponent piece can be eliminated do it if position.check_board() and (king.king_safety() or king.king_movement().count("k") == 1)\ and (rook.rook_safety() or not rook.rook_safety() and king.king_movement().count("R") == 1): return 99999 if their_king.check_mated(king, knight, rook) and king.king_safety()\ and (king.king_movement().count("R") == 1 or knight.knight_movement().count("R") == 1\ or rook.rook_safety()): return 1000 if not king.king_safety(): return -1000 if not knight.knight_safety() and king.king_movement().count("N") == 0\ and rook.rook_piece_check().count("N") == 0: return -1 if t_knight_index: if king.king_movement().count("R") == 1 and their_king.king_movement().count("R") == 1\ and their_knight.knight_movement().count("R") == 0 and not rook.check_king_space(t_king_index,10): return 200 + rook.trap_king(t_king_index) + \ (10 - abs(king_index // 10 - rook_index // 10)) + \ (10 - abs(knight_index // 10 - king_index // 10))+\ (100 - abs(knight_index - king_index )) if t_knight_index: if not rook.rook_safety() and their_knight.knight_movement().count( "R") == 1: return -5 if not rook.rook_safety(): return -4 if (t_king_index // 10 == 2 or t_king_index % 10 == 8\ or t_king_index // 10 == 8 or t_king_index % 10 == 1)\ and rook.check_king_space(t_king_index, 10)\ and (king.increase_movement(rook_index) or king.king_movement().count("R") == 1)\ and (their_king.increase_movement(rook_index) or their_king.king_movement().count("R") == 1)\ and their_king.increase_movement(king_index): if knight.knight_movement().count("k") == 1: return 500 + rook.trap_king(t_king_index) else: return 500 + rook.trap_king(t_king_index) + (5 - abs(knight_index//10 - t_king_index//10))\ + (5 - abs(knight_index % 10 - t_king_index % 10)) if rook.check_king_space(t_king_index, 7) and king.king_movement().count("R") == 1\ and their_king.increase_movement(king_index): if knight.knight_movement().count("k") == 1: return 300 + rook.trap_king(t_king_index) else: return 100 + rook.trap_king(t_king_index) +\ (10 - abs(king_index // 10 - t_king_index // 10))\ + (10 - abs(king_index % 10 - t_king_index % 10))+\ (100 - abs(knight_index - t_king_index)) if king.king_movement().count("R") == 1: if king_index // 10 < rook_index // 10 and rook_index % 10 == king_index % 10: pass elif rook_index % 10 > t_king_index % 10: return rook.trap_king(t_king_index) + ( 10 - abs(king_index % 10 - t_king_index % 10)) elif rook_index % 10 < t_king_index % 10: return rook.trap_king(t_king_index)+ (10 - abs(king_index % 10 - t_king_index% 10 ))\ + (10 - abs(king_index // 10 - t_king_index // 10 )) if king.increase_movement(rook_index) and rook_index // 10 != 9\ and rook_index % 10 < t_king_index % 10: return 20 if rook_index // 10 == 8 and king_index // 10 == 9\ and knight_index // 10 == 9: return 10 return 0