summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2014-10-18 12:57:42 +1100
committerPeter Ward <peteraward@gmail.com>2014-10-18 12:57:42 +1100
commit833f87ebba90e6c2d3cb386b2d51c3d113d98a54 (patch)
tree366f13421fd9766160d704a056072598a8efa88e
parent01e53c672e239e95aa1102533b0a6cff8424000c (diff)
dump of random stuff from <reboot>
This commit should really be re-done, better.
-rw-r--r--arena.txt22
-rw-r--r--empty.txt27
-rw-r--r--large.txt31
-rw-r--r--manhattan.py61
-rw-r--r--maze.txt12
-rw-r--r--peter.py15
-rw-r--r--robots/client.py45
-rw-r--r--robots/cursesviewer.py17
-rw-r--r--robots/game.py16
-rw-r--r--two.txt15
-rw-r--r--up_bot.py5
11 files changed, 226 insertions, 40 deletions
diff --git a/arena.txt b/arena.txt
new file mode 100644
index 0000000..03ea50b
--- /dev/null
+++ b/arena.txt
@@ -0,0 +1,22 @@
+XXXXXXXXXXXXXXXX...XXXXXXXXXXXXXXX
+X.+..+..+..+..+....+..+..+..+..+.X
+X.........XXX........X.....XXX...X
+X....a......X.......XXc..........X
+X.+..+..+..+..+....+..+..+..+..+.X
+X.....XXXX...X........XXXX.......X
+X....e...........XXXXXg..........X
+X.+..+..+..+..+....+..+..+..+..+.X
+X....XXXX....X........XXXX.......X
+........iX...X...........kX.......
+..+X.+..+X.+.X+....+X.+..+X.+..+..
+X..X.....X..XX.XXX..X.....X......X
+X..X.......l....X...X.......j....X
+X.+X.+..+..+..+.X..+X.+..+..+..+.X
+X..X.....XXX....X...X.....XXX....X
+X..X.....X..XX.XXX..X.....X......X
+X..X.......h....X...X.......f....X
+X.+X.+..+..+..+.X..+X.+..+..+..+.X
+X..X.....XXX....X...X.....XXX....X
+X.............d................b.X
+X.+..+..+..+..+....+..+..+..+..+.X
+XXXXXXXXXXXXXXXX...XXXXXXXXXXXXXXX
diff --git a/empty.txt b/empty.txt
index 7a405b7..105bc1b 100644
--- a/empty.txt
+++ b/empty.txt
@@ -1,12 +1,15 @@
-XXXXXXXXXXXXXXXXXXXX
-X.....+.X.+........X
-X..1.+..X.......2..X
-X....+..X....+.....X
-X...XXXXX..........X
-X..........XXXXXX..X
-X...+...........+..X
-X...XXXXXXXX..X....X
-X.....+.......X...4X
-X.............X....X
-X+.....+..3.....+..X
-XXXXXXXXXXXXXXXXXXXX
+.................
+..+..+..+..+..+..
+.................
+.....1...........
+..+..+..+..+..+..
+.................
+........3........
+..+..+..+..+..+..
+.................
+...........2.....
+..+..+..+..+..+..
+.................
+..............4..
+..+..+..+..+..+..
+.................
diff --git a/large.txt b/large.txt
new file mode 100644
index 0000000..1fbaae8
--- /dev/null
+++ b/large.txt
@@ -0,0 +1,31 @@
+XXXXXXXXXXXXXXXXXXXXX
+X1.................+X
+X.........+.........X
+XXXXXXXXXXXXXXXXX...X
+X...............+...X
+X...................X
+X...XXXXXXXXXXXXXXXXX
+X...................X
+X...................X
+XXXXXXXXXXXXXXXXX...X
+X.........3.........X
+X.........+.........X
+X...XXXXXXXXXXXXX...X
+X.........X.........X
+X.........X.........X
+X.+.....+...+.....+.X
+X.........X.........X
+X.........X.........X
+X...XXXXXXXXXXXXX...X
+X.........+.........X
+X.........4.........X
+X...XXXXXXXXXXXXXXXXX
+X...................X
+X...................X
+XXXXXXXXXXXXXXXXX...X
+X...................X
+X...+...............X
+X...XXXXXXXXXXXXXXXXX
+X.........+.........X
+X+.................2X
+XXXXXXXXXXXXXXXXXXXXX
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
diff --git a/maze.txt b/maze.txt
new file mode 100644
index 0000000..7a405b7
--- /dev/null
+++ b/maze.txt
@@ -0,0 +1,12 @@
+XXXXXXXXXXXXXXXXXXXX
+X.....+.X.+........X
+X..1.+..X.......2..X
+X....+..X....+.....X
+X...XXXXX..........X
+X..........XXXXXX..X
+X...+...........+..X
+X...XXXXXXXX..X....X
+X.....+.......X...4X
+X.............X....X
+X+.....+..3.....+..X
+XXXXXXXXXXXXXXXXXXXX
diff --git a/peter.py b/peter.py
new file mode 100644
index 0000000..a79e545
--- /dev/null
+++ b/peter.py
@@ -0,0 +1,15 @@
+import robots
+
+from capturer import CaptureSpawns
+from simple import random_walk
+from napoleon import Napoleon
+from manhattan import manhattan
+from up_bot import up_bot
+
+server = robots.Server()
+server.add_simple_bot(up_bot, 'Simba')
+server.add_simple_bot(random_walk, 'Zazu')
+server.add_simple_bot(manhattan, 'Nala')
+server.add_bot(CaptureSpawns, 'Scar')
+server.add_bot(Napoleon, 'Mufasa')
+server.run()
diff --git a/robots/client.py b/robots/client.py
index 612edb1..45b593e 100644
--- a/robots/client.py
+++ b/robots/client.py
@@ -1,8 +1,10 @@
import argparse
+import itertools
import os
import random
import signal
-from string import ascii_lowercase as lowercase
+#from string import ascii_lowercase as lowercase
+import time
import zmq
import logbook
@@ -236,6 +238,7 @@ class RemoteBot(object):
def main():
parser = argparse.ArgumentParser()
+ parser.add_argument('-l', '--loop', default=False, action='store_true')
parser.add_argument('map', type=argparse.FileType('r'))
args = parser.parse_args()
@@ -244,24 +247,32 @@ def main():
bots = choose_bots()
- game = Game(
- map_,
- victory_by_combat=len(bots) != 1,
- )
- for key, server_info, name in bots:
- suffix = ''.join(random.sample(lowercase, 3))
- fullname = '%s.%s.%s' % (
- server_info['name'],
- name,
- suffix,
- )
- game.add_bot(
- RemoteBot(*key),
- fullname,
+ loop = range(1)
+ if args.loop:
+ loop = itertools.count()
+
+ for _ in loop:
+ game = Game(
+ map_,
+ victory_by_combat=len(bots) != 1,
)
+ for key, server_info, name in bots:
+# suffix = ''.join(random.sample(lowercase, 3))
+# fullname = '%s.%s.%s' % (
+# server_info['name'],
+# name,
+# suffix,
+# )
+ game.add_bot(
+ RemoteBot(*key),
+ name,
+ )
+
+ viewer = CursesViewer(game)
+ viewer.run()
+ time.sleep(1)
- viewer = CursesViewer(game)
- viewer.run()
+ random.shuffle(bots)
if __name__ == '__main__':
main()
diff --git a/robots/cursesviewer.py b/robots/cursesviewer.py
index c4c7f74..96042a2 100644
--- a/robots/cursesviewer.py
+++ b/robots/cursesviewer.py
@@ -19,7 +19,7 @@ class CursesViewer:
self.terminal = Terminal()
- colours = cycle('red blue green yellow magenta cyan'.split())
+ colours = [1, 2, 3, 4, 5, 6, 9, 12, 13, 17, 20]
self.player_colours = dict(zip(
sorted(game.state.players),
colours,
@@ -28,10 +28,11 @@ class CursesViewer:
def draw_board(self, state):
stdout = []
stdout.append(self.terminal.clear)
+ stdout.append('Turn %d\n' % self.game.time)
players = []
for name, colour in sorted(self.player_colours.items()):
- fn = getattr(self.terminal, 'on_' + colour)
+ fn = self.terminal.on_color(colour)
players.append(fn(name))
stdout.append(' '.join(players) + '\n')
@@ -44,7 +45,7 @@ class CursesViewer:
value = ' '
if cell['city'] == City.GHOST:
- background = 'white'
+ background = 15
else:
background = self.player_colours.get(cell['allegiance'])
@@ -53,21 +54,21 @@ class CursesViewer:
foreground = self.player_colours[robot_owner]
if foreground == background:
- foreground = 'white'
+ foreground = 15
if cell['city'] == City.FACTORY:
value = '[%s]' % energy
else:
value = '<%s>' % energy
- func = getattr(self.terminal, foreground)
+ func = self.terminal.color(foreground)
value = func(value)
elif cell['city'] == City.FACTORY:
value = '[ ]'
- if background:
- func = getattr(self.terminal, 'on_' + background)
+ if background is not None:
+ func = self.terminal.on_color(background)
value = func(value)
output.append(value)
@@ -87,6 +88,8 @@ class CursesViewer:
winners = self.game.finished
if winners is True:
print('Game ended in a draw.')
+ elif winners is False:
+ print('Game ended in stalemate.')
else:
print('Game won by:', ', '.join(winners))
diff --git a/robots/game.py b/robots/game.py
index 9160245..0581020 100644
--- a/robots/game.py
+++ b/robots/game.py
@@ -2,8 +2,7 @@ from collections import defaultdict, deque
from contextlib import contextmanager
from copy import deepcopy
from random import random
-import time
-import traceback
+from string import ascii_lowercase as lowercase
from robots.constants import Action, City, Energy, ENERGY_BY_ACTION
from robots.state import GameState
@@ -22,6 +21,8 @@ def extract_spawn_points(map_):
points = []
for x, y, cell in iter_board(map_):
+ if cell in lowercase:
+ cell = lowercase.index(cell)
try:
n = int(cell)
except ValueError:
@@ -90,6 +91,7 @@ class Game:
return winners or won
def call_bots(self):
+ kill_players = []
for whoami, bot in self.bots.items():
try:
with protected(self.state, 'state') as state:
@@ -112,13 +114,17 @@ class Game:
except Exception:
# TODO: log this exception
- traceback.print_exc()
- time.sleep(1)
+ kill_players.append(whoami)
+# traceback.print_exc()
+# time.sleep(1)
else:
for bot, action in zip(my_robots, result):
yield whoami, bot, action
+ for player in kill_players:
+ self.state.robots_by_player[player][:] = []
+
def apply_actions(self, actions):
robots_by_player = defaultdict(list)
@@ -216,6 +222,8 @@ class Game:
while not self.finished:
yield self.state
self.next()
+ if self.time > 400:
+ break
yield self.state
def next(self):
diff --git a/two.txt b/two.txt
new file mode 100644
index 0000000..add738d
--- /dev/null
+++ b/two.txt
@@ -0,0 +1,15 @@
+.................
+..+..+..+..+..+..
+..........XXX....
+.....1...........
+..+..+..+..+..+..
+.....XXXX........
+........3X.......
+..+X.+..+X.+..+..
+...X.....X.......
+...X.......2.....
+..+X.+..+..+..+..
+...X.....XXX.....
+..............4..
+..+..+..+..+..+..
+.................
diff --git a/up_bot.py b/up_bot.py
new file mode 100644
index 0000000..4b2b403
--- /dev/null
+++ b/up_bot.py
@@ -0,0 +1,5 @@
+def up_bot(whoami, state):
+ # Get the number of robots you control.
+ n_robots = len(state.robots_by_player[whoami])
+ # Tell them all to go up.
+ return 'U' * n_robots