コード例 #1
0
def update_board(updatedBoard: connectfour.GameState, user_input:str) -> connectfour.GameState:
    '''updates board so game state is always current
    '''
    valid_column_numbers = '1234567'
    if user_input[0:4].upper() == "DROP":
        try:
           if user_input[-1] not in valid_column_numbers:
               connectfour.InvalidMoveError(Exception)
               print('Error, try again')
               new_input = read_game_command()
               return update_board(updatedBoard, new_input)
           new_board =  connectfour.drop(updatedBoard, int(user_input[-1])-1)
           return(new_board)
        except connectfour.InvalidMoveError:
            print('Error, try again')
            new_input = read_game_command()
            return update_board(updatedBoard, new_input)
    elif user_input[0:3].upper() == "POP":
        try:
            if user_input[-1] not in valid_column_numbers:
               connectfour.InvalidMoveError(Exception)
               print('Error, try again')
               new_input = read_game_command()
               return update_board(updatedBoard, new_input)
            new_board =  connectfour.pop(updatedBoard, int(user_input[-1])-1)
            return(new_board)
        except connectfour.InvalidMoveError:
            print('Error, try again')
            new_input = read_game_command()
            return update_board(updatedBoard, new_input)
    else:
      print('Error, try again')
      new_input = read_game_command()
      return update_board(updatedBoard,new_input)
コード例 #2
0
def move():
    ''' Asks the user for a command and checks if the move is valid
    '''
    while True:
        try:
            print("Drop: Place a disc in the designated column (EX: DROP 1)")
            print(
                "Pop: Remove one of your discs at the bottom of a column (EX: POP 1)"
            )
            print()
            action = input('What would you like to do?\n')
            action = action.upper().split()

            if len(action) != 2:
                raise connectfour.InvalidMoveError()

            elif not action[0].startswith('DROP') and not action[0].startswith(
                    'POP'):
                raise connectfour.InvalidMoveError()

            elif int(action[1]) > 7 or int(action[1]) < 1:
                raise connectfour.InvalidMoveError()

            return PlayerCommand(command=action[0], column=(action[1]))
        except:
            print()
            print('Invalid Move. Please try again.\n')
コード例 #3
0
def _read_line_check_invalid_and_winner(connection: ServerConnection) -> str:
    '''After server line received, check if invalid or winner and raise
    and exception'''
    line_received = _read_line(connection)
    print("SERVER: ", line_received)
    if line_received == 'INVALID':
        raise connectfour.InvalidMoveError()
    elif (line_received == 'WINNER_RED') or (line_received == 'WINNER_YELLOW'):
        raise connectfour.GameOverError()
    return line_received
コード例 #4
0
def execute_move(state: lib.GameState, col: int, move: str) -> lib.GameState:
    """
    Executes the give move on the given column for the given GameState
    :param state: The GameState that this move will be executed on
    :param col: The column that this move will happen on
    :param move: The move that will be executed
    :return: A new GameState with the results of the executed move
    """
    if move == DROP_TOP:
        return lib.drop(state, col - 1)
    elif move == POP_BOTTOM:
        return lib.pop(state, col - 1)
    else:
        raise lib.InvalidMoveError()