def act(self, game): robots = game['robots'] locs = rg.locs_around(self.location, filter_out=('invalid', 'obstacle')) enemies = [loc for loc in locs if loc in robots and robots[loc]['player_id'] != self.player_id] if enemies: ## attack weakest loc = sorted(enemies, key=lambda x: robots[x]['hp'])[0] return ['attack', loc] ## so no enemy nearby, walk around randomly but prefer non-spawn points ## filter out my own occupied spots locs = [loc for loc in locs if loc not in robots] ## empty non spawn points? non_spawn = [loc for loc in locs if 'spawn' not in rg.loc_types(loc)] if non_spawn: loc = random.choice(non_spawn) return ['move', loc] spawn = [loc for loc in locs if 'spawn' in rg.loc_types(loc)] if spawn: loc = random.choice(spawn) return ['move', loc] ## no more options return ['guard']
def act(self, game): # get a list of all my allies allies = [bot for bot in game['robots'].values() if bot.player_id == self.player_id] # process movement alone movements = {} attacks = {} for bot in allies: movements[bot.location] = request_move(bot, game, allies) # remove collisions for loc, choice in movements.items(): # check for environment collisions if 'obstacle' in rg.loc_types(choice) or 'invalid' in rg.loc_types(choice): del movements[loc] continue occupant = game['robots'].get(choice) # check for vacancy if occupant != None: del movements[loc] # check for enemy if occupant.player_id != self.player_id: attacks[loc] = occupant.location # enact MY decision if self.location in movements: return ['move', movements[self.location]] elif self.location in attacks: return ['attack', attacks[self.location]] # neither move nor attack. if I will die in 1 turn and there are nearby enemies, suicide elif self.hp <= 18 and True in [loc in game['robots'] and game['robots'][loc].player_id != self.player_id for loc in rg.locs_around(self.location)]: return ['suicide'] else: return ['guard']
def act(self, game): wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} wejscia = {poz for poz in wszystkie if 'spawn' in rg.loc_types(poz)} zablokowane = {poz for poz in wszystkie if 'obstacle' in rg.loc_types(poz)} druzyna = {poz for poz in game.robots if game.robots[poz].player_id == self.player_id} wrogowie = set(game.robots) - druzyna sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie sasiednie_wrogowie2 = {poz for poz in sasiednie if (set(rg.locs_around(poz)) & wrogowie)} - druzyna # działanie domyślne: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] if self.location in wejscia: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] if self.location == rg.CENTER_POINT: ruch = ['guard'] if sasiednie_wrogowie: if 9*len(sasiednie_wrogowie) >= self.hp: pass else: ruch = ['attack', sasiednie_wrogowie.pop()] if sasiednie_wrogowie2: ruch = ['attack', sasiednie_wrogowie2.pop()] return ruch
def score_orientation(loc): self.orientation_score = 0 #Terrain types self._loc_type = rg.loc_types(loc) if 'spawn' in self._loc_type: self.orientation_score += spawn if 'spawn' in rg.loc_types(base_bot): self.orientation_score += spawn_move # Distance to center self.dist_to_center = round(rg.dist(loc, gravity_center)) if self.dist_to_center <= inner_circle: self.dist_to_center = 0 self.orientation_score += - self.dist_to_center #Distance to friends self.dist_to_closest_friend = 0 for loc2 in friends: self._ref_dist = 16 self.dist_to_closest_friend = 16 self.dist_to_closest_friend = rg.dist(loc, loc2) if self.dist_to_closest_friend < self._ref_dist: self._ref_dist = self.dist_to_closest_friend self.orientation_score += round(self.dist_to_closest_friend) return self.orientation_score
def act(self, game): global runda_numer, wybrane_pola if game.turn != runda_numer: runda_numer = game.turn wybrane_pola = set() # jeżeli się ruszamy, zapisujemy docelowe pole def ruszaj(loc): wybrane_pola.add(loc) return ['move', loc] # jeżeli stoimy, zapisujemy zajmowane miejsce def stoj(act, loc=None): wybrane_pola.add(self.location) return [act, loc] # wyznaczamy zbiory różnych pól na planszy wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} wejscia = {loc for loc in wszystkie if 'spawn' in rg.loc_types(loc)} zablokowane = {loc for loc in wszystkie if 'obstacle' in rg.loc_types(loc)} druzyna = {loc for loc in game.robots if game.robots[loc].player_id == self.player_id} wrogowie = set(game.robots) - druzyna sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie sasiednie_wrogowie2 = {loc for loc in sasiednie if (set(rg.locs_around(loc)) & wrogowie)} - druzyna # bezpieczne = sasiednie - sasiednie_wrogowie - sasiednie_wrogowie2 - wejscia - druzyna bezpieczne = sasiednie - sasiednie_wrogowie - sasiednie_wrogowie2 - \ wejscia - druzyna - wybrane_pola def mindist(bots, loc): return min(bots, key=lambda x: rg.dist(x, loc)) def minhp(bots): return min(bots, key=lambda x: game.robots[x].hp) if wrogowie: najblizszy_wrog = mindist(wrogowie,self.location) else: najblizszy_wrog = rg.CENTER_POINT ruch = ['guard'] if self.location in wejscia: if bezpieczne: ruch = ['move', mindist(bezpieczne, rg.CENTER_POINT)] elif sasiednie_wrogowie: if 9*len(sasiednie_wrogowie) < self.hp: ruch = stoj('attack',minhp(sasiednie_wrogowie.pop())) elif bezpieczne: ruch = ruszaj(mindist(bezpieczne, rg.CENTER_POINT)) else: ruch = stoj('suicide') elif sasiednie_wrogowie2 and self.location not in wybrane_pola: ruch = ['attack', sasiednie_wrogowie2.pop()] elif bezpieczne: ruch = ruszaj(mindist(bezpieczne, najblizszy_wrog)) return ruch
def escape_spawn_trap_move(self, game): if 'spawn' in rg.loc_types(self.location): locs = [x for x in rg.locs_around(self.location) if self.is_empty_loc(game, x)] free_locs = [x for x in locs if 'spawn' not in rg.loc_types(x)] safe_locs = [x for x in locs if self.is_safe_from_attacks(game, x)] safe_and_free_locs = [x for x in free_locs if x in safe_locs] # Try moving away first, even if may get hit. if len(safe_and_free_locs) > 0: return ['move', random.choice(safe_and_free_locs)] elif len(safe_locs) > 0: return ['move', random.choice(safe_locs)] elif len(free_locs) > 0: return ['move', random.choice(free_locs)] elif len(locs) > 0: return ['move', random.choice(locs)] # No where to move. else: # Todo: for friendlies, can tell them to gtfo out lol. # Todo: find a route, instead of just moving back n forth lol # Gonna die anyways, explode and cause some damage! # Enemies likely to guard. Some may move but hard to tell. if self.is_spawn_reset(game): return ['suicide'] # Else, go to some other behaviour return False
def score_orientation(loc): self.orientation_score = 0 #Terrain types self._loc_type = rg.loc_types(loc) if 'spawn' in self._loc_type: self.orientation_score += spawn if 'spawn' in rg.loc_types(base_bot): self.orientation_score += spawn_move # Distance to center self.dist_to_center = round(rg.dist(loc, gravity_center)) if self.dist_to_center <= inner_circle: self.dist_to_center = 0 self.orientation_score += -self.dist_to_center #Distance to friends self.dist_to_closest_friend = 0 for loc2 in friends: self._ref_dist = 16 self.dist_to_closest_friend = 16 self.dist_to_closest_friend = rg.dist(loc, loc2) if self.dist_to_closest_friend < self._ref_dist: self._ref_dist = self.dist_to_closest_friend self.orientation_score += round(self.dist_to_closest_friend) return self.orientation_score
def act(self, game): # zbiory pól na planszy # wszystkie pola wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} # punkty wejścia (spawn) wejscia = {loc for loc in wszystkie if 'spawn' in rg.loc_types(loc)} # pola zablokowane (obstacle) zablokowane = {loc for loc in wszystkie if 'obstacle' in rg.loc_types(loc)} # pola zajęte przez nasze roboty druzyna = {loc for loc in game.robots if game.robots[loc].player_id == self.player_id} # pola zajęte przez wrogów wrogowie = set(game.robots) - druzyna sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie sasiednie_wrogowie2 = {loc for loc in sasiednie if (set(rg.locs_around(loc)) & wrogowie)} - druzyna bezpieczne = sasiednie - sasiednie_wrogowie - sasiednie_wrogowie2 - wejscia - druzyna print wejscia # domyślne działanie robota: ruch do środka ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] return ruch
def act(self, game): # Borrowed sets of sets from github.com/ramk13 all_locs = {(x, y) for x in xrange(19) for y in xrange(19)} spawn = {loc for loc in all_locs if 'spawn' in rg.loc_types(loc)} obstacle = {loc for loc in all_locs if 'obstacle' in rg.loc_types(loc)} team = {loc for loc in game.robots if game.robots[loc].player_id == self.player_id} enemy = set(game.robots) - team adjacent = set(rg.locs_around(self.location)) - obstacle adjacent_enemy = adjacent & enemy safe = adjacent - adjacent_enemy - spawn - team north = {loc for loc in team if loc[1] > 9 and loc[0] > 9} east = {loc for loc in team if loc[1] <= 9 < loc[0]} west = {loc for loc in team if loc[1] <= 9 and loc[0] <= 9} south = team - north - east - west x, y = int(sum(x for x, y in north if self)/len(north)), int(sum(y for x, y in north)/len(north)) sx, sy = int(sum(x for x, y in south)/len(south)), int(sum(y for x, y in south)/len(south)) if self.location in ((nx, ny), (sx, sy)) or rg.locs_around(self.location) in ((nx, ny), (sx, sy)): if self.location not in spawn: return ['guard'] if len(adjacent_enemy) >= 3: return ['suicide'] if self.hp < 9 and adjacent_enemy: return ['suicide'] if adjacent_enemy: return ['attack', min(adjacent_enemy, key=lambda x: rg.dist(x, self.location))] if safe: if self.location in north: return ['move', rg.toward(self.location, (nx, ny))] else: return ['move', rg.toward(self.location, (sx, sy))] return ['guard']
def act(self, game): self.game = game enemy_location = self.closest_adjacent_enemy() if 'spawn' in rg.loc_types(self.location): destinations = rg.locs_around(self.location, filter_out=('invalid','obstacle','spawn')) if destinations: # print("Robot {0}: In a spawn. Moving to {1}".format(str(self.location), str(destination))) return self.move(destinations[0]) else: return self.guard() elif enemy_location: # print("Robot {0}:Trying to attack. Location:{1}".format(str(self.location), str(enemy_location))) return self.attack(enemy_location) else: enemies = [] for loc, robot in game.robots.items(): if robot.player_id != self.player_id: enemies.append(loc) enemies.sort() if rg.toward(self.location, enemies[0]) == self.location: # print("Robot {0}:Don't want to collide; guarding.".format(self.location)) return self.guard() else: destination = rg.toward(self.location, enemies[0]) # print("Robot {0}: Not in position; moving to {1}.".format(self.location, destination)) if 'spawn' not in rg.loc_types(destination): return self.move(rg.toward(self.location, enemies[0])) else: return self.move(rg.toward(self.location, rg.CENTER_POINT))
def act(self, game): # wyznaczamy zbiory różnych pól na planszy wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} wejscia = {loc for loc in wszystkie if 'spawn' in rg.loc_types(loc)} zablokowane = {loc for loc in wszystkie if 'obstacle' in rg.loc_types(loc)} druzyna = {loc for loc in game.robots if game.robots[loc].player_id == self.player_id} wrogowie = set(game.robots) - druzyna sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie sasiednie_wrogowie2 = {loc for loc in sasiednie if (set(rg.locs_around(loc)) & wrogowie)} - druzyna bezpieczne = sasiednie - sasiednie_wrogowie - sasiednie_wrogowie2 - wejscia - druzyna def mindist(bots, loc): return min(bots, key=lambda x: rg.dist(x, loc)) ruch = ['guard'] if self.location in wejscia: if bezpieczne: ruch = ['move', mindist(bezpieczne, rg.CENTER_POINT)] if self.location == rg.CENTER_POINT: ruch = ['guard'] if sasiednie_wrogowie: if 9*len(sasiednie_wrogowie) < self.hp: ruch = ['attack', sasiednie_wrogowie.pop()] if sasiednie_wrogowie2: ruch = ['attack', sasiednie_wrogowie2.pop()] return ruch
def act(self, game): # wszystkie pola wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} # punkty wejścia wejscia = {poz for poz in wszystkie if 'spawn' in rg.loc_types(poz)} # pola zablokowane zablokowane = {poz for poz in wszystkie if 'obstacle' in rg.loc_types(poz)} # pola zajęte przez nasze roboty przyjaciele = {poz for poz in game.robots if game.robots[poz].player_id == self.player_id} # pola zajęte przez wrogów wrogowie = set(game.robots) - przyjaciele # pola sąsiednie sasiednie = set(rg.locs_around(self.location)) - zablokowane # pola sąsiednie zajęte przez wrogów wrogowie_obok = sasiednie & wrogowie # działanie domyślne: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w punkcie wejścia, opuść go if self.location in wejscia: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w środku, broń się if self.location == rg.CENTER_POINT: ruch = ['guard'] # jeżeli obok są przeciwnicy, atakuj if wrogowie_obok: ruch = ['attack', wrogowie_obok.pop()] return ruch
def __init__(self, robot, game): self.robot = robot self.game = game self.current_loc_types = rg.loc_types(robot.location) self.unobstructed_locs = [] self.normal_unobstructed_locs = [] self.safe_locs = [] self.immediate_friends = [] self.immediate_enemies = [] valid_and_wall_locs = rg.locs_around(robot.location, filter_out='invalid') self.valid_locs = [] for vwloc in valid_and_wall_locs: if not 'obstacle' in rg.loc_types(vwloc): self.unobstructed_locs.append(vwloc) self.valid_locs.append(vwloc) elif vwloc in self.game.robots: # valid locs INCLUDES robots self.valid_locs.append(vwloc) self.immediate_enemies = self.enemies_around(self.robot.location, self.robot.player_id) self.immediate_friends = self.friends_around(self.robot.location, self.robot.player_id, self.robot.location) for unobloc in self.unobstructed_locs: if not 'spawn' in rg.loc_types(unobloc): self.normal_unobstructed_locs.append(unobloc) if not self.enemies_around(unobloc, robot.player_id): self.safe_locs.append(unobloc)
def move(bot, game): x, y = bot.location RIGHT, UP, LEFT, DOWN = (1, 0), (0, -1), (-1, 0), (0, 1) prio = () result = False if rg.locs_around(tuple(bot.location), filter_out=('invalid', 'obstacle', 'normal')): #We are out of order return False if x <= 9 and y > 9: #lower left quadrant; moving right and down priority if bot.hp >= CRITICAL_HP: prio = (DOWN, RIGHT, UP, LEFT) else: prio = (LEFT, UP, RIGHT, DOWN) elif x > 9 and y >= 9: #lower right; right and up if bot.hp >= CRITICAL_HP: prio = (RIGHT, UP, LEFT, DOWN) else: prio = (DOWN, LEFT, UP, RIGHT) elif x > 9 and y <= 9: #upper right; up and left if bot.hp >= CRITICAL_HP: prio = (UP, LEFT, DOWN, RIGHT) else: prio = (RIGHT, DOWN, LEFT, UP) elif x <= 9 and y <= 9: #upper left; left and down if bot.hp >= CRITICAL_HP: prio = (LEFT, DOWN, RIGHT, UP) else: prio = (UP, RIGHT, DOWN, LEFT) else: prio = (UP, ) for d in prio: dest = (x + d[0], y + d[1]) occupied = False for loc, nbot in game.get('robots').items(): if bot.hp >= CRITICAL_HP and nbot.hp < CRITICAL_HP and nbot.get( "location") == dest: continue if occupied: #if it is occupied by an ally, check for a less prioritized move, but if not, try moving anyway result = ["move", dest] continue elif "spawn" in rg.loc_types(dest) or "invalid" in rg.loc_types( dest): continue elif bot.hp < CRITICAL_HP: x2 = dest[0] + d[0] y2 = dest[1] + d[ 1] # Also check the tile after, so we go in the second row instead if "spawn" in rg.loc_types((x2, y2)): continue else: result = ["move", dest] else: result = ["move", dest] break return result
def check_walkable(loc, game=None): if not set(rg.loc_types(loc)).isdisjoint(set(['invalid', 'obstacle'])): return False if game and loc in game['robots']: return False if game and game['turn'] % 10 == 0 and 'spawn' in rg.loc_types(loc): return False return True
def init_obstacles(): self.obstacles = np.ones((19, 19)) * np.nan for i in range(19): for j in range(19): if 'invalid' in rg.loc_types((i, j)) or 'obstacle' in rg.loc_types((i, j)): self.obstacles[i, j] = np.inf for _, bot in game.get('robots').items(): self.obstacles[tuple(bot["location"])] = enemy_cell if bot["player_id"] != self.player_id else friendly_cell
def check_walkable(self, loc, game): # if True in [(loc in game['robots']), ('obstacle' in rg.loc_types(loc)), ('invalid' in rg.loc_types(loc))]: if True in [('obstacle' in rg.loc_types(loc)), ('invalid' in rg.loc_types(loc))]: return False # if it's a spawning turn # if 'spawn' in rg.loc_types(loc) and game['turn'] % 10 == 1: # return False return True
def act(self, game): if "spawn" in rg.loc_types(self.location): s = sanitize(['move', rg.toward(self.location, rg.CENTER_POINT)]) return s adjacent_enemies = [] for loc, bot in game.get('robots').items(): if bot.get('player_id') != self.player_id: if rg.dist(loc, self.location) <= 1: allies = 0 #Find out how many allies are around this enemy for nloc, nbot in game.get('robots').items(): if bot.get("player_id") == self.player_id and rg.dist(loc, nloc) <=1: allies = allies + 1 adjacent_enemies.append([loc, bot, allies]) else: if "spawn" in rg.loc_types(bot.get("location")): #The friendly wants to get out of spawn, make way for it r = Robot.move(self, game) if r and rg.toward(bot.get("location"), rg.CENTER_POINT) == r[1]: return sanitize(['move', rg.toward(self.location, rg.CENTER_POINT)]) if adjacent_enemies and self.hp >= CRITICAL_HP: if len(adjacent_enemies) * ATTACK_DAMAGE > self.hp: # They can kill me! lets flee! return sanitize(self.flee(game)) adjacent_enemies.sort(key= lambda x: (x[2], x[1].get("hp"))) return sanitize(['attack', adjacent_enemies[0][0]]) elif adjacent_enemies and self.hp < CRITICAL_HP: return sanitize(self.flee(game)) else: r = Robot.move(self, game) if not r: return ["guard"] #Check if allied damaged bots will move to our destination, and if so, let them move = True for loc, bot in game.get('robots').items(): if bot.get("player_id") == self.player_id and not bot.location == self.location: if rg.dist(loc, r[1]) <= 1: if Robot.get_destination(bot, game) == r[1]: #our destination matches, let them come if bot.get("hp") < CRITICAL_HP: return ["guard"] else: # Figure out who should be given highest movement priority (based on which one is furthest from middle, or who has lowest hp in tiebreaker) prio = rg.dist(self.location, rg.CENTER_POINT) prio_o = rg.dist(bot.location, rg.CENTER_POINT) if prio == prio_o: #Tie if self.hp >= bot.hp: move = True else: move = False elif prio > prio_o: move = True else: move = False if not move: return ["guard"] else: return sanitize(r) or ["guard"]
def act(self, game): # wszystkie pola wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} # punkty wejścia wejscia = {poz for poz in wszystkie if 'spawn' in rg.loc_types(poz)} # pola zablokowane zablokowane = { poz for poz in wszystkie if 'obstacle' in rg.loc_types(poz) } # pola zajęte przez nasze roboty przyjaciele = { poz for poz in game.robots if game.robots[poz].player_id == self.player_id } # pola zajęte przez wrogów wrogowie = set(game.robots) - przyjaciele # pola sąsiednie sasiednie = set(rg.locs_around(self.location)) - zablokowane # pola sąsiednie zajęte przez wrogów wrogowie_obok = sasiednie & wrogowie wrogowie_obok2 = { poz for poz in sasiednie if (set(rg.locs_around(poz)) & wrogowie) } - przyjaciele # pola bezpieczne bezpieczne = sasiednie - wrogowie_obok - wejscia - przyjaciele # funkcja znajdująca najsłabszego wroga obok z podanego zbioru (bots) def minhp(bots): return min(bots, key=lambda x: game.robots[x].hp) # działanie domyślne: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w punkcie wejścia, opuść go if self.location in wejscia: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w środku, broń się if self.location == rg.CENTER_POINT: ruch = ['guard'] # jeżeli obok są przeciwnicy, atakuj, o ile to bezpieczne, # najsłabszego wroga if wrogowie_obok: if 9 * len(wrogowie_obok) >= self.hp: if bezpieczne: ruch = ['move', bezpieczne.pop()] else: ruch = ['attack', minhp(wrogowie_obok)] if wrogowie_obok2: ruch = ['attack', wrogowie_obok2.pop()] return ruch
def act(self, game): # Update the class variables if this is a new turn. if game.turn != self.currentTurn: # If this is the very first turn, print out some info. if game.turn == 1: print "The rally point is: {0}".format(self.rallyPoint); self.updateVariables(game); self.currentTurn = game.turn; print "New Turn! " + str(self.currentTurn) + " Friends: " + str(self.numberOfFriends) + " Foes: " + str(self.numberOfFoes); # First order of business, if this bot is on a spawn point, we need to get him out of there. if 'spawn' in rg.loc_types(self.location): # Try to move toward the rally point. moveToPoint = rg.toward(self.location, self.rallyPoint); if 'normal' in rg.loc_types(moveToPoint): return ['move', moveToPoint]; else: # Can't move towards the rally point. See where we can move. adjacentLocations = rg.locs_around(self.location); for loc in adjacentLocations: if 'normal' in rg.loc_types(loc): return ['move', loc]; # If we got here, we can't move off the spawn point. Let's try to do some damage if we have a nearby enemy. for loc in adjacentLocations: if loc in game.robots: if game.robots[loc].player_id != self.player_id: return self.attackOrSuicide(loc, game.robots[loc]); # If we are here, nothing else we can do, but guard. We must be blocked by our guys. Hopefully they will get out of the way. return ['guard']; # Now some strategy. if self.numberOfFoes > self.numberOfFriends: # We have less bots than the enemy. Let's play it safe until the numbers are back in our favor. # Head to the rally point if possible. Fight if someone gets in the way, but be passive otherwise. if (self.location == self.rallyPoint): return ['guard']; moveToPoint = rg.toward(self.location, self.rallyPoint); if 'normal' in rg.loc_types(moveToPoint): return ['move', moveToPoint]; else: # We can't move. Attack if we can. for loc in adjacentLocations: if loc in game.robots: if game.robots[loc].player_id != self.player_id: return self.attackOrSuicide(loc, game.robots[loc]); # Guard if we can't move. return ['guard']; else: # We have the numbers advantage. Start hunting. return ['guard'];
def act(self, game): nearest = self.nearestEnemy(game) print rg.loc_types(nearest) if rg.dist(self.location, nearest) <= 1: return ['attack', nearest] elif ('obstacle' not in rg.loc_types(nearest) or 'spawn' not in rg.loc_types(nearest)): return ['move', rg.toward(self.location, nearest)] else: return ['guard']
def init_obstacles(): self.obstacles = np.ones((19, 19)) * np.nan for i in range(19): for j in range(19): if 'invalid' in rg.loc_types( (i, j)) or 'obstacle' in rg.loc_types((i, j)): self.obstacles[i, j] = np.inf for _, bot in game.get('robots').items(): self.obstacles[tuple(bot["location"])] = enemy_cell if bot[ "player_id"] != self.player_id else friendly_cell
def move(bot, game): x, y = bot.location RIGHT, UP, LEFT, DOWN = (1, 0), (0, -1), (-1, 0), (0, 1) prio = () result = False if rg.locs_around(tuple(bot.location), filter_out=('invalid', 'obstacle', 'normal')): #We are out of order return False if x <= 9 and y > 9: #lower left quadrant; moving right and down priority if bot.hp >= CRITICAL_HP: prio = (DOWN, RIGHT, UP, LEFT) else: prio = (LEFT, UP, RIGHT, DOWN) elif x > 9 and y >= 9: #lower right; right and up if bot.hp >= CRITICAL_HP: prio = (RIGHT, UP, LEFT, DOWN) else: prio = (DOWN, LEFT, UP, RIGHT) elif x > 9 and y <= 9: #upper right; up and left if bot.hp >= CRITICAL_HP: prio = (UP, LEFT, DOWN, RIGHT) else: prio = (RIGHT, DOWN, LEFT, UP) elif x <= 9 and y <= 9: #upper left; left and down if bot.hp >= CRITICAL_HP: prio = (LEFT, DOWN, RIGHT, UP) else: prio = (UP, RIGHT, DOWN, LEFT) else: prio = (UP,) for d in prio: dest = (x + d[0], y + d[1]) occupied = False for loc, nbot in game.get('robots').items(): if bot.hp >= CRITICAL_HP and nbot.hp < CRITICAL_HP and nbot.get("location") == dest: continue if occupied: #if it is occupied by an ally, check for a less prioritized move, but if not, try moving anyway result = ["move", dest] continue elif "spawn" in rg.loc_types(dest) or "invalid" in rg.loc_types(dest): continue elif bot.hp < CRITICAL_HP: x2 = dest[0] + d[0] y2 = dest[1] + d[1] # Also check the tile after, so we go in the second row instead if "spawn" in rg.loc_types((x2,y2)): continue else: result = ["move", dest] else: result = ["move", dest] break return result
def entireBoard(self): board = [] lmin = 0 lmax = 18 for x in range(lmin, lmax + 1): for y in range(lmin, lmax + 1): loc = (x,y) if 'normal' in rg.loc_types(loc) or 'spawn' in rg.loc_types(loc): board.append((x,y)) return board
def act(self, game): wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} wejscia = {poz for poz in wszystkie if 'spawn' in rg.loc_types(poz)} zablokowane = { poz for poz in wszystkie if 'obstacle' in rg.loc_types(poz) } druzyna = { poz for poz in game.robots if game.robots[poz].player_id == self.player_id } wrogowie = set(game.robots) - druzyna sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie sasiednie_wrogowie2 = { poz for poz in sasiednie if (set(rg.locs_around(poz)) & wrogowie) } - druzyna bezpieczne = sasiednie - sasiednie_wrogowie - sasiednie_wrogowie2 - wejscia - druzyna def mindist(bots, poz): return min(bots, key=lambda x: rg.dist(x, poz)) if wrogowie: najblizszy_wrog = mindist(wrogowie, self.location) else: najblizszy_wrog = rg.CENTER_POINT # działanie domyślne: ruch = ['guard'] if self.location in wejscia: if bezpieczne: ruch = ['move', mindist(bezpieczne, rg.CENTER_POINT)] if self.location == rg.CENTER_POINT: ruch = ['guard'] if sasiednie_wrogowie: if 9 * len(sasiednie_wrogowie) >= self.hp: if bezpieczne: ruch = ['move', mindist(bezpieczne, rg.CENTER_POINT)] else: ruch = ['attack', sasiednie_wrogowie.pop()] if sasiednie_wrogowie2: ruch = ['attack', sasiednie_wrogowie2.pop()] if bezpieczne: ruch = ['move', mindist(bezpieczne, najblizszy_wrog)] return ruch
def act(self, game): global runda_numer, wybrane_ruchy if game.turn != runda_numer: runda_numer = game.turn wybrane_ruchy = set() def ruszaj(loc): wybrane_ruchy.add(loc) return ['move', loc] def stoj(act, loc=None): wybrane_ruchy.add(self.location) return [act, loc] wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} wejscia = {loc for loc in wszystkie if 'spawn' in rg.loc_types(loc)} zablokowane = {loc for loc in wszystkie if 'obstacle' in rg.loc_types(loc)} druzyna = {loc for loc in game.robots if game.robots[loc].player_id == self.player_id} wrogowie = set(game.robots)-druzyna sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie sasiednie_wrogowie2 = {loc for loc in sasiednie if (set(rg.locs_around(loc)) & wrogowie)} - druzyna bezpieczne = sasiednie - sasiednie_wrogowie - sasiednie_wrogowie2 - wejscia - druzyna - wybrane_ruchy def mindist(bots, loc): return min(bots, key=lambda x: rg.dist(x, loc)) if wrogowie: najblizszy_wrog = mindist(wrogowie,self.location) else: najblizszy_wrog = rg.CENTER_POINT # akcja domyślna, którą nadpiszemy, jak znajdziemy coś lepszego ruch = ['guard'] if self.location in wejscia: if bezpieczne: ruch = ['move', mindist(bezpieczne, rg.CENTER_POINT)] elif sasiednie_wrogowie: if 9*len(sasiednie_wrogowie) >= self.hp: if bezpieczne: ruch = ['move', mindist(bezpieczne, rg.CENTER_POINT)] else: ruch = ['attack', sasiednie_wrogowie.pop()] elif sasiednie_wrogowie2 and self.location not in wybrane_ruchy: #ruch = ['attack', sasiednie_wrogowie2.pop()] if sasiednie_wrogowie: ruch = stoj('attack',sasiednie_wrogowie.pop()) elif bezpieczne: #ruch = ['move', mindist(bezpieczne, najblizszy_wrog)] ruch = ruszaj(mindist(bezpieczne, najblizszy_wrog)) return ruch
def can_move(loc, game, spawnok=False): """ Returns True if loc is an unoccupied, normal location :param loc: Proposed location :param game: Current game data :param spawnok: Is it OK to move to a spawn zone location? """ # In GitHub version of rgkit, rg.loc_types(loc) returns a set, and website # version it returns a list. Cast as set. Website uses Python < 2.7, so no # set literals if spawnok: return set(rg.loc_types(loc)) | set(['spawn']) == set(['normal', 'spawn']) and loc not in game['robots'] return set(rg.loc_types(loc)) == set(['normal']) and loc not in game['robots']
def ring_search(self,src,wdist=1,inclusive=False): result=[] try: for x in range(src[0]-wdist,src[0]+wdist+1): for y in range(src[1]-wdist,src[1]+wdist+1): xy=x,y if'obstacle'in rg.loc_types(xy):continue if'invalid'in rg.loc_types(xy):continue if inclusive: if rg.wdist(src,xy)<=wdist:result.append(xy) if not inclusive: if rg.wdist(src,xy)==wdist:result.append(xy) except TypeError,e:raise Exception('Typeerror %s, src = %s and wdist = %s'%(e,src,wdist)) return set(result)
def act(self, game): # wszystkie pola wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} # punkty wejścia wejscia = {poz for poz in wszystkie if 'spawn' in rg.loc_types(poz)} # pola zablokowane zablokowane = { poz for poz in wszystkie if 'obstacle' in rg.loc_types(poz) } przyjaciele = { poz for poz in game.robots if game.robots[poz].player_id == self.player_id } # pola zajęte przez wrogów wrogowie = set(game.robots) - przyjaciele # pola sąsiednie sasiednie = set(rg.locs_around(self.location)) - zablokowane # pola sąsiednie zajęte przez wrogów wrogowie_obok = sasiednie & wrogowie wrogowie_obok2 = { poz for poz in sasiednie if (set(rg.locs_around(poz)) & wrogowie) } - przyjaciele # pola bezpieczne bezpieczne = sasiednie - wrogowie_obok - wejscia - przyjaciele # działania domyslne: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jeteś wpkt wejścia opuść go if self.location in wejscia: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w środku broń się if self.location == rg.CENTER_POINT: ruch = ['guard'] # jeżeli obok są przeciwnicy, atakuj o ile to bezpieczne if wrogowie_obok: if 9 * len(wrogowie_obok) >= self.hp: if bezpieczne: ruch['move', bezpieczne.pop()] else: ruch = ['attack', wrogowie_obok.pop()] if wrogowie_obok2: ruch = ['attack', wrogowie_obok2.pop()] return ruch
def act(self, game): # wszystkie pola wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} # punkty wejścia wejscia = {poz for poz in wszystkie if 'spawn' in rg.loc_types(poz)} # pola zablokowane zablokowane = {poz for poz in wszystkie if 'obstacle' in rg.loc_types(poz)} # pola zajęte przez nasze roboty przyjaciele = {poz for poz in game.robots if game.robots[poz].player_id == self.player_id} # pola zajęte przez wrogów wrogowie = set(game.robots) - przyjaciele # pola sąsiednie sasiednie = set(rg.locs_around(self.location)) - zablokowane # pola sąsiednie zajęte przez wrogów wrogowie_obok = sasiednie & wrogowie wrogowie_obok2 = {poz for poz in sasiednie if (set(rg.locs_around(poz)) & wrogowie)} - przyjaciele # pola bezpieczne bezpieczne = sasiednie - wrogowie_obok - wejscia - przyjaciele # funkcja znajdująca najsłabszego wroga obok z podanego zbioru (bots) def minhp(bots): return min(bots, key=lambda x: game.robots[x].hp) # działanie domyślne: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w punkcie wejścia, opuść go if self.location in wejscia: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w środku, broń się if self.location == rg.CENTER_POINT: ruch = ['guard'] # jeżeli obok są przeciwnicy, atakuj, o ile to bezpieczne, # najsłabszego wroga if wrogowie_obok: if 9*len(wrogowie_obok) >= self.hp: if bezpieczne: ruch = ['move', bezpieczne.pop()] else: ruch = ['attack', minhp(wrogowie_obok)] if wrogowie_obok2: ruch = ['attack', wrogowie_obok2.pop()] return ruch
def spawn(self, game, current_return): if current_return != ['empty']: return current_return #If bot is in spawn, determine how to move out. if rg.loc_types(self.location) == 'spawn': possible_locations = rg.locs_around(self.location, filter_out=('invalid', 'obstacle', 'spawn')) enemy_locs = [] for locs in possible_locations: if locs in game.robots and game.robots[ locs].player_id != self.player_id: enemy_locs.append(locs) for enemy in enemy_locs: if enemy.hp < 9: return ['attack', enemy] elif enemy in possible_locations: possible_locations.remove(enemy) best_option = [100, (0, 0)] for locs in possible_locations: current = [rg.wdist(locs, rg.CENTER_POINT), locs] if current[0] <= best_option[0]: best_option = current return ['move', best_option[1]] return current_return
def act(self, game): # if we're on one of the corners, guard if self.location in self.corners: return ['guard'] # if there are enemies around, attack them for loc, bot in game.robots.iteritems(): if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: return ['attack', loc] #elif rg.dist(loc, self.location) <= 2: # new_loc = rg.toward(self.location, # loc) # return ['move', new_loc] distances = {} for corner in self.corners: distances[corner] = rg.dist(self.location, corner) new_loc = rg.toward(self.location, min(distances, key=distances.get)) if 'obstacle' in rg.loc_types(new_loc): return ['guard'] else: return ['move', new_loc]
def act(self, game): # Attack nearby robots. Suicide if we are low on health. for loc, bot in game.robots.iteritems(): if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: if self.hp <= 11 and bot.hp <= 15: return ['suicide'] else: return ['attack', loc] # Find the nearest enemy bot and move towards it if we can. Otherwise head to the center. distanceToEnemy = 50 enemyLocation = rg.CENTER_POINT for loc, bot in game.robots.iteritems(): if bot.player_id != self.player_id: distance = rg.dist(loc, self.location) if distanceToEnemy > distance: distanceToEnemy = distance enemyLocation = loc moveToPoint = rg.toward(self.location, loc) if moveToPoint == self.location: return ['guard'] if 'normal' in rg.loc_types(moveToPoint): return ['move', moveToPoint] else: return ['guard']
def act(self, game): # Move out of spawning zone if 'spawn' in rg.loc_types(self.location): loc = self.get_open_adjacent(game) if loc: return ['move', loc] else: # All adjacent are occupied, or still in the spawning zone. loc = self.get_open_adjacent(game, spawnok=True) if loc: return ['move', loc] # Attack neighbours adj_enemies = self.get_adjacent_bots(game) if adj_enemies: if self.is_worth_dying(adj_enemies): return ['suicide'] return ['attack', next(iter(adj_enemies.keys()))] # Move away from friendlies adj_friendlies = self.get_adjacent_bots(game, enemies=False) for adj in adj_friendlies: x, y = self.location ax, ay = adj loc = (x + x - ax, y + y - ay) if can_move(loc, game): return ['move', loc] # Squat return ['guard']
def in_danger(self,game): if 'spawn' in rg.loc_types(self.location): return True for location in rg.locs_around(self.location): if location in game['robots']: return True return False
def location_score(player_id, location, game): loc_types = rg.loc_types(location) score = 4 if 'invalid' not in loc_types and 'spawn' not in loc_types and 'obstacle' not in loc_types: for loc in rg.locs_around(location): surr_loc_types = rg.loc_types(loc) if loc in game.robots: if game.robots[loc].player_id != player_id: score -= 1 else: score += 1 if 'invalid' in surr_loc_types or 'spawn' in surr_loc_types: score -= 1 return score else: return -1
def tryEscapeSpawnIfApplicable(self): if not ('spawn' in rg.loc_types(self.me.location)): return None # N/A #print "escaping spawn" walker = Walker(self.me, PathComputer(["spawn"])) moveOrNone = walker.tryGoTowards(rg.CENTER_POINT) if moveOrNone: return Move(moveOrNone, "SpawnEscaper 1") walker = Walker(self.me, PathComputer([])) moveOrNone = walker.tryGoTowards(rg.CENTER_POINT) if moveOrNone: #print "gameMap: %s" % _gameView.getGameMap(moveOrNone.getBasicMove()[1][0], moveOrNone.getBasicMove()[1][1]) return Move(moveOrNone, "SpawnEscaper 2") pc = PathComputer(["spawn"]) locAround = pc.locationsAround(self.me.location) if locAround: #print "gameMap: %s" % _gameView.getGameMap(locAround[0][0], locAround[0][1]) return Move(["move", locAround[0]], "SpawnEscaper 3") pc = PathComputer([]) locAround = pc.locationsAround(self.me.location) if locAround: #print "gameMap: %s" % _gameView.getGameMap(locAround[0][0], locAround[0][1]) return Move(["move", locAround[0]], "SpawnEscaper 4") #print "could not escape spawn" return None
def prepare_backdrop(self, win): self._win.create_rectangle(0, 0, self._winsize, self._winsize + self._blocksize, fill='#555', width=0) self._win.create_rectangle(0, self._winsize, self._winsize, self._winsize + self._blocksize * 15/4, fill='#333', width=0) for x in range(self._settings.board_size): for y in range(self._settings.board_size): rgb = self._settings.normal_color if "normal" in rg.loc_types((x, y)) else self._settings.obstacle_color self.draw_grid_object((x, y), fill=rgb_to_hex(*rgb), layer=1, width=0)
def act(self, game): if 'spawn' in rg.loc_types(self.location): return ['move', rg.toward(self.location, rg.CENTER_POINT)] my_friendly_neighbours = self.get_neighbours(game) if my_friendly_neighbours: return ['move', rg.toward(self.location, rg.CENTER_POINT)] bad_neighbours = self.get_neighbours(game, friendly=False, onlyspawn=False) # CAMP THE SPAWN! SHOOT NOW! shoot = 'attack' if bad_neighbours: if random.randint(0, 10) > 7: print('HAHAHA! DIE DIE DIE!') return [shoot, bad_neighbours[0]['location']] friends = [ bot for bot in game.robots.values() if bot['player_id'] == self.player_id ] def check_friend(current_close_friend, new_friend): if not current_close_friend: return new_friend if rg.wdist(self.location, new_friend.location) < rg.wdist( self.location, current_close_friend.location): return new_friend return current_close_friend closest_friend = reduce(check_friend, friends, None) return ['guard']
def act(self, game): my_team = self.player_id if 'spawn' in rg.loc_types(self.location): return ['move', rg.toward(self.location, rg.CENTER_POINT)] else: # Get the nearby locations near_locations = rg.locs_around(self.location) # For each location see if there is a robot there or not... for loc in near_locations: try: robot_location = game.robots[loc] # There is... # If it is our own robot, just move to the center one step. Stop moving when we reach the center. if game.robots[loc][ 'player_id'] == my_team and self.location != rg.CENTER_POINT: return [ 'move', rg.toward(self.location, rg.CENTER_POINT) ] elif game.robots[loc]['player_id'] != my_team: return ['attack', loc] except KeyError: # There are no robots there... pass return ['guard']
def act(self, game): # Find nearest enemy enemies = {} nearest_enemy = (1000000, rg.CENTER_POINT) for loc, bot in game.robots.iteritems(): if bot.player_id != self.player_id: enemies[loc] = bot distance = rg.dist(loc, self.location) if distance < nearest_enemy[0]: nearest_enemy = (distance, loc) # If nearest enemy is next to us, ATTACK or KABOOM! if nearest_enemy[0] <= 1: potential_suicide_damage = 0 for loc in rg.locs_around(self.location): if loc in enemies: potential_suicide_damage += min(15, enemies[loc].hp) if self.hp < potential_suicide_damage: return ['suicide'] else: return ['attack', nearest_enemy[1]] # If we cannot move, defend. desired_move = rg.toward(self.location, nearest_enemy[1]) if rg.loc_types(desired_move) in ['obstacle', 'invalid']: return ['guard'] # Otherwise, advance on the enemy without mercy return ['move', desired_move]
def act(self, game): # Move out of spawning zone if 'spawn' in rg.loc_types(self.location): loc = self.get_open_adjacent(game) if loc: return ['move', loc] else: # All adjacent are occupied, or still in the spawning zone. loc = self.get_open_adjacent(game, spawnok=True) if loc: return ['move', loc] # Find an enemy loc, d = self.get_closest_enemy(game) if d < 0: # The are no enemies return ['guard'] elif d <= 1: # Enemy within range if self.is_worth_dying(game): return ['suicide'] return ['attack', loc] else: # Enemy out of range closer = rg.toward(self.location, loc) if can_move(closer, game): return ['move', closer] loc = self.get_open_adjacent(game) if loc: return ['move', loc] return ['guard']
def coward(self, game): # First, make sure you are not in a spawn point if 'spawn' in rg.loc_types(self.location): #Move out of spawn point! return ['move', rg.toward(self.location, rg.CENTER_POINT)] # second, make sure you are not next to an enemy locations = rg.locs_around(self.location, filter_out=('invalid', 'obstacle')) enemies = [] for l in locations: bot = game['robots'].get(l) if bot and bot['player_id'] != self.player_id: enemies.append(bot) if enemies: #Find a safe place! for l in locations: if not game['robots'].get(l): # unoccupied spot! return ['move', l] else: # surrounded! if self.hp < rg.settings.attack_range[1] * len(enemies): return ['suicide'] else: target = min(enemies, key=lambda x: x['hp']) return ['attack', target['location']] # finally, just chill out. return ['guard']
def act(self, game): # wyznaczamy zbiory różnych pól na planszy wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} wejscia = {loc for loc in wszystkie if 'spawn' in rg.loc_types(loc)} zablokowane = { loc for loc in wszystkie if 'obstacle' in rg.loc_types(loc) } druzyna = { loc for loc in game.robots if game.robots[loc].player_id == self.player_id } wrogowie = set(game.robots) - druzyna sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie sasiednie_wrogowie2 = { loc for loc in sasiednie if (set(rg.locs_around(loc)) & wrogowie) } - druzyna bezpieczne = sasiednie - sasiednie_wrogowie - sasiednie_wrogowie2 - wejscia - druzyna def mindist(bots, loc): return min(bots, key=lambda x: rg.dist(x, loc)) if wrogowie: najblizszy_wrog = mindist(wrogowie, self.location) else: najblizszy_wrog = rg.CENTER_POINT ruch = ['guard'] if self.location in wejscia: if bezpieczne: ruch = ['move', mindist(bezpieczne, rg.CENTER_POINT)] elif sasiednie_wrogowie: if 9 * len(sasiednie_wrogowie) < self.hp: ruch = ['attack', sasiednie_wrogowie.pop()] elif bezpieczne: ruch = ['move', mindist(bezpieczne, rg.CENTER_POINT)] elif sasiednie_wrogowie2: ruch = ['attack', sasiednie_wrogowie2.pop()] elif bezpieczne: ruch = ['move', mindist(bezpieczne, najblizszy_wrog)] return ruch
def act(self, game): all_locs = {(x, y) for x in xrange(19) for y in xrange(19)} spawn = {loc for loc in all_locs if 'spawn' in rg.loc_types(loc)} obstacle = {loc for loc in all_locs if 'obstacle' in rg.loc_types(loc)} team = { loc for loc in game.robots if game.robots[loc].player_id == self.player_id } enemy = set(game.robots) - team adjacent = set(rg.locs_around(self.location)) - obstacle adjacent_enemy = adjacent & enemy adjacent_enemy2 = { loc for loc in adjacent if (set(rg.locs_around(loc)) & enemy) } - team safe = adjacent - adjacent_enemy - adjacent_enemy2 - spawn - team def mindist(bots, loc): return min(bots, key=lambda x: rg.dist(x, loc)) if enemy: closest_enemy = mindist(enemy, self.location) else: closest_enemy = rg.CENTER_POINT # we'll overwrite this if there's something better to do move = ['guard'] if self.location in spawn: if safe: move = ['move', mindist(safe, rg.CENTER_POINT)] elif adjacent_enemy: if 9 * len(adjacent_enemy) >= self.hp: if safe: move = ['move', mindist(safe, rg.CENTER_POINT)] else: move = ['attack', adjacent_enemy.pop()] elif adjacent_enemy2: move = ['attack', adjacent_enemy2.pop()] elif safe: move = ['move', mindist(safe, closest_enemy)] return move
def ring_search(self, src, wdist=1, inclusive=False): """Give me all locations that are within wdist of src excluding src""" result = [] try: for x in range(src[0] - wdist, src[0] + wdist + 1): for y in range(src[1] - wdist, src[1] + wdist + 1): xy = (x, y) if 'obstacle' in rg.loc_types(xy): continue if 'invalid' in rg.loc_types(xy): continue if inclusive: if rg.wdist(src, xy) <= wdist: result.append(xy) if not inclusive: if rg.wdist(src, xy) == wdist: result.append(xy) except TypeError, e: raise Exception("Typeerror %s, src = %s and wdist = %s" % (e, src, wdist))
def act(self, game): # wszystkie pola wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} # punkty wejścia (spawn) wejscia = {loc for loc in wszystkie if 'spawn' in rg.loc_types(loc)} # pola zablokowane (obstacle) zablokowane = { loc for loc in wszystkie if 'obstacle' in rg.loc_types(loc) } # pola zajęte przez nasze roboty druzyna = { loc for loc in game.robots if game.robots[loc].player_id == self.player_id } # pola zajęte przez wrogów wrogowie = set(game.robots) - druzyna # for loc, bot in game.robots.iteritems(): # if bot.player_id != self.player_id: # if rg.dist(loc, self.location) <= 1: # return ['attack', loc] wejscia = {loc for loc in wszystkie if 'spawn' in rg.loc_types(loc)} sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie if sasiednie_wrogowie: if 9 * len(sasiednie_wrogowie) < self.hp: return ['attack', sasiednie_wrogowie.pop()] if self.location in wejscia: move = ['move', rg.toward(self.location, rg.CENTER_POINT)] if self.location == rg.CENTER_POINT: return ['guard'] return ['move', rg.toward(self.location, rg.CENTER_POINT)]
def get_attack_goodness(self, loc, game): """Returns how 'good' attacking a certain location is. Based upon the number of friendlies and enemies next to the location, any bot that is in the location, etc.""" types = rg.loc_types(loc) enemies_next_to_loc = self.get_enemy_bots_next_to(loc, game) friendlies_next_to_loc = self.get_friendlies_next_to(loc, game) nearby_friendlies_in_trouble = [] for friendly in friendlies_next_to_loc: if bot_is_in_trouble(friendly, game): nearby_friendlies_in_trouble.append(friendly) nearby_enemies_in_trouble = [] for enemy in enemies_next_to_loc: if bot_is_in_trouble(enemy, game): nearby_enemies_in_trouble.append(enemy) robot = get_bot_in_location(loc, game) goodness = 0 if robot: if robot.player_id == self.player_id: # we're attacking a friendly's location # no enemy's gonna move into them... goodness -= 5 else: #attacking an enemy is good goodness += (100 - robot.hp) / 50.0 * 20 else: # no bot is at the location # so base the goodness on how likely it is for bots to move there #more enemies that can move into the location, the better # weighted by 3 because even if there are two other friendlies # next to the loc, we still want to attack if it's the only square # an enemy is next to goodness += len(enemies_next_to_loc) * 3 #enemies aren't too likely to move next to a friendly goodness -= len(friendlies_next_to_loc) # if there are enemies in trouble nearby, we want to try and catch # them escaping! goodness += len(nearby_enemies_in_trouble) * 5 # nearby friendlies in trouble will definitely want to escape this # turn # maybe to this square goodness -= len(nearby_friendlies_in_trouble) # don't attack where an ally is already moving to # or attacking, at least not too much if loc in future_moves: goodness -= 20 elif loc in future_attacks: goodness -= 3 return goodness
def act(self, game): empties = [] oppos = {} oppo_hp = 0 robots = game.get('robots') for loc in self.around(self.location): obj = robots.get(loc, None) if obj: if obj['player_id'] != self.player_id: oppos[loc] = obj oppo_hp += obj['hp'] else: if game['turn'] % 10 == 9 and 'spawn' in rg.loc_types(loc): pass # don't send robo for death! else: empties.append(loc) if empties and game.get("turn") % 10 >= 8 and 'spawn' in rg.loc_types( self.location): #print 'away from spawn %s' % str(self.location) return self.away(robots, empties) if empties and oppos: if self.hp < 15 or len(oppos) > 1: # or self.hp < oppo_hp: return self.away(robots, empties) return self.attack(robots, oppos) if not empties and len(oppos) >= 1: if self.hp <= 9 * len(oppos): return ['suicide'] minhp = 99 minopo = None for oloc, oppo in oppos.items(): if oppo['hp'] < minhp: minopo = oppo minhp = oppo['hp'] return ['attack', minopo['location']] if not oppos and empties: cand = self.tryToHitOpo(robots, empties, game['turn']) if cand: return cand if empties: return self.away(robots, empties) return ['guard']
def plan_move(self, game, new_loc): if new_loc == self.location and self.location == rg.CENTER_POINT: return ['guard'] if new_loc == self.location: return ['move', rg.toward(self.location, rg.CENTER_POINT)] around_me = rg.loc_types(new_loc) if around_me == ['normal']: return ['move', rg.toward(self.location, new_loc)] else: return None
def act(self, game): # wszystkie pola wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} # punkty wejścia wejscia = {poz for poz in wszystkie if 'spawn' in rg.loc_types(poz)} # pola zablokowane zablokowane = {poz for poz in wszystkie if 'obstacle' in rg.loc_types(poz)} # pola zajęte przez nasze roboty przyjaciele = {poz for poz in game.robots if game.robots[poz].player_id == self.player_id} # pola zajęte przez wrogów wrogowie = set(game.robots) - przyjaciele # pola sąsiednie sasiednie = set(rg.locs_around(self.location)) - zablokowane # pola sąsiednie zajęte przez wrogów wrogowie_obok = sasiednie & wrogowie for wrog in wrogowie: if rg.wdist(poz, self.location) == 2: # ruch = działanie xD # działanie domyślne: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w punkcie wejścia, opuść go if self.location in wejscia: ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] # jeżeli jesteś w środku, broń się if self.location == rg.CENTER_POINT: ruch = ['guard'] # jeżeli obok są przeciwnicy, atakuj if len(wrogowie_obok) > 2 and self.hp < 27: ruch = ['suicide'] else wrogowie_obok: ruch = ['attack', wrogowie_obok.pop()] return ruch
def diagonals(loc, filter_out=None): """like rg.locs_around, but gives up to 4 corners instead of 4 orthogonal directions """ filter_out = filter_out or [] offsets = ((1, 1), (1, -1), (-1, -1), (-1, 1)) locs = [] for o in offsets: new_loc = tuple(map(operator.add, loc, o)) if len(set(filter_out) & set(rg.loc_types(new_loc))) == 0: locs.append(new_loc) return locs
def get_neighbours(self, game, friendly=True, onlyspawn=True): if friendly: player_id = self.player_id else: player_id = (self.player_id + 1) % 2 return [ bot for bot in game.robots.values() if bot['player_id'] == player_id and ( not onlyspawn or 'spawn' in rg.loc_types(bot.location)) and bot.location in rg.locs_around(self.location) ]
def act(self, game): team = {loc for loc in game.robots if game.robots[loc].player_id == self.player_id} if(len(team) < 9): if self.location == rg.CENTER_POINT: return ['guard'] if rg.loc_types(self.location) == "spawn": return ['move', rg.toward(self.location, rg.CENTER_POINT)] # if there are enemies around, attack them for loc, bot in game.robots.iteritems(): if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1: return ['attack', loc] return ['move', rg.toward(self.location, rg.CENTER_POINT)] else: for loc, bot in game.robots.iteritems(): if bot.player_id != self.player_id and self.hp <= 8: if rg.dist(loc, self.location) <= 1: return ['suicide'] if bot.player_id != self.player_id: if rg.dist(loc, self.location) <= 1 and self.hp >= 8: return ['attack', loc] # return something if rg.loc_types(self.location) == "spawn": return ['move', rg.toward(self.location, rg.CENTER_POINT)] if game.turn <= 49: return ['move', rg.toward(self.location, (15,9))] if(game.turn > 49 and game.turn < 70): return ['move', rg.toward(self.location, (3, 9))] if(game.turn >= 70): return ['move', rg.toward(self.location, (15, 9))] return ['move', rg.toward(self.location, rg.CENTER_POINT)]
def two_away(loc, filter_out=None): """like rg.locs_around, but gives all spaces that are 2 steps away """ filter_out = filter_out or [] offsets = ((2, 0), (1, 1), (0, 2), (-1, 1), (-2, 0), (-1, -1), (0, -2), (1, -1)) locs = [] for o in offsets: new_loc = tuple(map(operator.add, loc, o)) if len(set(filter_out) & set(rg.loc_types(new_loc))) == 0: locs.append(new_loc) return locs
def act(self, game): # zbiory pól na planszy # wszystkie pola wszystkie = {(x, y) for x in xrange(19) for y in xrange(19)} # punkty wejścia (spawn) wejscia = {loc for loc in wszystkie if 'spawn' in rg.loc_types(loc)} # pola zablokowane (obstacle) zablokowane = { loc for loc in wszystkie if 'obstacle' in rg.loc_types(loc) } # pola zajęte przez nasze roboty druzyna = { loc for loc in game.robots if game.robots[loc].player_id == self.player_id } # pola zajęte przez wrogów wrogowie = set(game.robots) - druzyna sasiednie = set(rg.locs_around(self.location)) - zablokowane sasiednie_wrogowie = sasiednie & wrogowie sasiednie_wrogowie2 = { loc for loc in sasiednie if (set(rg.locs_around(loc)) & wrogowie) } - druzyna bezpieczne = sasiednie - sasiednie_wrogowie - sasiednie_wrogowie2 - wejscia - druzyna print wejscia # domyślne działanie robota: ruch do środka ruch = ['move', rg.toward(self.location, rg.CENTER_POINT)] return ruch