Commit 35dd6c4c authored by Constantin's avatar Constantin
Browse files

moar functiannzzz in labyRINT

parent 30476a55
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
from enum import Enum from enum import Enum
from functools import reduce from functools import reduce
directions = {"north": (-1, 0), "east": (0, 1), "west": (0, -1), "south": (1, 0)}
class Labyrinth: class Labyrinth:
class GroundType(Enum): class GroundType(Enum):
FLOOR = 1 FLOOR = 1
...@@ -23,6 +25,11 @@ class Labyrinth: ...@@ -23,6 +25,11 @@ class Labyrinth:
assert self.things.count(thing) == 0 assert self.things.count(thing) == 0
assert self.groundtype != Labyrinth.GroundType.WALL assert self.groundtype != Labyrinth.GroundType.WALL
self.things.append(thing) 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): def toString(self):
# always starts with there is/there are # always starts with there is/there are
if self.groundtype == Labyrinth.GroundType.WALL: if self.groundtype == Labyrinth.GroundType.WALL:
...@@ -59,12 +66,33 @@ class Labyrinth: ...@@ -59,12 +66,33 @@ class Labyrinth:
tiles.append(row) tiles.append(row)
return tiles 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): def getTiles(self):
return reduce(lambda x,y:x+y, self.tiles) return reduce(lambda x,y:x+y, self.tiles)
def getFreeTiles (self): def getFreeTiles (self):
return filter(lambda f: f.groundtype == Labyrinth.GroundType.FLOOR and len(f.things == 0) ,self.getTiles) 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): def moveThing (self, thing, newField):
if thing.onMove(newField) == False: if thing.onMove(newField) == False:
return False return False
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment