Пример #1
0
def test_pretty_print_board():

    test_board = cm.initialize_game_state()
    test_board = np.random.randint(0, 3, test_board.shape)
    print('')
    print(test_board)

    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)

    assert isinstance(board_str, str)
    return
Пример #2
0
def test_string_to_board():

    test_board = cm.initialize_game_state()
    test_board = np.random.randint(0, 3, test_board.shape)
    print('')
    print(test_board)
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    board_arr = cm.string_to_board(board_str)
    print('')
    print(board_arr)

    assert isinstance(test_board, np.ndarray)
    assert board_arr.dtype == np.int8
    assert board_arr.shape == test_board.shape
    return
Пример #3
0
def test_check_end_state():

    test_board = cm.initialize_game_state()
    test_board[:3, ::2] = cm.PLAYER1
    test_board[3:, 1::2] = cm.PLAYER1
    test_board[:3, 1::2] = cm.PLAYER2
    test_board[3:, ::2] = cm.PLAYER2
    print('')
    print(cm.pretty_print_board(test_board))
    assert cm.check_end_state(test_board, cm.PLAYER1) == \
        cm.GameState.IS_DRAW

    test_board[0, 1] = cm.PLAYER1
    test_board[1, 2] = cm.PLAYER1
    test_board[2, 3] = cm.PLAYER1
    test_board[3, 4] = cm.PLAYER1
    assert cm.check_end_state(test_board, cm.PLAYER1) == \
        cm.GameState.IS_WIN

    test_board[-1, -1] = cm.NO_PLAYER
    assert cm.check_end_state(test_board, cm.PLAYER1) == \
        cm.GameState.IS_WIN
Пример #4
0
def test_top_row():

    test_board = cm.initialize_game_state()
    test_board[0, 0] = cm.PLAYER1
    test_board[:6, 1] = cm.PLAYER1
    test_board[:2, 2] = cm.PLAYER1
    test_board[:3, 3] = cm.PLAYER1
    test_board[:4, 4] = cm.PLAYER1

    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)

    assert cm.top_row(test_board, cm.PlayerAction(0)) == 1
    assert cm.top_row(test_board, cm.PlayerAction(2)) == 2
    assert cm.top_row(test_board, cm.PlayerAction(3)) == 3
    assert cm.top_row(test_board, cm.PlayerAction(4)) == 4
    assert cm.top_row(test_board, cm.PlayerAction(5)) == 0
    assert cm.top_row(test_board, cm.PlayerAction(6)) == 0
    try:
        assert not cm.top_row(test_board, cm.PlayerAction(1))
    except IndexError:
        assert True
