From: Christian Heller Date: Sun, 25 Oct 2020 00:56:43 +0000 (+0200) Subject: De-hardcode map size test. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/todos?a=commitdiff_plain;h=09a7a28a19b56662eccceb5785fd36265845c15c;p=plomrogue2-experiments De-hardcode map size test. --- diff --git a/new2/plomrogue/game.py b/new2/plomrogue/game.py index 47e08fb..a8fc4ed 100755 --- a/new2/plomrogue/game.py +++ b/new2/plomrogue/game.py @@ -4,7 +4,7 @@ from plomrogue.commands import cmd_ALL, cmd_LOGIN, cmd_QUERY from plomrogue.io import GameIO from plomrogue.misc import quote from plomrogue.things import Thing, ThingPlayer -from plomrogue.mapping import MapGeometrySquare +from plomrogue.mapping import YX, MapGeometrySquare @@ -37,7 +37,7 @@ class Game(GameBase): self.io = GameIO(self) self.tasks = {'WAIT': Task_WAIT, 'MOVE': Task_MOVE} - self.map_geometry = MapGeometrySquare() + self.map_geometry = MapGeometrySquare(YX(24, 40)) self.commands = {'QUERY': cmd_QUERY, 'ALL': cmd_ALL, 'LOGIN': cmd_LOGIN} diff --git a/new2/plomrogue/mapping.py b/new2/plomrogue/mapping.py index aa5af2c..585000a 100644 --- a/new2/plomrogue/mapping.py +++ b/new2/plomrogue/mapping.py @@ -17,6 +17,9 @@ class YX(collections.namedtuple('YX', ('y', 'x'))): class MapGeometry(): + def __init__(self, size): + self.size = size + def get_directions(self): directions = [] for name in dir(self): @@ -26,7 +29,11 @@ class MapGeometry(): def move(self, start_pos, direction): mover = getattr(self, 'move_' + direction) - return mover(start_pos) + target = mover(start_pos) + if target.y < 0 or target.x < 0 or \ + target.y >= self.size.y or target.x >= self.size.x: + return None + return target diff --git a/new2/plomrogue/tasks.py b/new2/plomrogue/tasks.py index a753526..0108416 100644 --- a/new2/plomrogue/tasks.py +++ b/new2/plomrogue/tasks.py @@ -33,7 +33,7 @@ class Task_MOVE(Task): def check(self): test_pos = self.get_move_target() - if test_pos.y < 0 or test_pos.x < 0 or test_pos.y >= 24 or test_pos.x >= 40: + if test_pos is None: raise GameError('would move out of map') def do(self):