From 35dd6c4c5d42d3f68a8458734d94b769056483f1 Mon Sep 17 00:00:00 2001 From: Constantin Berhard Date: Thu, 31 Jul 2014 22:40:04 +0200 Subject: [PATCH] moar functiannzzz in labyRINT --- labyrinth.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/labyrinth.py b/labyrinth.py index 4fdb629..c15531c 100644 --- a/labyrinth.py +++ b/labyrinth.py @@ -3,6 +3,8 @@ from enum import Enum from functools import reduce +directions = {"north": (-1, 0), "east": (0, 1), "west": (0, -1), "south": (1, 0)} + class Labyrinth: class GroundType(Enum): FLOOR = 1 @@ -23,6 +25,11 @@ class Labyrinth: assert self.things.count(thing) == 0 assert self.groundtype != Labyrinth.GroundType.WALL self.things.append(thing) + def neighbor(self,dy,dx): + self.labyrinth.getTileAt(self.row + dy, self.column + dx) + def isWalkable(self): + # this may be more complicated in the future + return self.groundtype == Labyrinth.GroundType.FLOOR def toString(self): # always starts with there is/there are if self.groundtype == Labyrinth.GroundType.WALL: @@ -59,12 +66,33 @@ class Labyrinth: tiles.append(row) return tiles + def getWidth(self): + return len(self.tiles[0]) + + def getHeight(self): + return len(self.tiles) + + def outOfBounds(self,row,column): + return row < 0 or column < 0 or row >= self.getHeight() or column >= self.getWidth() + + def getTileAt(self,row,column): + if self.outOfBounds(row,column): + return None + else: + return self.tiles[row][column] + def getTiles(self): return reduce(lambda x,y:x+y, self.tiles) def getFreeTiles (self): return filter(lambda f: f.groundtype == Labyrinth.GroundType.FLOOR and len(f.things == 0) ,self.getTiles) + def getDescription (self, field): + descr = "" + for name,field in zip(["North","South","West","East"], [self.getTileAt(field.row+row,field.column+column) for row,column in [(-1,0),(1,0),(0,-1),(0,1)]]): + descr += "In the "+name+" "+("there is a wall" if field is None else str(field))+".\n" + return descr + def moveThing (self, thing, newField): if thing.onMove(newField) == False: return False -- GitLab