Ejemplo n.º 1
0
def _run(*args):
    idx, p1, p2, moves = args[0]
    game = Board(p1, p2)
    for m in moves:
        game.apply_move(m)
    winner, move_history, termination = game.play(time_limit=TIME_LIMIT)
    return (idx, winner == p1, len(move_history)), termination
Ejemplo n.º 2
0
def play_round(cpu_agent, test_agents, win_counts, num_matches):
    """Compare the test agents to the cpu agent in "fair" matches.

    "Fair" matches use random starting locations and force the agents to
    play as both first and second player to control for advantages resulting
    from choosing better opening moves or having first initiative to move.
    """
    timeout_count = 0
    forfeit_count = 0
    move_count = 0
    pool = Pool(NUM_PROCS)

    for _ in range(num_matches):

        # initialize all games with a random move and response
        init_moves = []
        init_game = Board("p1", "p2")
        for _ in range(2):
            move = random.choice(init_game.get_legal_moves())
            init_moves.append(move)
            init_game.apply_move(move)

        games = sum([[(2 * i, cpu_agent.player, agent.player, init_moves),
                      (2 * i + 1, agent.player, cpu_agent.player, init_moves)]
                    for i, agent in enumerate(test_agents)], [])

        # play all games and tally the results
        for result, termination in pool.imap_unordered(_run, games):
            game = games[result[0]]
            winner = game[1] if result[1] else game[2]
            move_count += result[2]

            win_counts[winner] += 1

            if termination == "timeout":
                timeout_count += 1
            elif winner not in test_agents and termination == "forfeit":
                forfeit_count += 1

    return timeout_count, forfeit_count, move_count
Ejemplo n.º 3
0
        return legal_moves[index]


if __name__ == "__main__":
    from isolation import Board

    # create an isolation board (by default 7x7)
    player1 = RandomPlayer()
    player2 = GreedyPlayer()
    game = Board(player1, player2)

    # place player 1 on the board at row 2, column 3, then place player 2 on
    # the board at row 0, column 5; display the resulting board state.  Note
    # that .apply_move() changes the calling object
    game.apply_move((2, 3))
    game.apply_move((0, 5))
    print(game.to_string())

    # players take turns moving on the board, so player1 should be next to move
    assert (player1 == game.active_player)

    # get a list of the legal moves available to the active player
    print(game.get_legal_moves())

    # get a successor of the current state by making a copy of the board and
    # applying a move. Notice that this does NOT change the calling object
    # (unlike .apply_move()).
    new_game = game.forecast_move((1, 1))
    assert (new_game.to_string() != game.to_string())
    print("\nOld state:\n{}".format(game.to_string()))
Ejemplo n.º 4
0
        return legal_moves[index]


if __name__ == "__main__":
    from isolation import Board

    # create an isolation board (by default 7x7)
    player1 = RandomPlayer()
    player2 = GreedyPlayer()
    game = Board(player1, player2)

    # place player 1 on the board at row 2, column 3, then place player 2 on
    # the board at row 0, column 5; display the resulting board state.  Note
    # that the .apply_move() method changes the calling object in-place.
    game.apply_move((2, 3))
    game.apply_move((0, 5))
    print(game.to_string())

    # players take turns moving on the board, so player1 should be next to move
    assert(player1 == game.active_player)

    # get a list of the legal moves available to the active player
    print(game.get_legal_moves())

    # get a successor of the current state by making a copy of the board and
    # applying a move. Notice that this does NOT change the calling object
    # (unlike .apply_move()).
    new_game = game.forecast_move((1, 1))
    assert(new_game.to_string() != game.to_string())
    print("\nOld state:\n{}".format(game.to_string()))
Ejemplo n.º 5
0

