From 220b7e3e76f075d8e53df4ba8bb29022d9ee609b Mon Sep 17 00:00:00 2001 From: Constantin Berhard Date: Thu, 31 Jul 2014 21:20:18 +0200 Subject: [PATCH] labyrinth class --- labyrinth.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ readlab.py | 13 ----------- 2 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 labyrinth.py delete mode 100644 readlab.py diff --git a/labyrinth.py b/labyrinth.py new file mode 100644 index 0000000..f658e66 --- /dev/null +++ b/labyrinth.py @@ -0,0 +1,65 @@ +# class for the labyrinth + +from enum import Enum + +class Labyrinth: + class GroundType(Enum): + FLOOR = 1 + WALL = 2 + + class Field: + def __init__(self,groundtype,column,row,labyrinth): + # please only read from these members + self.groundtype = groundtype + self.column = column + self.row = row + self.labyrinth = labyrinth + # the following usually change + self.things = [] # list of things/players on this field + def _removeThing(self,thing): + self.things.remove(thing) + def _addThing(self,thing): + assert self.things.count(thing) == 0 + assert self.groundtype != Labyrinth.GroundType.WALL + self.things.append(thing) + def toString(self): + # always starts with there is/there are + if self.groundtype == Labyrinth.GroundType.WALL: + return "there is a wall" + else: + if len(self.things) == 0: + return "there is nothing" + elif len(self.things) == 1: + return "there is "+self.things[0].toString() + else: + return "there are "+(", ".join([x.toString() for x in self.things[:-1]]))+" and "+self.things[-1].toString() + + # constructor for a completely read labyrinth + def __init__(self,width=None,height=None,stream=None): + if width is not None: # empty labyrinth of given size + assert height is not None + assert stream is None + self.tiles = [[Labyrinth.Field(GroundType.FLOOR,col,row,self) for col in range(width)] for row in range(height)] + elif height is not None: + assert False # width None, height not + elif stream is not None: + self.tiles = self._readlab(stream) + + # function for reading a labyrinth from a file + def _readlab (self,stream): + tiles = [] + for rownum,line in enumerate(stream): + row = [] + for colnum,char in enumerate(line): + if char in [' ','0']: + row.append(Labyrinth.Field(GroundType.FLOOR,colnum,rownum,self)) + elif char in ['W','w','#','1']: + row.append(Labyrinth.Field(GroundType.WALL,colnum,rownum,self)) + tiles.append(row) + return tiles + + def onThingMoved (self, thing, oldField, newField): + assert oldField.labyrinth == self + assert newField.labyrinth == self + oldField._removeThing(thing) + newField._addThing(thing) \ No newline at end of file diff --git a/readlab.py b/readlab.py deleted file mode 100644 index 74f3c94..0000000 --- a/readlab.py +++ /dev/null @@ -1,13 +0,0 @@ -# function for reading a labyrinth - -def readlab (stream): - field = [] - for line in stream: - row = [] - for char in line: - if char in [' ','0']: - row.append(' ') - elif char in ['W','w','#','1']: - row.append('W') - field.append(row) - return field \ No newline at end of file -- GitLab