Esempio n. 1
0
    def _all_moves(self, player=None, idx_list=range(64)):
        """
        Get a list containing all reachable moves for pieces owned by the
        specified player (including moves that would expose the player's king
        to check) that are located at positions included in the idx_list. By
        default, it compiles the list for the active player (i.e.,
        self.state.player) by checking every square on the board.
        """
        player = player or self.state.player
        res_moves = []
        for start in idx_list:
            if self.board.get_owner(start) != player:
                continue

            # MOVES contains the list of all possible moves for a piece of
            # the specified type on an empty chess board.
            piece = self.board.get_piece(start)
            rays = MOVES.get(piece, [''] * 64)

            for ray in rays[start]:
                # Trace each of the 8 (or fewer) possible directions that a
                # piece at the given starting index could move

                new_moves = self._trace_ray(start, piece, ray, player)
                res_moves.extend(new_moves)

        return res_moves
Esempio n. 2
0
    def san_to_long(self, san_str, verbose=False):
        san = San(san_str)
        if not san.end_square:
            # Castling move, end square depends on player
            if san.castling == 'K' and self.state.player == 'w':
                san.end_square = 'g1'
                san.from_hints = 'e1'
            elif san.castling == 'Q' and self.state.player == 'w':
                san.end_square = 'c1'
                san.from_hints = 'e1'
            elif san.castling == 'K' and self.state.player == 'b':
                san.end_square = 'g8'
                san.from_hints = 'e8'
            elif san.castling == 'Q' and self.state.player == 'b':
                san.end_square = 'c8'
                san.from_hints = 'e8'

        if len(san.from_hints) == 2:
            return san.from_hints + san.end_square

        if not san.piece:
            san.piece = 'P'
        if self.state.player == 'b':
            san.piece = san.piece.lower()

        start_squares = self.board.find_all_pieces(san.piece)
        if len(start_squares) == 1:
            return Game.i2xy(
                start_squares[0]) + san.end_square + san.promotion.lower()

        # If there's more than one piece on the board of this type,
        # we have to determine which ones have a legal move
        rays = MOVES.get(san.piece, [''] * 64)

        for square in start_squares:
            if san.from_hints not in Game.i2xy(square):
                continue

            for ray in rays[square]:
                moves = self._trace_ray(square, san.piece, ray,
                                        self.state.player)
                for move in moves:
                    if san.end_square == move[2:4] and (
                            not san.promotion
                            or san.promotion.lower() == move[-1]):
                        # We found our move, we can just return it
                        return move