if __name__ == "__main__":
    from isolation import Board

    # create an isolation board (by default 7x7)
    #player1 = RandomPlayer()
    #player2 = MinimaxPlayer()
    player2 = AlphaBetaPlayer()
    player1 = GreedyPlayer()
    game = Board(player1, player2)

    # place player 1 on the board at row 2, column 3, then place player 2 on
    # the board at row 0, column 5; display the resulting board state.  Note
    # that the .apply_move() method changes the calling object in-place.
    game.apply_move((2, 2))
    game.apply_move((2, 1))
    print(game.to_string())

    # players take turns moving on the board, so player1 should be next to move
    assert (player1 == game.active_player)

    # get a list of the legal moves available to the active player
    print(game.get_legal_moves())
    '''
    # get a successor of the current state by making a copy of the board and
    # applying a move. Notice that this does NOT change the calling object
    # (unlike .apply_move()).
    new_game = game.forecast_move((1, 1))
    assert(new_game.to_string() != game.to_string())
    print("\nOld state:\n{}".format(game.to_string()))
Ejemplo n.º 6
0
if __name__ == "__main__":
    from isolation import Board
    from game_agent import CustomPlayer

    for i in range(10):
        # create an isolation board (by default 7x7)
        player1 = CustomPlayer(method='alphabeta')
        player2 = CustomPlayer(method='alphabeta', score_fn=improved_score)
        game = Board(player1, player2)

        # place player 1 on the board at row 2, column 3, then place player 2 on
        # the board at row 0, column 5; display the resulting board state.  Note
        # that .apply_move() changes the calling object
        board_positions = [(a, b) for b in range(7) for a in range(7)]
        game.apply_move(
            board_positions.pop(randint(0,
                                        len(board_positions) - 1)))
        game.apply_move(
            board_positions.pop(randint(0,
                                        len(board_positions) - 1)))
        print(game.to_string())

        # players take turns moving on the board, so player1 should be next to move
        assert (player1 == game.active_player)

        # get a list of the legal moves available to the active player
        # print(game.get_legal_moves())

        # get a successor of the current state by making a copy of the board and
        # applying a move. Notice that this does NOT change the calling object
        # (unlike .apply_move()).
        return legal_moves[index]


if __name__ == "__main__":
    from isolation import Board

    # create an isolation board (by default 7x7)
    player1 = GreedyPlayer()
    player2 = AlphaBetaPlayer()
    game = Board(player1, player2)

    # place player 1 on the board at row 2, column 3, then place player 2 on
    # the board at row 0, column 5; display the resulting board state.  Note
    # that the .apply_move() method changes the calling object in-place.
    game.apply_move((0, 1))
    game.apply_move((0, 5))
    print(game.to_string())

    # players take turns moving on the board, so player1 should be next to move
    assert (player1 == game.active_player)

    # get a list of the legal moves available to the active player
    # print("The legal moves for the current player at this point are:", game.get_legal_moves())

    # Get all the payoffs of each move for that player
    # print("The payoffs for Player 1 at this point are:", player1.get_move(game, 0.0))

    # get a successor of the current state by making a copy of the board and
    # applying a move. Notice that this does NOT change the calling object
    # (unlike .apply_move()).
Ejemplo n.º 8
0
from isolation import Board
from sample_players import (RandomPlayer, open_move_score, improved_score,
                            center_score)
from game_agent import (MinimaxPlayer, AlphaBetaPlayer, custom_score,
                        custom_score_2, custom_score_3)

Agent = namedtuple("Agent", ["player", "name"])

# introduce the test agent and cpu agent
test_agent = Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved")
cpu_agent = Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved2")
# cpu_agent = Agent(RandomPlayer(), "Random")

#NUM_MATCHES = 1  # number of matches against each opponent

TIME_LIMIT = 150  # number of milliseconds before timeout

game = Board(cpu_agent.player, test_agent.player)

# initialize all games with a random move and response
for _ in range(2):
    move = random.choice(game.get_legal_moves())
    game.apply_move(move)

winner, history, termination = game.play(time_limit=TIME_LIMIT)

print("winner:", winner)
print("history:", history)
print("termination:", termination)
# note that winner will be displayed in green while the lowser is in red
    for i in range(5):
        width = config[i][0]
        height = config[i][1]
        p1_wins = 0
        p2_wins = 0
        p3_wins = 0

        p1_win_arr = []
        p2_win_arr = []
        p3_win_arr = []

        for _ in range(game_num):
            game = Board(player1, player2, player3, width, height)
            m1, m2, m3 = random.sample(range(0, width * height), 3)

            game.apply_move((m1 // width, m1 % width))
            game.apply_move((m2 // width, m2 % width))
            game.apply_move((m3 // width, m3 % width))

            winners, history, outcome = game.play()

            print("\nWinner: {}\nOutcome: {}".format(winners[0], outcome))
            print("\nWinner: {}\nOutcome: {}".format(winners[1], outcome))
            print(game.to_string())
            print("Move history:\n{!s}".format(history))

            if (id(winners[0]) == id(player1)) or (id(winners[1])
                                                   == id(player1)):
                p1_wins += 1

            p1_win_arr.append(p1_wins)