def onShowActions(self, event): if self.state.turn < 100: # the following is needed since if the turn does not change, # non-stateless bots behave differently if self.player and self.turn_repeat: self.player = Player(self.player1_path) self.player.set_player_id(1) if self.player2 and self.turn_repeat: self.player2 = Player(self.player2_path) self.player2.set_player_id(0) self.UI.clearActions() actions = self.getActions() self.cached_actions = actions for loc, action in actions.iteritems(): self.UI.renderAction(loc, action) self.turn_repeat = True try: rgsim_text = self.player._module.rgsim_text for loc, text in rgsim_text.iteritems(): self.UI.renderText(loc, text) except AttributeError: print "No rgsim_text dict found for player 1, skipping..." except: print("Error in rgsim_text dict, please ensure keys are " + "valid locations and values are strings")
def onReloadPlayer(self, event): self.UI.fadeActions() self.cached_actions = None self.human_actions = {} if self.player: self.player = Player(self.player1_path) self.player.set_player_id(1) if self.player2: self.player2 = Player(self.player2_path) self.player2.set_player_id(0)
def __init__(self, player1_path, player2_path): self.match_id = None self.player = Player(player1_path) if player1_path else None if self.player: self.player1_path = player1_path self.player.set_player_id(1) self.player2 = Player(player2_path) if player2_path else None if self.player2: self.player2_path = player2_path self.player2.set_player_id(0) self.turn_repeat = False self.UI = SimulatorUI(settings) self.UI.setTitle("Robot Game Simulator") self.state = GameState(settings, turn=1) self.cached_actions = None self.human_actions = {} self.UI.setTurn(self.state.turn) self.UI.bind("w", lambda ev: self.UI.moveSelection((0, -1))) self.UI.bind("a", lambda ev: self.UI.moveSelection((-1, 0))) self.UI.bind("s", lambda ev: self.UI.moveSelection((0, 1))) self.UI.bind("d", lambda ev: self.UI.moveSelection((1, 0))) self.UI.bind("<Up>", lambda ev: self.UI.moveSelection((0, -1))) self.UI.bind("<Left>", lambda ev: self.UI.moveSelection((-1, 0))) self.UI.bind("<Down>", lambda ev: self.UI.moveSelection((0, 1))) self.UI.bind("<Right>", lambda ev: self.UI.moveSelection((1, 0))) self.UI.bind("<ButtonPress-1>", lambda ev: self.UI.onMouseClick(ev)) self.UI.bind("l", self.onLoadMatch) self.UI.bind("k", self.onLoadTurn) self.UI.bind("o", self.onReloadPlayer) self.UI.bind("p", self.onSwapPlayer) self.UI.bind("t", self.onEditTurn) self.UI.bind("f", self.onAddTeammate) self.UI.bind("e", self.onAddEnemy) self.UI.bind("r", self.onRemove) self.UI.bind("c", self.onClear) self.UI.bind("<Delete>", self.onRemove) self.UI.bind("<BackSpace>", self.onRemove) self.UI.bind("h", self.onEditHP) self.UI.bind("<space>", self.onShowActions) self.UI.bind("<Return>", self.onSimulate) self.UI.bind("n", self.onNextAction) self.UI.bind("g", self.onSpawnRobots) self.UI.run()
def from_robots(robots, settings=None, options=None, delta_callback=None): players = [] for robot in robots: players.append(Player(robot=robot)) return Runner(players, settings=settings, options=options, delta_callback=delta_callback)
def break_ties(args): """Breaks ties between a group of Individuals by pitting them against each other. In case of an absolute tie, the 'most elite' is favored. This fills half of a matrix of scores. If we know A's score in (A vs B), then we can determine B's score in (B vs A) without making them compete a second time.""" individual = args[0] tied_individuals = args[1] individual_idx = args[2] options = rgrun.Options() options.headless = True options.quiet = 10 options.n_of_games = constants.games_per_scoring players = [Player(robot=individual.get_robot()), None] results = [] # AS PLAYER 1 # Play against other elites in generation for x in range(individual_idx + 1, len(tied_individuals)): opponent = tied_individuals[x] players[1] = Player(name="individual", robot=opponent.get_robot()) result = rgrun.Runner(players=players, options=options).run() results.append(sum(p1 >= p2 for p1, p2 in result)) # AS PLAYER 2 players = [None, Player(robot=individual.get_robot())] # Play against other elites in generation for x in range(individual_idx + 1, len(tied_individuals)): opponent = tied_individuals[x] players[0] = Player(name="individual", robot=opponent.get_robot()) result = rgrun.Runner(players=players, options=options).run() results.append(sum(p2 >= p1 for p1, p2 in result)) return results
def initial_score_individuals(args): individual = args[0] gen = args[1] options = rgrun.Options() options.headless = True options.quiet = 10 options.n_of_games = constants.games_per_scoring individual.score = 0 players = [Player(robot=individual.get_robot()), None] folder = os.getcwd() folder = os.path.join(folder, "rgkit", "bots") # AS PLAYER 1 # Play against the coded bots for file_name in os.listdir(folder): loc_path = os.path.join("rgkit", "bots", file_name) #print "\t\tOpponent:", file_name try: players[1] = Player(file_name=loc_path) except IOError: print "Error opening", loc_path sys.exit(1) results = rgrun.Runner(players=players, options=options).run() individual.score += sum(p1 > p2 for p1, p2 in results) # Play against other elites in generation is_elite = (gen.population.index(individual) < constants.elite_size) if is_elite: individual.score += 1 # Free win for being in elite for elite_idx in range(constants.elite_size): if individual is not gen.population[elite_idx]: players[1] = Player( name="individual", robot=gen.population[elite_idx].get_robot()) results = rgrun.Runner(players=players, options=options).run() individual.score += sum(p1 > p2 for p1, p2 in results) else: # No free win for non-elite contenders for elite_idx in range(constants.elite_size): players[1] = Player(name="individual", robot=gen.population[elite_idx].get_robot()) results = rgrun.Runner(players=players, options=options).run() individual.score += sum(p1 > p2 for p1, p2 in results) # AS PLAYER 2 players = [None, Player(robot=individual.get_robot())] # Play against the coded bots for file_name in os.listdir(folder): loc_path = os.path.join("rgkit", "bots", file_name) #print "\t\tOpponent:", file_name try: players[0] = Player(file_name=loc_path) except IOError: print "Error opening", loc_path sys.exit(1) results = rgrun.Runner(players=players, options=options).run() individual.score += sum(p2 > p1 for p1, p2 in results) # Play against other elites in generation is_elite = (gen.population.index(individual) < constants.elite_size) if is_elite: individual.score += 1 # Free win for being in elite for elite_idx in range(constants.elite_size): if individual is not gen.population[elite_idx]: players[0] = Player( name="individual", robot=gen.population[elite_idx].get_robot()) results = rgrun.Runner(players=players, options=options).run() individual.score += sum(p2 > p1 for p1, p2 in results) else: # No free win for non-elite contenders for elite_idx in range(constants.elite_size): players[0] = Player(name="individual", robot=gen.population[elite_idx].get_robot()) results = rgrun.Runner(players=players, options=options).run() individual.score += sum(p2 > p1 for p1, p2 in results) return individual.score