コード例 #1
0
    def in_board(self, coordinate: Coordinate) -> bool:
        """
        returns whether a coordinate is within the bounds of the board

        arguments:
        coordinate -- coordinate to check
        """

        return (coordinate.get_rank() >= 0 and coordinate.get_file() >= 0
                and coordinate.get_rank() < self.HEIGHT
                and coordinate.get_file() < self.WIDTH)
コード例 #2
0
    def get_diagonal_moves(self, piece: Piece,
                           board: List[List[Piece]]) -> List[Move]:
        """
        returns a list of all diagonal moves

        arguments:
        piece -- piece to generate moves
        board -- board to validate moves
        """

        if piece.piece_type != Type.BISHOP and piece.piece_type != Type.QUEEN:
            return []

        directions: List[Tuple[int, int]] = [(1, 1), (-1, 1), (-1, -1),
                                             (1, -1)]
        moves: List[Move] = []

        for direction in directions:
            destination: Coordinate = Coordinate(
                piece.coordinate.get_rank() + direction[0],
                piece.coordinate.get_file() + direction[1],
            )
            while self.in_board(destination):
                moves.append(
                    Move(piece.coordinate, destination,
                         self.piece_at(destination, board)))
                if self.piece_at(destination, board):
                    break
                destination = Coordinate(
                    destination.get_rank() + direction[0],
                    destination.get_file() + direction[1],
                )

        return moves
コード例 #3
0
    def piece_at(self, coordinate: Coordinate,
                 board: List[List[Piece]]) -> Union[Piece, None]:
        """
        returns a reference to the piece at a specified coordinate, if no piece is present returns None

        arguments:
        coordinate -- coordinate to check
        board -- board to check for piece in
        """

        if not self.in_board(coordinate):
            return None
        else:
            return board[coordinate.get_rank()][coordinate.get_file()]