Exemple #1
0
def playSilent(agent_0, agent_1):
	start = time.time()
	lastAction = -1
	pygame.init()
	cur_state = SquadroState()
	agents = [getattr(__import__(agent_0), 'MyAgent')(), getattr(__import__(agent_1), 'MyAgent')()]
	counter = 0
	while not cur_state.game_over():
		counter +=1
		action = agents[cur_state.get_cur_player()].get_action(cur_state, lastAction, False)
		lastAction = action
		cur_state.apply_action(action)
	end = time.time()
	print("Done in", end-start)
	return cur_state.get_winner()
def main():

    cur_state = SquadroState()
    agents = [
        getattr(__import__(agent_0), 'MyAgent')(),
        getattr(__import__(agent_1), 'MyAgent')()
    ]
    agents[0].set_id(0)
    agents[1].set_id(1)

    cur_state.cur_pos[0][0] = (100, 400)
    cur_state.cur_pos[0][1] = (200, 100)
    cur_state.returning[0][1] = True
    cur_state.finished[0][2] = True
    cur_state.finished[0][3] = True
    cur_state.cur_player = 0

    l1 = [
        cur_state.get_pawn_advancement(cur_state.cur_player, pawn)
        for pawn in [0, 1, 2, 3, 4]
    ]
    l2 = [
        cur_state.get_pawn_advancement(1 - cur_state.cur_player, pawn)
        for pawn in [0, 1, 2, 3, 4]
    ]

    #print(l1, l2)

    for i in range(2, 50):
        agents[0].MC_steps = i
        action = agents[0].get_action(cur_state.copy(), 0, 10)

    cur_state.apply_action(action)
