Exemplo n.º 1
0
    def get_input(self, msg: str) -> Point:
        '''Prompts for input from the user. Parses input'''
        piece_location, move_to = '', ''

        while True:
            user_input = input(msg)

            if user_input in ('Quit', 'quit', 'q', 'Q'):
                raise UserQuitMidGameException

            if user_input == 'interact':
                breakpoint()

            for delim in self.INPUT_DELIMS:
                if delim in user_input:
                    try:
                        piece_location, move_to = user_input.split(delim)
                    except ValueError:
                        print(self.INVALID_INPUT_MSG)
                        continue

            if not piece_location or not move_to:
                print(self.INVALID_INPUT_MSG)
                continue

            piece_location = Point.from_str(piece_location)
            move_to = Point.from_str(move_to)

            if not self.validate_input(piece_location, move_to):
                continue
            break

        return piece_location, move_to
Exemplo n.º 2
0
    def parse_move(cls, move: str, color: Color) -> Move:
        og_move = move
        if '+' in move:
            move = move.replace('+', '')

        if '#' in move:
            move = move[:-1]

        upgrade = None
        if '=' in move:
            upgrade = cls._PIECE_MAP[move[-1]]
            move = move[:-2]

        en_passant = False
        if '(ep)' in move:
            en_passant = True
            move = move[:-4]

        action = ''
        destination = None
        castle = None
        col_helper = None
        row_helper = None

        if len(move) == 2:
            piece = PieceType.Pawn
            destination = Point.from_str(move)
            action = ' moves to '
        elif move in ('0-0', 'O-O'):
            piece = PieceType.King
            castle = Castle.Kingside
        elif move in ('0-0-0', 'O-O-O'):
            piece = PieceType.King
            castle = Castle.Queenside
        else:
            post = move.split('x')
            if len(post) == 1:
                action = ' moves to '
                prefix, dest_str = move[1:-2], move[-2:]
            else:
                action = ' captures piece on '
                prefix, dest_str = post
                prefix = prefix[1:]

            piece = cls._PIECE_MAP.get(move[0], PieceType.Pawn)
            destination = Point.from_str(dest_str)

            if len(prefix) == 0:
                pass
            elif len(prefix) == 1:
                if prefix[0] in tuple('abcdefgh'):
                    col_helper = prefix.upper()
                else:
                    row_helper = int(prefix)
            elif len(prefix) == 2:
                col_helper = prefix[0].upper()
                row_helper = int(prefix[1])
            else:
                raise UnknownMove(move)

        if en_passant:
            action = ' en passants to '

        return Move(
            move=og_move,
            color=color,
            piece=piece,
            col_helper=col_helper,
            row_helper=row_helper,
            destination=destination,
            en_passant=en_passant,
            upgrade=upgrade,
            castle=castle,
            action=action,
        )
Exemplo n.º 3
0
    def start_game(self,
                   initial_moves: tp.Union[None, Parser,
                                           tp.List[tp.Tuple[str,
                                                            str]]] = None):
        game_status: Status = Ref(Status.InProgress)
        competitive_ending: Ref[tp.Optional[CompetitiveRulesetEndings]] = Ref(
            None)

        if initial_moves is not None:
            if isinstance(initial_moves, Parser):
                for white_move, black_move in tqdm(initial_moves.yield_moves(),
                                                   total=8849):
                    piece, w_destination, w_promotion = self.board.parse_points_from_move(
                        white_move)
                    self.board.perform_move(piece,
                                            w_destination,
                                            promotion=w_promotion)

                    self._reset_after_turn(game_status)

                    if black_move is None:
                        break
                    piece, b_destination, b_promotion = self.board.parse_points_from_move(
                        black_move)
                    self.board.perform_move(piece,
                                            b_destination,
                                            promotion=b_promotion)
                    self._reset_after_turn(game_status)

            else:
                for move1, move2 in tqdm(initial_moves):
                    destination = Point.from_str(move2)
                    self.board.perform_move(self.board[move1],
                                            destination,
                                            promotion=PieceType.Queen)

                    self._reset_after_turn(game_status)

        while self._game_not_finished(game_status, competitive_ending):
            print(self.board)
            #print(f'White Score: {self.get_score(Color.White)}')
            #print(f'Black Score: {self.get_score(Color.Black)}')

            print(f"{self.current_team.value} team's turn.")

            try:
                piece_location, move_to = self.get_input(
                    'Please input your move: ')
            except UserQuitMidGameException as e:
                print(e)
                return game_status, competitive_ending

            piece = self.board[piece_location]

            # Cache values before move
            white_check = self.board.get_king(Color.White).in_check
            black_check = self.board.get_king(Color.Black).in_check

            self.board.perform_move(piece, move_to)

            self.output_check_status_updates(white_check, black_check)

            self._reset_after_turn(game_status)

        self.display_game_ending(game_status, competitive_ending)

        return game_status, competitive_ending