my_nearest_to_enemy_planets = sorted(planets_to_send_to, key=lambda p : p.distance(self.enemy_com) + p.id/1000000.0) for source_planet in self.my_planets: if self.ships_available[source_planet] > 0: #log.info("Post-Offense for %s" % source_planet) for dest_planet in my_nearest_to_enemy_planets: distance = source_planet.distance(dest_planet) if distance > 0 and distance < com_enemy_planet_distance_map[source_planet]: if com_enemy_planet_distance_map[dest_planet] < com_enemy_planet_distance_map[source_planet] and \ closest_enemy_planet_distance_map[dest_planet] <= closest_enemy_planet_distance_map[source_planet]: source_planet.send_fleet(dest_planet, self.ships_available[source_planet]) self.ships_available[source_planet] = 0 break def do_turn(self): self.all_planets = self.universe.all_planets self.my_planets = self.universe.my_planets self.enemy_planets = self.universe.enemy_planets self.nobodies_planets = self.universe.nobodies_planets if len(self.my_planets) == 0: return self.doPrep() self.doDefenseOffense() self.doPostOffense() Game(MyBot, universe_class=Universe2, planet_class=Planet2)
from planetwars import BaseBot, Game, NOBODY, ENEMIES class StillStupidBot(BaseBot): """Another stupid bot that spews out ships.""" def do_turn(self): for p in self.universe.my_planets: if p.ship_count > 30: for target in self.universe.find_planets(growth_rate=set( [5, 4, 3]), owner=NOBODY): p.send_fleet(target, 10) break Game(StillStupidBot)
from planetwars import BaseBot, Game import random from logging import getLogger log = getLogger(__name__) class LoggingBot(BaseBot): """Stupid bot that randomly spews out ships. This time with logging. While logging is enabled all exceptions your code throws will be catched and written into the logfile (including tracebacks). To enable logging start with "--log bla.log", e.g: python loggingbot.py --log logfile.log Hint: Start with -h to see a list of all options. """ def do_turn(self): log.info("I'm starting my turn") for p in self.universe.my_planets: if p.ship_count > 50: log.debug("Attacking from %s" % p) p.send_fleet(random.choice(list(self.universe.not_my_planets)), p.ship_count / 2) Game(LoggingBot)
from planetwars import BaseBot, Game import random class StupidBot(BaseBot): """Stupid bot that randomly spews out ships.""" def do_turn(self): for p in self.universe.my_planets: if p.ship_count > 50: p.send_fleet(random.choice(list(self.universe.not_my_planets)), p.ship_count / 2) Game(StupidBot)
class jbot(BaseBot): """Another bot that spews out ships.""" def do_turn(self): #Get new enemy atatcks self.universe.new_enemy_fleets = self.universe.enemy_fleets - self.universe._old_enemy_fleets #Build lists of my and neutral planets under attack self.universe.neutral_targets = [ p for p in self.universe.enemy_targets if p.owner == player.NOBODY ] self.universe.my_planets_under_attack = [ p for p in self.universe.enemy_targets if p.owner == player.ME ] planets_to_help = [ p for p in self.universe.my_targets if p.in_future(None).owner != player.ME ] self.universe.my_planets_under_attack.extend(planets_to_help) #Go through new enemy fleets and planet lists, planning attacks clean_attack_list(self.universe) determine_frontlines(self.universe) build_attack_list(self.universe) prioritize_attack_list(self.universe) self.universe.send_attacks() Game(jbot, universe_class=MyUniverse, planet_class=MyPlanet)