2012-08-07 13:19:31 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2012-10-12 13:50:02 +02:00
|
|
|
# This file is part of A Robot's Conundrum.
|
2012-08-07 13:19:31 +02:00
|
|
|
#
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License as published by the Free
|
|
|
|
# Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
# later version.
|
2012-08-07 13:19:31 +02:00
|
|
|
#
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
2012-08-07 13:19:31 +02:00
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
2012-10-12 13:50:02 +02:00
|
|
|
# A Robot's Conundrum. If not, see <http://www.gnu.org/licenses/>.
|
2012-08-07 13:19:31 +02:00
|
|
|
#
|
|
|
|
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
|
|
|
|
#
|
|
|
|
# direction.py
|
|
|
|
# --------------------
|
|
|
|
# date created : Tue Aug 7 2012
|
|
|
|
# copyright : (C) 2012 Niels G. W. Serup
|
2012-10-12 13:50:02 +02:00
|
|
|
# maintained by : Niels G. W. Serup <ngws@metanohi.name>
|
2012-08-07 13:19:31 +02:00
|
|
|
|
|
|
|
"""Directions."""
|
|
|
|
|
|
|
|
class Direction(object):
|
|
|
|
@staticmethod
|
|
|
|
def next_pos(pos):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2012-08-13 00:05:44 +02:00
|
|
|
@staticmethod
|
|
|
|
def to_str():
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2012-08-12 17:01:58 +02:00
|
|
|
@staticmethod
|
|
|
|
def from_sakse(p):
|
|
|
|
return {(0, -1): Up,
|
|
|
|
(0, 1): Down,
|
|
|
|
(-1, 0): Left,
|
|
|
|
(1, 0): Right}[p]
|
|
|
|
|
2012-08-07 13:19:31 +02:00
|
|
|
class Up(Direction):
|
|
|
|
@staticmethod
|
|
|
|
def next_pos(pos):
|
|
|
|
x, y = pos
|
|
|
|
return x, y - 1
|
|
|
|
|
2012-08-13 00:05:44 +02:00
|
|
|
@staticmethod
|
|
|
|
def to_str():
|
|
|
|
return 'up'
|
|
|
|
|
2012-08-07 13:19:31 +02:00
|
|
|
class Right(Direction):
|
|
|
|
@staticmethod
|
|
|
|
def next_pos(pos):
|
|
|
|
x, y = pos
|
|
|
|
return x + 1, y
|
|
|
|
|
2012-08-13 00:05:44 +02:00
|
|
|
@staticmethod
|
|
|
|
def to_str():
|
|
|
|
return 'right'
|
|
|
|
|
2012-08-07 13:19:31 +02:00
|
|
|
class Down(Direction):
|
|
|
|
@staticmethod
|
|
|
|
def next_pos(pos):
|
|
|
|
x, y = pos
|
|
|
|
return x, y + 1
|
|
|
|
|
2012-08-13 00:05:44 +02:00
|
|
|
@staticmethod
|
|
|
|
def to_str():
|
|
|
|
return 'down'
|
|
|
|
|
2012-08-07 13:19:31 +02:00
|
|
|
class Left(Direction):
|
|
|
|
@staticmethod
|
|
|
|
def next_pos(pos):
|
|
|
|
x, y = pos
|
|
|
|
return x - 1, y
|
2012-08-07 15:26:16 +02:00
|
|
|
|
2012-08-13 00:05:44 +02:00
|
|
|
@staticmethod
|
|
|
|
def to_str():
|
|
|
|
return 'left'
|
|
|
|
|
2012-08-14 17:02:06 +02:00
|
|
|
all_directions = [Up, Right, Down, Left]
|
2012-08-08 18:37:09 +02:00
|
|
|
_sp = lambda n: lambda d: all_directions[(all_directions.index(d) + n) % 4]
|
|
|
|
succ, pred = _sp(1), _sp(-1)
|
|
|
|
isDirection = all_directions.__contains__
|