summaryrefslogtreecommitdiff
path: root/capturer.py
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2014-04-10 20:11:12 +1000
committerPeter Ward <peteraward@gmail.com>2014-04-10 20:11:12 +1000
commit9acf1ee8064b6c7bbf5446085eac769effb0df13 (patch)
tree755bd09097b6301f0ff2837f2e4e0fdbe53e4e84 /capturer.py
parent5cd48c7ecf008d2baf0523dc3adfeb28d13bb417 (diff)
split out bots into separate files
Diffstat (limited to 'capturer.py')
-rw-r--r--capturer.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/capturer.py b/capturer.py
new file mode 100644
index 0000000..a6bb990
--- /dev/null
+++ b/capturer.py
@@ -0,0 +1,69 @@
+import random
+
+import robots
+
+from robots.algorithms import distance_relaxer
+from robots.constants import City
+from robots.utils import add_spawns
+
+class CaptureSpawns(object):
+ def __init__(self, variance=0.1):
+ self.distances = None
+ self.iterations = 10
+ self.variance = variance
+
+ def __call__(self, whoami, state):
+ my_robots = state.robots_by_player[whoami]
+
+ self.distances = [
+ [
+ {
+ City.NORMAL: (
+ float('inf')
+# if self.distances is None or self.distances[y][x] is None
+# else self.distances[y][x] + self.iterations // 2
+ ),
+ City.FACTORY: (
+ 0
+ if state.allegiances.get((x, y)) != whoami else
+ float('inf')
+ ),
+ City.GHOST: None,
+ }[city]
+ for x, city in enumerate(row)
+ ]
+ for y, row in enumerate(state.cities)
+ ]
+
+ predecessors = distance_relaxer(self.distances, None)
+
+ results = []
+ for x, y, energy in my_robots:
+ if random.random() < self.variance:
+ result = random.choice('ULDR')
+ elif predecessors[y][x]:
+ result = predecessors[y][x]
+ elif state.allegiances.get((x, y)) != whoami:
+ result = 'P'
+ else:
+ result = random.choice('UDLR')
+ results.append(result)
+ return results
+
+if __name__ == '__main__':
+# random.seed(42)
+ map_ = robots.border_map(40, 30, 0)
+
+ add_spawns(map_, 300, 'X')
+ add_spawns(map_, 20, '+')
+
+ for y in range(20):
+ map_[y][20] = 'X'
+
+ add_spawns(map_, 6)
+
+ game = robots.Game(map_)
+ game.add_bot(CaptureSpawns(variance=0), 'Alice')
+ game.add_bot(CaptureSpawns(variance=0.01), 'Bob')
+ viewer = robots.CursesViewer(game)
+ viewer.run()