diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2022-09-25 00:10:13 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2022-09-25 00:38:04 +1000 |
commit | 7daa5284f9eddf6d4b4e7838919e80ce25324bb0 (patch) | |
tree | ac92d9aa97621b4c314441a4cb7cdcab60b4f85e /example-bots/manhattan.py | |
parent | e031af6e5e8324fe4cda66d9597904040b17ca80 (diff) |
Diffstat (limited to 'example-bots/manhattan.py')
-rw-r--r-- | example-bots/manhattan.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/example-bots/manhattan.py b/example-bots/manhattan.py new file mode 100644 index 0000000..a65d0e9 --- /dev/null +++ b/example-bots/manhattan.py @@ -0,0 +1,46 @@ +import random + +def distance(x1, y1, x2, y2): + return abs(x1 - x2) + abs(y1 - y2) + +def direction_to(rx, ry, fx, fy): + moves = [] + if fx < rx: + moves.append('L') + elif fx > rx: + moves.append('R') + if fy < ry: + moves.append('U') + elif fy > ry: + moves.append('D') + return random.choice(moves) + +def closest_factory(whoami, state, rx, ry): + closest_factory = None + closest_distance = 9999999 + + for fx, fy in state.factories: + if state[fx, fy].allegiance != whoami: + d = distance(rx, ry, fx, fy) + if d < closest_distance: + closest_factory = (fx, fy) + closest_distance = d + + return closest_distance, closest_factory + +def calculate_orders(whoami, state): + orders = [] + + my_robots = state.robots_by_player[whoami] + for rx, ry, energy in my_robots: + distance, factory = closest_factory(whoami, state, rx, ry) + + if factory is None or random.random() < 0.1: + orders.append(random.choice('UDLRP')) + elif distance == 0: + orders.append('P') + else: + fx, fy = factory + orders.append(direction_to(rx, ry, fx, fy)) + + return orders |