import random def move_toward_closest_factory(whoami, state, rx, ry): if state[rx, ry].allegiance != whoami and state[rx, ry].city == '+': return 'P' to_do = [ (rx + 1, ry, 'R'), (rx - 1, ry, 'L'), (rx, ry - 1, 'U'), (rx, ry + 1, 'D') ] random.shuffle(to_do) seen = [] while to_do: (x, y, move) = to_do.pop(0) if (x, y) in seen or state[x, y].city == 'X': continue seen.append((x, y)) if state[x, y].allegiance != whoami and state[x, y].city == '+': return move else: to_add = [(x + 1, y, move), (x - 1, y, move), (x, y + 1, move), (x, y - 1, move)] random.shuffle(to_add) to_do += to_add def calculate_orders(whoami, state): orders = [] my_robots = state.robots_by_player[whoami] for rx, ry, energy in my_robots: move = move_toward_closest_factory(whoami, state, rx, ry) if move and random.random() > 0.1: orders.append(move) else: orders.append(random.choice('UDLRP')) return orders