From 833f87ebba90e6c2d3cb386b2d51c3d113d98a54 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Sat, 18 Oct 2014 12:57:42 +1100 Subject: dump of random stuff from This commit should really be re-done, better. --- manhattan.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 manhattan.py (limited to 'manhattan.py') diff --git a/manhattan.py b/manhattan.py new file mode 100644 index 0000000..f64b1e2 --- /dev/null +++ b/manhattan.py @@ -0,0 +1,61 @@ +import random + +def distance(x1, y1, x2, y2): + ''' + >>> distance(0, 0, 3, 0) + 3 + >>> distance(1, 1, 3, 3) + 4 + ''' + return abs(x1 - x2) + abs(y1 - y2) + +def direction_to(rx, ry, fx, fy): + ''' + >>> direction_to(0, 0, 3, 0) + 'R' + >>> direction_to(0, 0, 0, 3) + 'D' + >>> direction_to(6, 0, 3, 0) + 'L' + >>> direction_to(0, 6, 0, 3) + 'U' + ''' + 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.allegiances.get((fx, fy)) != whoami: + d = distance(rx, ry, fx, fy) + if d < closest_distance: + closest_factory = (fx, fy) + closest_distance = d + + return closest_distance, closest_factory + +def manhattan(whoami, state): + orders = [] + + for rx, ry, energy in state.robots_by_player[whoami]: + 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 -- cgit v1.2.3