def __init__(self, board): provs = board.spaces.keys() self.defence_value = dict.fromkeys(provs, 0) self.attack_value = dict.fromkeys(provs, 0) self.strength_value = dict.fromkeys(provs, 0) self.competition_value = dict.fromkeys(provs, 0) self.proximity_map = defaultdict(lambda: [0] * PROXIMITY_DEPTH) self.adjacent_units = defaultdict(lambda: defaultdict(list))
def parse_position(stream): tokens = lex(stream) assert tokens.next() == "NOW" season = [] assert tokens.next() == "(" for token in tokens: if token == ")": break else: season.append(token) position = defaultdict(set) for token in tokens: if token == ")": # Left over from a coastline specification continue assert token == "(" power = tokens.next() unit = [] for item in tokens: if item == ")": break elif item != "(": unit.append(item) position[power].add(tuple(unit)) return season, position
def __init__(self, **kwargs): self.brain = None self.__super.__init__(**kwargs) self.log_debug(9, '%s (%s); started at %s', self.name, self.version, ctime()) self.inputs = {} self.learned = defaultdict(OrderSet) self.last_turn = None self.datc = DatcOptions()
def generate_move_combos(self): adj_provs = defaultdict(list) for unit in self.power.units: for key in unit.location.borders_out: dest = self.map.locs[key] if not any(other.nation == self.power for other in dest.province.units): adj_provs[key[1]].append(MoveOrder(unit, dest)) return [OrderCombo(self, combo) for value in adj_provs.values() for combo in sublists(value) if combo]
def dumb_movement(self, values, orders, units): waiting = defaultdict(list) our_units = list(units) while our_units: # Put our units into a random order. This is one of the ways # in which DumbBot is made non-deterministic - the order # in which the units are considered can affect the orders selected shuffle(our_units) unordered = [] for unit in our_units: if not orders.get_order(unit): order = self.order_unit(unit, orders, values, waiting) if order: orders.add(order, unit.nation) else: unordered.append(unit) our_units = unordered return orders
def generate(self): moves = defaultdict(int) board = self.game.board for x in range(9): if board[x]: # Only look at blank spaces continue for y in range(9): if y == x: continue for z in range(9): if z in (x, y): continue if x + y + z != 12: # Only look at rows continue # Count the number of rows x participates in, # to make the center worth more. moves[x] += 1 if board[y] == board[z]: if board[z] == self.player: moves[x] += 1000 elif board[z] == 0: moves[x] += 5 else: moves[x] += 500 elif board[y] == self.player and board[z] == 0: # Make this better than a blank-blank-blank row moves[x] += 20 self.game.output("%r", moves) return weighted_choice(moves)
def generate(self, msg): self.timestamp += 1 results = defaultdict(list) for rule in self.rules: output = rule.matches(msg) if output is not None: results[output].append(rule) while sum(r.n for s in results for r in results[s]) < self.values.Omna: rule = self.coverage(msg, results) output = rule.matches(msg) results[output].append(rule) self.delete() actions = dict((key, sum(r.p * r.F for r in results[key]) / sum(r.F for r in results[key])) for key in results if results[key]) action = weighted_choice(actions) action_set = results[action] brigade = self.values.g * max(actions.values()) return action, action_set, brigade
def connectionMade(self): HTTPClient.connectionMade(self) self.data = None self.make_request() self.headers = defaultdict(list)
def __init__(self, values=None, **kwargs): self.__super.__init__(**kwargs) self.vals = values or DumbBot_Values() self.log_debug(9, '%s (%s); started at %s', self.name, self.version, ctime()) self.attitude = defaultdict(lambda: 1)