Exemplo n.º 1
0
 def init_game(random = True):
     def get_random_orientation():
         return np.random.choice([0, 90, 180, 270])
     game = tron.TronGame(width = args.width, height = args.height, save_history = args.save_replay)
     if random:
         game.set_player_pos(game.get_random_pos(), game.get_random_pos())
         game.set_player_orientation([get_random_orientation(), get_random_orientation()])
     return game
Exemplo n.º 2
0
def demo_game(net1, net2):
    print("PLAYING DEMO GAME!")

    game = tron.TronGame(width=20, height=20)

    finished = False
    while not finished:
        evolve(game, net1, net2)

        gameState = game.get_game_state()
        finished = gameState[0]

    gameState = game.get_game_state()
    print(gameState)
Exemplo n.º 3
0
def game_outcome(net1, net2):
    game = tron.TronGame(width=gameSize[0], height=gameSize[1])

    finished = False
    while not finished:
        evolve(game, net1, net2)

        gameState = game.get_game_state()
        finished = gameState[0]

    winners = gameState[4]

    if (winners[0] and not winners[1]):
        #print("PLAYER 2 has WON")
        return 2
    if (not winners[0] and winners[1]):
        #print("PLAYER 1 has WON")
        return 1
    else:
        return 0
Exemplo n.º 4
0
from strategy.reinforcement_learning.rl_strategy_train import *
import tron
import pickle
import os
from time import time

env = tron.TronGame(width=30, height=30)

STRATEGY_FILE = 'tmp/rl_strategy.npy'


def calculate_reward(player_idx):
    return env.tick + (-10 if env.player_lost[player_idx] else 20)


USE_CHECK_POINT = True
NUM_GAMES = 1000000

if USE_CHECK_POINT and os.path.exists(STRATEGY_FILE):
    with open(STRATEGY_FILE, 'rb') as f:
        RLS = pickle.load(f)
else:
    RLS = [
        QLearningTable(actions=list(env.get_available_actions()))
        for x in range(2)
    ]


def check_point():
    with open(STRATEGY_FILE, 'wb') as f:
        pickle.dump(RLS, f)
Exemplo n.º 5
0
def init_game(args):
    env = tron.TronGame(width=args.width, height=args.width)
    env.reset(set_random=True)
    return env
Exemplo n.º 6
0
 def init_game(random=True):
     game = tron.TronGame(width=args.width, height=args.height)
     if random:
         game.set_player_pos(game.get_random_pos(), game.get_random_pos())
     return game
Exemplo n.º 7
0
def main():
    args = get_args()

    used_replays = replay_helper.get_all_replays(args.replay_folder)

    pygame.font.init()
    font = pygame.font.SysFont("Verdana", 20)

    text_color = (120, 120, 120)
    text_position = (5, 5)
    game_offset = (5, 100)

    for replay_file, replay in used_replays:
        info = replay.info
        start_positions = [body[0] for body in replay.bodies]
        start_orientations = [
            orientation[0] for orientation in replay.orientations
        ]
        width, height = replay.info['width'], replay.info['height']

        game = tron.TronGame(width=width, height=height, save_history=False)
        game.set_player_orientation(start_orientations)
        game.set_player_pos(*start_positions)

        pygame.init()
        screen = pygame.display.set_mode(
            (int(width * args.player_dim) + game_offset[0] * 2,
             int(height * args.player_dim) + game_offset[1]))

        pygame.display.set_caption('Tron')
        pygame.mouse.set_visible(0)

        texts = [replay_file.split('/')[-1]]
        if 'strategies' in info:
            strategies = info['strategies']
            texts.append(" vs. ".join(strategies))

        def render_text(t):
            return font.render(t, True, text_color)

        texts = [render_text(t) for t in texts]

        clock = pygame.time.Clock()

        for idx, game_field in enumerate(replay.game_field):
            clock.tick(1000)
            resume_this_game = draw_game(screen,
                                         game_field,
                                         clock,
                                         args.player_dim,
                                         args.tick_delay,
                                         background_offset=game_offset)

            for idx, text in enumerate(texts +
                                       [render_text('Tick: {}'.format(idx))]):
                screen.blit(text,
                            (text_position[0], text_position[1] + (30 * idx)))

            pygame.display.flip()
            pygame.time.wait(args.tick_delay)
            if not resume_this_game:
                break

        pygame.time.wait(args.delay_after_gameover)

        # OLD
        if False:

            for p1_action, p2_action in zip(*replay.actions):
                game.set_action(0, p1_action)
                game.set_action(1, p2_action)
                'FALSE'
                resume_this_game = draw_game(screen, game, clock,
                                             args.player_dim, args.tick_delay)