summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2010-09-10 11:49:17 +1000
committerPeter Ward <peteraward@gmail.com>2010-09-10 11:49:17 +1000
commit368a79abc1982615dc521e2128abd15ba1d28966 (patch)
treec0845071f39aa111f87797dd46a324e71b8714e2
parent56c61e0850478e3aa31b4e559acabcd0f3c4f0d2 (diff)
Added wrapping support.
-rw-r--r--snake.py13
1 files changed, 9 insertions, 4 deletions
diff --git a/snake.py b/snake.py
index 0429040..add6e62 100644
--- a/snake.py
+++ b/snake.py
@@ -15,9 +15,10 @@ import traceback
from common import *
class SnakeEngine(object):
- def __init__(self, rows, columns, n_apples, results=False):
+ def __init__(self, rows, columns, n_apples, wrap=False, results=False):
super(SnakeEngine, self).__init__()
+ self.wrap = wrap
self.bots = {}
self.results = None
if results:
@@ -123,9 +124,13 @@ class SnakeEngine(object):
nx = x + dx
ny = y + dy
- if ny < 0 or ny >= self.rows or nx < 0 or nx >= self.columns:
- self.remove_bot(letter)
- continue
+ if self.wrap:
+ ny %= self.rows
+ nx %= self.columns
+ else:
+ if ny < 0 or ny >= self.rows or nx < 0 or nx >= self.columns:
+ self.remove_bot(letter)
+ continue
oldcell = self.board[ny][nx]
if oldcell in (Squares.EMPTY, Squares.APPLE):