Пример #5
0
def test_apply_player_action():

    test_board1_old = cm.initialize_game_state()
    test_board2 = cm.initialize_game_state()

    test_board1_new = cm.apply_player_action(test_board1_old,
                                             cm.PlayerAction(3), cm.PLAYER1,
                                             True)
    new_row = cm.top_row(test_board1_new, cm.PlayerAction(3))
    old_row = cm.top_row(test_board1_old, cm.PlayerAction(3))
    print('')
    print(cm.pretty_print_board(test_board1_old))
    print('')
    print(cm.pretty_print_board(test_board1_new))
    assert not np.all(test_board1_old == test_board1_new)
    assert new_row != old_row
    test_board1_old = test_board1_new

    test_board1_new = cm.apply_player_action(test_board1_old,
                                             cm.PlayerAction(3), cm.PLAYER2,
                                             True)
    new_row = cm.top_row(test_board1_new, cm.PlayerAction(3))
    old_row = cm.top_row(test_board1_old, cm.PlayerAction(3))
    assert not np.all(test_board1_old == test_board1_new)
    assert new_row != old_row
    test_board1_old = test_board1_new

    test_board1_new = cm.apply_player_action(test_board1_old,
                                             cm.PlayerAction(3), cm.PLAYER1,
                                             True)
    new_row = cm.top_row(test_board1_new, cm.PlayerAction(3))
    old_row = cm.top_row(test_board1_old, cm.PlayerAction(3))
    assert not np.all(test_board1_old == test_board1_new)
    assert new_row != old_row
    test_board1_old = test_board1_new

    test_board1_new = cm.apply_player_action(test_board1_old,
                                             cm.PlayerAction(3), cm.PLAYER2,
                                             True)
    new_row = cm.top_row(test_board1_new, cm.PlayerAction(3))
    old_row = cm.top_row(test_board1_old, cm.PlayerAction(3))
    assert not np.all(test_board1_old == test_board1_new)
    assert new_row != old_row
    test_board1_old = test_board1_new

    test_board1_new = cm.apply_player_action(test_board1_old,
                                             cm.PlayerAction(3), cm.PLAYER1,
                                             True)
    new_row = cm.top_row(test_board1_new, cm.PlayerAction(3))
    old_row = cm.top_row(test_board1_old, cm.PlayerAction(3))
    assert not np.all(test_board1_old == test_board1_new)
    assert new_row != old_row
    test_board1_old = test_board1_new

    test_board1_new = cm.apply_player_action(test_board1_old,
                                             cm.PlayerAction(3), cm.PLAYER2,
                                             True)
    try:
        new_row = cm.top_row(test_board1_new, cm.PlayerAction(3))
    except IndexError:
        pass
    old_row = cm.top_row(test_board1_old, cm.PlayerAction(3))
    assert not np.all(test_board1_old == test_board1_new)
    assert new_row == old_row
    test_board1_old = test_board1_new

    try:
        test_board1_new = cm.apply_player_action(test_board1_old,
                                                 cm.PlayerAction(3),
                                                 cm.PLAYER1, True)
    except IndexError:
        assert True
    assert np.all(test_board1_old == test_board1_new)
    # test_board1_old = test_board1_new

    test_board2[0, 3] = cm.PLAYER1
    test_board2[1, 3] = cm.PLAYER2
    test_board2[2, 3] = cm.PLAYER1
    test_board2[3, 3] = cm.PLAYER2
    test_board2[4, 3] = cm.PLAYER1
    test_board2[5, 3] = cm.PLAYER2
    assert np.all(test_board1_new == test_board2)
Пример #6
0
def test_connect_four():
    # Test a diagonal
    test_board = cm.initialize_game_state()
    test_board[0, 3] = cm.PLAYER1
    test_board[1, 4] = cm.PLAYER1
    test_board[2, 5] = cm.PLAYER1
    test_board[3, 6] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1)

    # Test the same diagonal for the other player
    test_board = cm.initialize_game_state()
    test_board[0, 3] = cm.PLAYER2
    test_board[1, 4] = cm.PLAYER2
    test_board[2, 5] = cm.PLAYER2
    test_board[3, 6] = cm.PLAYER2
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER2)

    # Rotate the diagonal
    test_board = cm.initialize_game_state()
    test_board[0, 6] = cm.PLAYER1
    test_board[1, 5] = cm.PLAYER1
    test_board[2, 4] = cm.PLAYER1
    test_board[3, 3] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1)

    # Test a row at the top
    test_board = cm.initialize_game_state()
    test_board[-1, 3:] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1)

    # Test a row at the bottom
    test_board = cm.initialize_game_state()
    test_board[0, :4] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1)

    # Test a column on the left
    test_board = cm.initialize_game_state()
    test_board[2:6, 0] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1)

    # Test a column on the right
    test_board = cm.initialize_game_state()
    test_board[:4, 6] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1)

    # Test some things in the middle
    test_board = cm.initialize_game_state()
    test_board[2, 5] = cm.PLAYER1
    test_board[3, 4] = cm.PLAYER1
    test_board[4, 3] = cm.PLAYER1
    test_board[5, 2] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1)

    test_board = cm.initialize_game_state()
    test_board[0, 2] = cm.PLAYER1
    test_board[1, 3] = cm.PLAYER1
    test_board[2, 4] = cm.PLAYER1
    test_board[3, 5] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1)

    # Test the last_action option for column 6
    test_board = cm.initialize_game_state()
    test_board[0, 3] = cm.PLAYER1
    test_board[1, 4] = cm.PLAYER1
    test_board[2, 5] = cm.PLAYER1
    test_board[3, 6] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1, cm.PlayerAction(6))

    # Test the last_action option for column 5
    test_board = cm.initialize_game_state()
    test_board[0, 2] = cm.PLAYER1
    test_board[1, 3] = cm.PLAYER1
    test_board[2, 4] = cm.PLAYER1
    test_board[3, 5] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1, cm.PlayerAction(5))

    # Test the last_action option for column 1
    test_board = cm.initialize_game_state()
    test_board[:4, 0] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1, cm.PlayerAction(0))

    # Test the last_action option for column 2 diagonal pattern
    test_board = cm.initialize_game_state()
    test_board[0, 1] = cm.PLAYER1
    test_board[1, 2] = cm.PLAYER1
    test_board[2, 3] = cm.PLAYER1
    test_board[3, 4] = cm.PLAYER1
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert cm.connect_four(test_board, cm.PLAYER1, cm.PlayerAction(2))

    # Test the last_action option for column 2 diagonal pattern
    test_board = cm.initialize_game_state()
    test_board[:3, 2] = cm.PLAYER1
    test_board[0, 3] = cm.PLAYER1
    test_board[2, 3] = cm.PLAYER1
    test_board[1, 5] = cm.PLAYER1
    test_board[4, 2] = cm.PLAYER1
    test_board[5, 2:4] = cm.PLAYER1
    test_board[3, 5] = cm.PLAYER1
    test_board[0, 1] = cm.PLAYER2
    test_board[1, 3:5] = cm.PLAYER2
    test_board[0, 4:] = cm.PLAYER2
    test_board[2, 5] = cm.PLAYER2
    test_board[4, 3] = cm.PLAYER2
    test_board[3, 2:4] = cm.PLAYER2
    board_str = cm.pretty_print_board(test_board)
    print('')
    print(board_str)
    assert not cm.connect_four(test_board, cm.PLAYER1, cm.PlayerAction(2))
