Example #1
0
def play_tictactoe():
    """ () -> None

    Play a single game of tic-tac-toe, with one player being the program
    user and the other player being this computer program.

    (No docstring example given since the function indirectly depends on either
    user input or randomly generated numbers.)
    """

    # Initialize the game setup.
    board_size = get_game_size()
    game_board = tictactoe_functions.make_empty_board(board_size)

    print('\nYou are using symbol ' + SYMBOLS[HUMAN] + ' and the computer '
          + 'program is using symbol ' + SYMBOLS[COMPUTER] + '.')
    print(format_game_board(game_board))

    # Play the game until a player wins or there is a draw.
    is_human_move = False
    have_a_winner = False
    while (not have_a_winner and 
           not tictactoe_functions.game_board_full(game_board)):

        is_human_move = not is_human_move
        (row,col) = get_move(is_human_move, game_board)
        if is_human_move:
            player_symbol = SYMBOLS[HUMAN]
            print('\nYou chose row ' + str(row) + ' and column '
                  + str(col) + '.')
        else:
            player_symbol = SYMBOLS[COMPUTER]
            print('The computer program then chose row ' + str(row)
                  + ' and column ' + str(col) + '.')

        game_board = tictactoe_functions.make_move(
                         player_symbol, row, col, game_board)

        if not is_human_move:
            print(format_game_board(game_board))

        have_a_winner = game_won(game_board, player_symbol)
      

    if have_a_winner:
        print('We have a winner!')
        if is_human_move:
            print(format_game_board(game_board))
            print('You beat the computer program! Congratulations!')
        else:
            print('The computer program won!') 
            print('Re-think your strategy and try again.')
    else:
        print(format_game_board(game_board))
        print('The game has played to a draw.')
        print('Re-think your strategy and try again.')
Example #2
0
def play_tictactoe():
    """ () -> None

    Play a single game of tic-tac-toe, with one player being the program
    user and the other player being this computer program.

    (No docstring example given since the function indirectly depends on either
    user input or randomly generated numbers.)
    """

    # Initialize the game setup.
    board_size = get_game_size()
    game_board = tictactoe_functions.make_empty_board(board_size)

    print('\nYou are using symbol ' + SYMBOLS[HUMAN] + ' and the computer ' +
          'program is using symbol ' + SYMBOLS[COMPUTER] + '.')
    print(format_game_board(game_board))

    # Play the game until a player wins or there is a draw.
    is_human_move = False
    have_a_winner = False
    while (not have_a_winner
           and not tictactoe_functions.game_board_full(game_board)):

        is_human_move = not is_human_move
        (row, col) = get_move(is_human_move, game_board)
        if is_human_move:
            player_symbol = SYMBOLS[HUMAN]
            print('\nYou chose row ' + str(row) + ' and column ' + str(col) +
                  '.')
        else:
            player_symbol = SYMBOLS[COMPUTER]
            print('The computer program then chose row ' + str(row) +
                  ' and column ' + str(col) + '.')

        game_board = tictactoe_functions.make_move(player_symbol, row, col,
                                                   game_board)

        if not is_human_move:
            print(format_game_board(game_board))

        have_a_winner = game_won(game_board, player_symbol)

    if have_a_winner:
        print('We have a winner!')
        if is_human_move:
            print(format_game_board(game_board))
            print('You beat the computer program! Congratulations!')
        else:
            print('The computer program won!')
            print('Re-think your strategy and try again.')
    else:
        print(format_game_board(game_board))
        print('The game has played to a draw.')
        print('Re-think your strategy and try again.')
Example #3
0
       'returned {0}.'.format(type(result))
assert not result, \
       "tictactoe_functions.game_board_full('----') should return False, " \
       'but returned {0}.'.format(result)

# Type check and simple test for tictactoe_functions.get_board_size
result = tictactoe_functions.get_board_size('-')
assert isinstance(result, int), \
       'tictactoe_functions.get_board_size should return an int, but ' \
       'returned {0} .'.format(type(result))
assert result == 1, \
       "tictactoe_functions.get_board_size('-') should return 1, but "\
       'returned {0}.'.format(result)

# Type check and simple test for tictactoe_functions.make_empty_board
result = tictactoe_functions.make_empty_board(1)
assert isinstance(result, str), \
       'tictactoe_functions.make_empty_board should return a str, but ' \
       'returned {0}.'.format(type(result))
assert result == constant_before[0], \
       "tictactoe_functions.make_empty_board(1) should return '" \
       + constant_before[0] + "', but returned '{0}'.".format(result)

# Type check and simple test for tictactoe_functions.get_position
result = tictactoe_functions.get_position(1, 1, 3)
assert isinstance(result, int), \
       'tictactoe_functions.get_position should return an int, but returned ' \
       '{0}.'.format(type(result))
assert result == 0, \
       'tictactoe_functions.get_position(1, 1, 3) should return 0, but ' \
       'returned {0}.'.format(result)
Example #4
0
       'returned {0}.'.format(type(result))
assert not result, \
       "tictactoe_functions.game_board_full('----') should return False, " \
       'but returned {0}.'.format(result)

# Type check and simple test for tictactoe_functions.get_board_size
result = tictactoe_functions.get_board_size('-')
assert isinstance(result, int), \
       'tictactoe_functions.get_board_size should return an int, but ' \
       'returned {0} .'.format(type(result))
assert result == 1, \
       "tictactoe_functions.get_board_size('-') should return 1, but "\
       'returned {0}.'.format(result)

# Type check and simple test for tictactoe_functions.make_empty_board
result = tictactoe_functions.make_empty_board(1)
assert isinstance(result, str), \
       'tictactoe_functions.make_empty_board should return a str, but ' \
       'returned {0}.'.format(type(result))
assert result == constant_before[0], \
       "tictactoe_functions.make_empty_board(1) should return '" \
       + constant_before[0] + "', but returned '{0}'.".format(result)

# Type check and simple test for tictactoe_functions.get_position
result = tictactoe_functions.get_position(1, 1, 3)
assert isinstance(result, int), \
       'tictactoe_functions.get_position should return an int, but returned ' \
       '{0}.'.format(type(result))
assert result == 0, \
       'tictactoe_functions.get_position(1, 1, 3) should return 0, but ' \
       'returned {0}.'.format(result)