0
\$\begingroup\$

Write a function make_human_move(current_player, board), which does the following:

  1. Print out the player's shape (e.g. if the player shape is 'X', the function prints "X's move").
  2. Asks the human player for his/her move. The message asked is "Enter row and column [0 - 2]: ".
  3. A move consists of two integer values separated by a space (e.g. '0 0'). You can assume the input will always be two integer values separated by a space.
  4. The function checks if the move is valid or not.
    a) A valid move is when the integer value is between 0 and 2 for both row and column values.
    b) It also makes sure that a valid location on the board is empty (i.e. currently an empty space character ' ').
  5. If a move is invalid, the function prints "Illegal move. Try again." and repeats from step 2.
  6. Once a valid move is entered, the board state is updated.

This is how I am doing it right now.

def make_human_move(current_player, board):
    """Given a board state and the human piece ('O' or 'X')
       ask the player for a location to play in. Repeat until a
       valid response is given. Then make the move, i.e., update the board by setting the chosen cell to the player's piece.       
    """
    print(current_player + "'s move")
    user_input = input("Enter row and column [0 - 2]: ")
    input_list = user_input.split()
    while True:
        restart = False
        for i in input_list:
            if (int(i) > 2 or int(i) < 0) or (board[int(input_list[0])][int(input_list[1])] != " "):
                print("Illegal move. Try again.")
                user_input = input("Enter row and column [0 - 2]: ")
                input_list = user_input.split()
                restart = True
                break
        if restart:
            continue
        else:
            board[int(input_list[0])][int(input_list[1])] = current_player
            break
\$\endgroup\$
2
  • 2
    \$\begingroup\$ The current question title of your question is too generic to be helpful. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$
    – BCdotWEB
    Commented Sep 23, 2020 at 15:40
  • 1
    \$\begingroup\$ Why would you use recursion for this? \$\endgroup\$
    – user985366
    Commented Sep 23, 2020 at 19:12

1 Answer 1

2
\$\begingroup\$

Here's my attempt. The function make_human_move just delegates the actual work to a recursive function. The function guard_range simply checks if the given argument (which must be an int) is in the expected range, and if not, it raise an exception. In the interesting part of the code, which is the recursive function make_human_move_rec, I used exceptions mainly because this way I'm able to deal with malformed input from the user (for instance, if the user inputs 0 z, the int(input_list[1]) will raise an exception).

def make_human_move(current_player, board):
    print(current_player + "'s move")
    make_human_move_rec(current_player, board)

def guard_range(x):
    if x > 2 or x < 0: raise
    return x

def make_human_move_rec(current_player, board):
    user_input = input("Enter row and column [0 - 2]: ")
    input_list = user_input.split()
    try:
        # There are three ways the following lines can raise an exception: the
        # input is not formatted correctly, it's not a number, or the input is
        # out of range.
        input_row = guard_range(int(input_list[0]))
        input_col = guard_range(int(input_list[1]))

        # Also raise an exception if the board is occupied at the destination.
        if board[input_row][input_col] != " ":
            raise

        # If we get here, then the move is valid. We do our thing.
        board[input_row][input_col] = current_player
    except:
        print("Illegal move. Try again.")
        make_human_move_rec(current_player, board)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.