Пример #7
0
def human_vs_agent(generate_move_1: GenMove,
                   generate_move_2: GenMove = user_move,
                   player_1: str = "Player 1",
                   player_2: str = "Player 2",
                   args_1: tuple = (),
                   args_2: tuple = (),
                   init_1: Callable = lambda board, player: None,
                   init_2: Callable = lambda board, player: None):

    import time
    from agents.common_arrays import PLAYER1, PLAYER2, GameState
    from agents.common_arrays import initialize_game_state, pretty_print_board,\
        apply_player_action, check_end_state

    players = (PLAYER1, PLAYER2)

    # Play two games, where each player gets a chance to go first
    for play_first in (1, -1):
        # Initialize a string to store actions
        game_moves_out = ''
        game_moves = ''
        # This loop initializes the variables to speed up computation when
        # using the numba compiler
        for init, player in zip((init_1, init_2)[::play_first], players):
            init(initialize_game_state(), player)

        saved_state = {PLAYER1: None, PLAYER2: None}
        board = initialize_game_state()
        gen_moves = (generate_move_1, generate_move_2)[::play_first]
        player_names = (player_1, player_2)[::play_first]
        gen_args = (args_1, args_2)[::play_first]

        playing = True
        while playing:
            for player, player_name, gen_move, args in zip(
                    players, player_names, gen_moves, gen_args):

                # Time how long a move takes
                t0 = time.time()

                # Inform the player which letter represents them
                print(pretty_print_board(board))
                print(f'{player_name} you are playing with '
                      f'{"X" if player == PLAYER1 else "O"}')

                # Generate an action, either through user input or by an
                # agent function
                action, saved_state[player] = gen_move(board.copy(), player,
                                                       saved_state[player],
                                                       *args)

                print(f"Move time: {time.time() - t0:.3f}s")

                # Save the move
                game_moves_out += str(action)
                game_moves += str(action)
                game_moves += ', '
                # Update the board with the action
                apply_player_action(board, action, player)
                end_state = check_end_state(board, player)

                # Check to see whether the game is a win or draw
                if end_state != GameState.STILL_PLAYING:
                    print(pretty_print_board(board))

                    if end_state == GameState.IS_DRAW:
                        print("Game ended in draw")
                        print(game_moves)
                    else:
                        print(f'{player_name} won playing '
                              f'{"X" if player == PLAYER1 else "O"}')
                        print(game_moves)
                        text_file = open("tests/Test_cases_wins", "a")
                        text_file.write(game_moves_out + '\n')
                        text_file.close()

                    playing = False
                    break