summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--oldbot.py29
-rwxr-xr-xoldbots/peter.py83
-rw-r--r--peter.py71
-rwxr-xr-xsnake.py4
4 files changed, 114 insertions, 73 deletions
diff --git a/oldbot.py b/oldbot.py
new file mode 100644
index 0000000..436375c
--- /dev/null
+++ b/oldbot.py
@@ -0,0 +1,29 @@
+import subprocess
+
+class BotWrapper(object):
+ def __init__(self, process):
+ self.process = process
+
+ def __call__(self, board, (x, y)):
+ height = len(board)
+ width = len(board[0])
+
+ letter = board[y][x].lower()
+
+ proc = subprocess.Popen(
+ [self.process],
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ )
+
+ board = '\n'.join([''.join(row) for row in board])
+
+ print>>proc.stdin, width, height, letter
+ print>>proc.stdin, board
+ proc.stdin.close()
+ proc.wait()
+
+ if proc.returncode == 0:
+ output = proc.stdout.read()
+ return output.strip()
+
diff --git a/oldbots/peter.py b/oldbots/peter.py
new file mode 100755
index 0000000..72bbc95
--- /dev/null
+++ b/oldbots/peter.py
@@ -0,0 +1,83 @@
+#!/usr/bin/env python
+"""New and improved bot, OPTIMISED!!"""
+
+import random
+import sys
+
+EMPTY_TILE = '.'
+APPLE_TILE = '*'
+
+WIDTH, HEIGHT, SNAKE_BODY = raw_input().split()
+WIDTH = int(HEIGHT)
+HEIGHT = int(HEIGHT)
+
+SNAKE_BODY = SNAKE_BODY.lower()
+SNAKE_HEAD = SNAKE_BODY.upper()
+
+HEADX = None
+HEADY = None
+
+def get_cell(board, x, y):
+ if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT:
+ raise KeyError, 'out of range.'
+ return board[y][x]
+
+BOARD = []
+for y in xrange(HEIGHT):
+ row = raw_input()
+ for x, char in enumerate(row):
+ if char == SNAKE_HEAD:
+ HEADX = x
+ HEADY = y
+ BOARD.append(row)
+
+md_two = {
+ (-1, 0, 'l'): ((-2, 0), (-1, 1), (-1, -1)),
+ (0, -1, 'u'): ((-1, -1), (1, -1), (0, -2)),
+ (1, 0, 'r'): ((2, 0), (1, 1), (1, -1)),
+ (0, 1, 'd'): (((0, 2), (-1, 1), (1, 1))),
+}
+
+max_score = 0
+max_moves = []
+
+for (dx, dy, move), adj in md_two.items():
+ score = 0
+
+ try:
+ square = get_cell(BOARD, HEADX + dx, HEADY + dy)
+ except KeyError:
+ continue
+
+ if square == APPLE_TILE:
+ score += 2
+ elif square != EMPTY_TILE:
+ continue # Definitely cannot move here.
+
+ for ddx, ddy in adj:
+ try:
+ square = get_cell(BOARD, HEADX + ddx, HEADY + ddy)
+ except KeyError:
+ score -= 1
+ continue
+
+ if square == APPLE_TILE:
+ score += 2
+ elif square == EMPTY_TILE:
+ score += 1
+ elif square == SNAKE_BODY:
+ score -= 1
+ elif square.isupper():
+ score += 3
+
+ if score == max_score:
+ max_moves.append(move)
+ elif score > max_score:
+ max_score = score
+ max_moves = [move]
+
+if max_moves:
+ print random.choice(max_moves)
+else:
+ print 'U' # Suicide!
+
diff --git a/peter.py b/peter.py
deleted file mode 100644
index b182575..0000000
--- a/peter.py
+++ /dev/null
@@ -1,71 +0,0 @@
-#!/usr/bin/env python
-"""New and improved bot, OPTIMISED!!"""
-
-import random
-import sys
-
-from common import *
-
-def peter_bot(board, (HEADX, HEADY)):
- HEIGHT = len(board)
- WIDTH = len(board[0])
- SNAKE_BODY = board[HEADY][HEADX]
-
- SNAKE_BODY = SNAKE_BODY.lower()
- SNAKE_HEAD = SNAKE_BODY.upper()
-
- def get_cell(board, x, y):
- if x < 0 or x >= WIDTH or y < 0 or y >= HEIGHT:
- raise KeyError, 'out of range.'
- return board[y][x]
-
- md_two = {
- (-1, 0, 'l'): ((-2, 0), (-1, 1), (-1, -1)),
- (0, -1, 'u'): ((-1, -1), (1, -1), (0, -2)),
- (1, 0, 'r'): ((2, 0), (1, 1), (1, -1)),
- (0, 1, 'd'): (((0, 2), (-1, 1), (1, 1))),
- }
-
- max_score = 0
- max_moves = []
-
- for (dx, dy, move), adj in md_two.items():
- score = 0
-
- try:
- square = get_cell(board, HEADX + dx, HEADY + dy)
- except KeyError:
- continue
-
- if square == Squares.APPLE:
- score += 2
- elif square != Squares.EMPTY:
- continue # Definitely cannot move here.
-
- for ddx, ddy in adj:
- try:
- square = get_cell(board, HEADX + ddx, HEADY + ddy)
- except KeyError:
- score -= 1
- continue
-
- if square == Squares.APPLE:
- score += 2
- elif square == Squares.EMPTY:
- score += 1
- elif square == SNAKE_BODY:
- score -= 1
- elif square.isupper():
- score += 3
-
- if score == max_score:
- max_moves.append(move)
- elif score > max_score:
- max_score = score
- max_moves = [move]
-
- if max_moves:
- return random.choice(max_moves)
- else:
- return 'U' # Suicide!
-
diff --git a/snake.py b/snake.py
index f0bf46b..46f7db2 100755
--- a/snake.py
+++ b/snake.py
@@ -235,13 +235,13 @@ class SnakeEngine(object):
if __name__ == '__main__':
from bots import *
- from peter import *
+ from oldbot import BotWrapper
game = SnakeEngine(25, 25, 50)
game.add_bot(right_bot)
game.add_bot(random_bot)
game.add_bot(random_bounds_bot)
game.add_bot(random_square_bot)
- game.add_bot(peter_bot)
+ game.add_bot(BotWrapper('oldbots/peter.py'))
game.run()