summaryrefslogtreecommitdiff
path: root/peter.py
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2009-09-28 12:00:43 +1000
committerPeter Ward <peteraward@gmail.com>2009-09-28 12:00:43 +1000
commit84c620ce93dbb9da9fc2328c13b8a48316641d4a (patch)
treec0f8e69bb7fadad122381e6862d8b046f11e549b /peter.py
parent9cc9e121b18556f6c83cad335e32621c27f656bc (diff)
Added peter bot.
Diffstat (limited to 'peter.py')
-rw-r--r--peter.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/peter.py b/peter.py
new file mode 100644
index 0000000..b182575
--- /dev/null
+++ b/peter.py
@@ -0,0 +1,71 @@
+#!/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!
+