Exemple #3
0
def main(agent_0, agent_1, time_out, first):
    # Initialisation
    pygame.init()
    n_tiles = 7
    n_pawns = 5
    board = Board(n_tiles, n_pawns)
    cur_state = SquadroState()
    if first != -1:
        cur_state.cur_player = first
    agents = [
        getattr(__import__(agent_0), 'MyAgent')(),
        getattr(__import__(agent_1), 'MyAgent')()
    ]
    agents[0].set_id(0)
    agents[1].set_id(1)
    times_left = [time_out, time_out]
    last_action = None

    while not cur_state.game_over():
        # Draw board
        board.screen.fill(0)
        board.draw_board(cur_state)
        board.show_turn(cur_state)

        # Update screen
        pygame.display.flip()

        # Make move
        cur_player = cur_state.get_cur_player()
        timer_stop = [False]
        timer = TimerDisplay(board, cur_player, times_left.copy(), timer_stop)
        timer.start()
        try:
            action, exe_time = get_action_timed(agents[cur_player],
                                                cur_state.copy(), last_action,
                                                times_left[cur_player])
            timer_stop[0] = True
            times_left[cur_player] -= exe_time

            if cur_state.is_action_valid(action):
                cur_state.apply_action(action)
                last_action = action
            else:
                cur_state.set_invalid_action(cur_player)

        except TimeoutError:
            timer_stop[0] = True
            cur_state.set_timed_out(cur_player)

        timer.join()

        # Events
        for event in pygame.event.get():

            # Quit when pressing the X button
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)

    # Game finished: display the winner
    while True:
        # Draw board
        board.screen.fill(0)
        board.draw_board(cur_state)

        # Print the winner
        font = pygame.font.Font("freesansbold.ttf", 48)

        if cur_state.get_winner() == 0:
            text = font.render(" Yellow wins! ", True, (255, 164, 0),
                               (34, 34, 34))
        else:
            text = font.render(" Red wins! ", True, (150, 0, 0), (34, 34, 34))

        textRect = text.get_rect()
        textRect.center = (n_tiles * 100 // 2, n_tiles * 100 // 2)
        board.screen.blit(text, textRect)

        # Print if time-out or invalid action
        if cur_state.timeout_player != None:
            font2 = pygame.font.Font("freesansbold.ttf", 18)
            text2 = font2.render("The opponent timed out", True,
                                 (255, 255, 255), (34, 34, 34))
            textRect2 = text2.get_rect()
            textRect2.center = (n_tiles * 100 // 2, n_tiles * 100 // 2 + 34)
            board.screen.blit(text2, textRect2)
        elif cur_state.invalid_player != None:
            font2 = pygame.font.Font("freesansbold.ttf", 18)
            text2 = font2.render("The opponent made an invalid move", True,
                                 (255, 255, 255), (34, 34, 34))
            textRect2 = text2.get_rect()
            textRect2.center = (n_tiles * 100 // 2, n_tiles * 100 // 2 + 34)
            board.screen.blit(text2, textRect2)

        # Update screen
        pygame.display.flip()

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)

        sleep(1)
Exemple #4
0
def main(agent_0, agent_1, time_out, sleep_time):
    # Initialisation
    pygame.init()
    n_tiles = 7
    n_pawns = 5
    screen = pygame.display.set_mode((n_tiles * 100, n_tiles * 100))

    # Ressourses
    tile = pygame.image.load("resources/tile.png")
    corner = pygame.image.load("resources/corner.png")
    start_l = [
        pygame.image.load("resources/start_" + str(x) + "_l.png")
        for x in range(1, 4)
    ]
    start_b = [
        pygame.image.load("resources/start_" + str(x) + "_b.png")
        for x in range(1, 4)
    ]
    start_r = [
        pygame.image.load("resources/start_" + str(x) + "_r.png")
        for x in range(1, 4)
    ]
    start_t = [
        pygame.image.load("resources/start_" + str(x) + "_t.png")
        for x in range(1, 4)
    ]
    yellow_pawn = pygame.image.load("resources/yellow_pawn.png")
    red_pawn = pygame.image.load("resources/red_pawn.png")
    yellow_pawn_ret = pygame.image.load("resources/yellow_pawn_ret.png")
    red_pawn_ret = pygame.image.load("resources/red_pawn_ret.png")
    yellow_pawn_fin = pygame.image.load("resources/yellow_pawn_fin.png")
    red_pawn_fin = pygame.image.load("resources/red_pawn_fin.png")

    cur_state = SquadroState()
    agents = [
        getattr(__import__(agent_0), 'MyAgent')(),
        getattr(__import__(agent_1), 'MyAgent')()
    ]

    while not cur_state.game_over():
        # Clear screen
        screen.fill(0)

        # Draw the tiles
        for i in range(1, n_tiles - 1):
            for j in range(1, n_tiles - 1):
                screen.blit(tile, (i * 100, j * 100))
        screen.blit(corner, (0, 0))
        screen.blit(start_l[0], (0, 100))
        screen.blit(start_l[2], (0, 200))
        screen.blit(start_l[1], (0, 300))
        screen.blit(start_l[2], (0, 400))
        screen.blit(start_l[0], (0, 500))
        screen.blit(corner, (0, 600))
        screen.blit(start_b[0], (100, 600))
        screen.blit(start_b[2], (200, 600))
        screen.blit(start_b[1], (300, 600))
        screen.blit(start_b[2], (400, 600))
        screen.blit(start_b[0], (500, 600))
        screen.blit(corner, (600, 600))
        screen.blit(start_r[2], (600, 500))
        screen.blit(start_r[0], (600, 400))
        screen.blit(start_r[1], (600, 300))
        screen.blit(start_r[0], (600, 200))
        screen.blit(start_r[2], (600, 100))
        screen.blit(corner, (600, 0))
        screen.blit(start_t[2], (500, 0))
        screen.blit(start_t[0], (400, 0))
        screen.blit(start_t[1], (300, 0))
        screen.blit(start_t[0], (200, 0))
        screen.blit(start_t[2], (100, 0))

        # Draw the pawns
        for i in range(n_pawns):
            if cur_state.is_pawn_finished(0, i):
                screen.blit(yellow_pawn_fin, cur_state.get_pawn_position(0, i))
            elif cur_state.is_pawn_returning(0, i):
                screen.blit(yellow_pawn_ret, cur_state.get_pawn_position(0, i))
            else:
                screen.blit(yellow_pawn, cur_state.get_pawn_position(0, i))

            if cur_state.is_pawn_finished(1, i):
                screen.blit(red_pawn_fin, cur_state.get_pawn_position(1, i))
            elif cur_state.is_pawn_returning(1, i):
                screen.blit(red_pawn_ret, cur_state.get_pawn_position(1, i))
            else:
                screen.blit(red_pawn, cur_state.get_pawn_position(1, i))

        # Draw who's turn it is
        font1 = pygame.font.Font("freesansbold.ttf", 12)
        text1 = font1.render("Current player:", True, (255, 255, 255),
                             (34, 34, 34))
        textRect1 = text1.get_rect()
        textRect1.center = ((n_tiles - 1) * 100 + 50, (n_tiles - 1) * 100 + 38)
        screen.blit(text1, textRect1)

        font2 = pygame.font.Font("freesansbold.ttf", 20)
        if cur_state.get_cur_player() == 0:
            text2 = font2.render("    ", True, (255, 164, 0), (255, 164, 0))
        else:
            text2 = font2.render("    ", True, (126, 0, 0), (126, 0, 0))
        textRect2 = text2.get_rect()
        textRect2.center = ((n_tiles - 1) * 100 + 50, (n_tiles - 1) * 100 + 62)
        screen.blit(text2, textRect2)

        # Update screen
        pygame.display.flip()

        # Make move
        action = agents[cur_state.get_cur_player()].get_action(
            cur_state, False, False)
        cur_state.apply_action(action)

        # Events
        for event in pygame.event.get():

            # Quit when pressing the X button
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)

        sleep(sleep_time)

    while True:
        # Clear screen
        screen.fill(0)

        # Draw the tiles
        for i in range(1, n_tiles - 1):
            for j in range(1, n_tiles - 1):
                screen.blit(tile, (i * 100, j * 100))
        screen.blit(corner, (0, 0))
        screen.blit(start_l[0], (0, 100))
        screen.blit(start_l[2], (0, 200))
        screen.blit(start_l[1], (0, 300))
        screen.blit(start_l[2], (0, 400))
        screen.blit(start_l[0], (0, 500))
        screen.blit(corner, (0, 600))
        screen.blit(start_b[0], (100, 600))
        screen.blit(start_b[2], (200, 600))
        screen.blit(start_b[1], (300, 600))
        screen.blit(start_b[2], (400, 600))
        screen.blit(start_b[0], (500, 600))
        screen.blit(corner, (600, 600))
        screen.blit(start_r[2], (600, 500))
        screen.blit(start_r[0], (600, 400))
        screen.blit(start_r[1], (600, 300))
        screen.blit(start_r[0], (600, 200))
        screen.blit(start_r[2], (600, 100))
        screen.blit(corner, (600, 0))
        screen.blit(start_t[2], (500, 0))
        screen.blit(start_t[0], (400, 0))
        screen.blit(start_t[1], (300, 0))
        screen.blit(start_t[0], (200, 0))
        screen.blit(start_t[2], (100, 0))

        # Draw the pawns
        for i in range(n_pawns):
            if cur_state.is_pawn_finished(0, i):
                screen.blit(yellow_pawn_fin, cur_state.get_pawn_position(0, i))
            elif cur_state.is_pawn_returning(0, i):
                screen.blit(yellow_pawn_ret, cur_state.get_pawn_position(0, i))
            else:
                screen.blit(yellow_pawn, cur_state.get_pawn_position(0, i))

            if cur_state.is_pawn_finished(1, i):
                screen.blit(red_pawn_fin, cur_state.get_pawn_position(1, i))
            elif cur_state.is_pawn_returning(1, i):
                screen.blit(red_pawn_ret, cur_state.get_pawn_position(1, i))
            else:
                screen.blit(red_pawn, cur_state.get_pawn_position(1, i))

        # Print the winner
        font = pygame.font.Font("freesansbold.ttf", 48)

        if cur_state.get_winner() == 0:
            text = font.render(" Yellow wins! ", True, (255, 164, 0),
                               (34, 34, 34))
        else:
            text = font.render(" Red wins! ", True, (126, 0, 0), (34, 34, 34))

        textRect = text.get_rect()
        textRect.center = (n_tiles * 100 // 2, n_tiles * 100 // 2)
        screen.blit(text, textRect)

        # Update screen
        pygame.display.flip()

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)

        sleep(1)
def game(agent, validation, i, new_model_path):
    
    results = 0
    init = 1

    # Initialisation
    cur_state = SquadroState()
    cur_state.cur_player = 0 if i % 2 == 0 else 1
    agents = [getattr(__import__(agent), 'MyAgent')(), getattr(__import__(agent), 'MyAgent')()]
    agents[0].set_id(0)
    agents[1].set_id(1)
    
    #print_network(agents[0].deepnetwork)
    
    if validation: # Use different models during validation phase
        ind = 0 if i % 4 < 2 else 1 
        agents[ind].set_model_path(new_model_path)
        print('Main model:', ind, ', First player:', cur_state.cur_player)
        # Remove stochastic actions
        #agents[0].epsilonMCTS = 0
        #agents[1].epsilonMCTS = 0
        agents[0].epsilonMove = 0
        agents[1].epsilonMove = 0
        '''
        print('Current network model...............................................')
        print_network(agents[0].deepnetwork)
        print('New network model....................................................')
        print_network(agents[1].deepnetwork)
        '''
        
    last_action = None

    
    while not cur_state.game_over():
    
        # Make move
        cur_player = cur_state.get_cur_player()
        action = get_action_timed(agents[cur_player], cur_state.copy(), last_action)
    
        if cur_state.is_action_valid(action):
            cur_state.apply_action(action)
            last_action = action
        else:
            cur_state.set_invalid_action(cur_player)
        
        if init:
            results = agents[cur_player].results
            init = 0
        else:
            results = np.append(results, agents[cur_player].results, 0)
            
    #if validation:
    #    print(results)
    #    print(cur_player)
    return (results, cur_player)
Exemple #6
0
def game(agent_0, agent_1, i):
    
    model_path = 'model/{}.pt'.format(agent_0)
    results = 0
    init = 1

    # Initialisation
    cur_state = SquadroState()
    cur_state.cur_player = 0 if i % 4 < 2 else 1
    
    if i % 2 == 0:
        agents = [getattr(__import__(agent_0), 'MyAgent')(), getattr(__import__(agent_1), 'MyAgent')()]
        DNN_player = 0
    else:
        agents = [getattr(__import__(agent_1), 'MyAgent')(), getattr(__import__(agent_0), 'MyAgent')()]
        DNN_player = 1
    
    print(DNN_player, cur_state.cur_player)
        
    agents[0].set_id(0)
    agents[1].set_id(1)
    agents[DNN_player].epsilonMove = 0
    #if i == 0:
        #print('Network 0 (main) -------------------------------------------------------')
        #print_network(agents[0].deepnetwork)
        #print('Network 1 (other) -------------------------------------------------------')
        #print_network(agents[1].deepnetwork)

    last_action = None
    
    while not cur_state.game_over():
    
        # Make move
        cur_player = cur_state.get_cur_player()
        action = get_action_timed(agents[cur_player], cur_state.copy(), last_action)
    
        if cur_state.is_action_valid(action):
            cur_state.apply_action(action)
            last_action = action
        else:
            cur_state.set_invalid_action(cur_player)

    #print('Last state:', agents[DNN_player].results)
    return (0, abs(cur_player - DNN_player))
def main(agent_0, agent_1, first):
    # Initialisation
    cur_state = SquadroState()
    if first != -1:
        cur_state.cur_player = first
    agents = [
        getattr(__import__(agent_0), 'MyAgent')(),
        getattr(__import__(agent_1), 'MyAgent')()
    ]
    agents[0].set_id(0)
    agents[1].set_id(1)
    last_action = None

    while not cur_state.game_over():

        # Make move
        cur_player = cur_state.get_cur_player()
        action = get_action_timed(agents[cur_player], cur_state.copy(),
                                  last_action)

        if cur_state.is_action_valid(action):
            cur_state.apply_action(action)
            last_action = action
        else:
            cur_state.set_invalid_action(cur_player)
    print(cur_player)
Exemple #8
0
def game(agent_0, agent_1, i):

    model_path = 'model/{}.pt'.format(agent_0)
    other_model_path = 'model/{}.pt'.format(agent_1)

    results = 0
    init = 1

    # Initialisation
    cur_state = SquadroState()

    agents = [
        getattr(__import__(agent_0), 'MyAgent')(),
        getattr(__import__(agent_0), 'MyAgent')()
    ]

    DNN_main_player = 0 if i % 2 == 0 else 1
    cur_state.cur_player = 0 if (2 * i) % 2 == 0 else 1

    print(DNN_main_player, cur_state.cur_player)

    agents[0].set_id(0)
    agents[1].set_id(1)
    agents[1 - DNN_main_player].set_model_path(other_model_path)
    '''
    agents[0].epsilonMCTS = 0
    agents[1].epsilonMCTS = 0
    '''
    agents[0].epsilonMove = 0
    agents[1].epsilonMove = 0
    if i == 0:
        print(
            'Network 0 (main) -------------------------------------------------------'
        )
        print_network(agents[DNN_main_player].deepnetwork)
        print(
            'Network 1 (other) -------------------------------------------------------'
        )
        print_network(agents[1 - DNN_main_player].deepnetwork)

    last_action = None

    while not cur_state.game_over():

        # Make move
        cur_player = cur_state.get_cur_player()
        action = get_action_timed(agents[cur_player], cur_state.copy(),
                                  last_action)

        if cur_state.is_action_valid(action):
            cur_state.apply_action(action)
            last_action = action
        else:
            cur_state.set_invalid_action(cur_player)

        if init:
            results = agents[cur_player].results
            init = 0
        else:
            results = np.append(results, agents[cur_player].results, 0)

    return (results, abs(DNN_main_player - cur_player))