From 7daa5284f9eddf6d4b4e7838919e80ce25324bb0 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Sun, 25 Sep 2022 00:10:13 +1000 Subject: Lots of changes! --- example-bots/pathing.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 example-bots/pathing.py (limited to 'example-bots/pathing.py') diff --git a/example-bots/pathing.py b/example-bots/pathing.py new file mode 100644 index 0000000..d537940 --- /dev/null +++ b/example-bots/pathing.py @@ -0,0 +1,40 @@ +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 -- cgit v1.2.3