Ejemplo n.º 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.')
Ejemplo n.º 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.')
Ejemplo n.º 3
0
import builtins

# Get the initial value of the constant
constant_before = [tictactoe_functions.EMPTY]

# Type check and simple test for tictactoe_functions.is_between
result = tictactoe_functions.is_between(2, 1, 3)
assert isinstance(result, bool), \
       'tictactoe_functions.is_between should return a bool, but returned ' \
       '{0}'.format(type(result))
assert result, \
       'tictactoe_functions.is_between(2, 1, 3) should return True, but ' \
       'returned {0}.'.format(result)

# Type check and simple test for tictactoe_functions.game_board_full
result = tictactoe_functions.game_board_full('----')
assert isinstance(result, bool), \
       'tictactoe_functions.game_board_full should return a bool, but ' \
       '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)
Ejemplo n.º 4
0
import builtins

# Get the initial value of the constant
constant_before = [tictactoe_functions.EMPTY]

# Type check and simple test for tictactoe_functions.is_between
result = tictactoe_functions.is_between(2, 1, 3)
assert isinstance(result, bool), \
       'tictactoe_functions.is_between should return a bool, but returned ' \
       '{0}'.format(type(result))
assert result, \
       'tictactoe_functions.is_between(2, 1, 3) should return True, but ' \
       'returned {0}.'.format(result)

# Type check and simple test for tictactoe_functions.game_board_full
result = tictactoe_functions.game_board_full('----')
assert isinstance(result, bool), \
       'tictactoe_functions.game_board_full should return a bool, but ' \
       '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)