Commit 7e65c144 authored by Constantin's avatar Constantin
Browse files

Merge branch 'master' of

parents 62f391ea 590f6419
......@@ -7,6 +7,7 @@ import random
class Game:
def __init__(self, labyrinthFileName, adminPW, logFileName=None, port=1234):
self.players = []
self.admins = []
with open(labyrinthFileName,'r') as f:
self.labyrinth = labyrinth.Labyrinth(game=self,stream=f)
self.sock = socket.socket()
......@@ -16,7 +17,6 @@ class Game:
self.sel.register(self.sock, selectors.EVENT_READ, self.accept)
self.logFile = None if logFileName is None else open(logFileName, 'a')
self.adminPW = adminPW
self.admins = []
# low-level functions
def accept(self, sock, mask):
......@@ -57,6 +57,7 @@ class Game:
print(*args, file=self.logFile, flush=True)
def showAdmins(self):
if not self.admins: return
lab = self.labyrinth.writelab()
for admin in self.admins:
# class for the labyrinth
from enum import Enum
import itertools
from functools import reduce
from wall import Wall
directions = {"north": (-1, 0), "east": (0, 1), "west": (0, -1), "south": (1, 0)}
directions_inv = {v:k for k, v in directions.items()}
def describeThings(things):
# always starts with there is/there are
if len(things) == 0:
return "there is nothing"
elif len(things) == 1:
return "there is "+str(things[0])
return "there are "+(", ".join([x.toString() for x in things[:-1]]))+" and "+things[-1].toString()
class Labyrinth:
class GroundType(Enum):
WALL = 2
class Field:
def __init__(self,groundtype,column,row,labyrinth):
def __init__(self,column,row,labyrinth):
# please only read from these members
self.groundtype = groundtype
self.column = column
self.row = row
self.labyrinth = labyrinth
......@@ -23,25 +30,15 @@ class Labyrinth:
def _removeThing(self,thing):
def _addThing(self,thing):
assert self.things.count(thing) == 0
assert self.groundtype != Labyrinth.GroundType.WALL
assert thing not in self.things
def neighbor(self,dy,dx):
return 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 __str__(self):
# always starts with there is/there are
if self.groundtype == Labyrinth.GroundType.WALL:
return "there is a wall"
if len(self.things) == 0:
return "there is nothing"
elif len(self.things) == 1:
return "there is "+str(self.things[0])
return "there are "+(", ".join([x.toString() for x in self.things[:-1]]))+" and "+self.things[-1].toString()
for thing in self.things:
if thing.blocksMove():
return False
return True
# constructor for a completely read labyrinth
def __init__(self,game=None,width=None,height=None,stream=None):
......@@ -49,7 +46,7 @@ class Labyrinth:
if width is not None: # empty labyrinth of given size
assert height is not None
assert stream is None
self.tiles = [[Labyrinth.Field(Labyrinth.GroundType.FLOOR,col,row,self) for col in range(width)] for row in range(height)]
self.tiles = [[Labyrinth.Field(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:
......@@ -60,11 +57,17 @@ class Labyrinth:
tiles = []
for rownum,line in enumerate(stream):
row = []
for colnum,char in enumerate(line):
for colnum,char in enumerate(line[:-1]):
field = Labyrinth.Field(colnum,rownum,self)
if char in [' ','0','_']:
elif char in ['W','w','#','1']:
wall = Wall()
wall.field = field
raise Exception("Unexpected character '{0}'".format(char))
return tiles
......@@ -72,15 +75,12 @@ class Labyrinth:
result = ""
for row in self.tiles:
for field in row:
if field.groundtype == Labyrinth.GroundType.WALL:
result += "#"
if not field.things:
result += "."
elif len(field.things) == 1:
result += repr(field.things[0])
if not field.things:
result += "."
elif len(field.things) == 1:
result += field.things[0].asChar()
result += "+"
result += "+"
result += "\n"
return result
......@@ -103,12 +103,19 @@ class Labyrinth:
return reduce(lambda x,y:x+y, self.tiles)
def getFreeTiles (self):
return list(filter(lambda f: f.groundtype == Labyrinth.GroundType.FLOOR and len(f.things) == 0 ,self.getTiles()))
return list(filter(Labyrinth.Field.isWalkable, self.getTiles()))
def getDescription (self, field):
def getDescription (self, player):
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"
local = list(filter(lambda thing: thing != player, player.field.things))
if local:
descr += "Next to you, "+describeThings(local)+"\n"
for direction, (dy,dx) in directions.items():
field = player.field.neighbor(dx=dx, dy=dy)
if field is None:
descr += "In the "+direction+", you see a weird kind of void. What could that be? Only time will tell...\n"
elif field.things:
descr += "In the "+direction+", "+describeThings(field.things)+"\n"
return descr
def moveThing (self, thing, newField, beforeFinish=None):
......@@ -130,8 +137,8 @@ class Labyrinth:
def createThing (self, thing, field):
thing.field = field
if thing.onMove(field):
def removeThing(self, thing):
......@@ -138,13 +138,19 @@ class Player(Thing):
elif verb in sayVerbs:
msg = " ".join(words[1:])
someoneClose = False
for dx, dy in directionOffsets.values():
for thing in self.field.neighbor(dx=dx, dy=dy).things:
if isinstance(thing, Player) and thing != self:
assert thing != self
if isinstance(thing, Player):
someoneClose = True
thing.send("You hear someone saying: "+msg)
for admin in
admin.send("{0} says: {1}".format(self.uid, msg))
self.send("You say: "+msg)
if someoneClose:
self.send("You say: "+msg)
self.send("You want to say something, but then you realize nobody's caring anyway. Get over it!")
elif verb in leaveVerbs:
self.send("Good Bye!")
......@@ -162,8 +168,8 @@ class Player(Thing):
self.send("I don't know what you are talking about")
def afterMove(self, oldField):
desc =
desc =
self.send(desc, end="")
def asChar(self):
def __repr__(self):
return "P"
......@@ -8,5 +8,8 @@ class Thing():
def afterMove(self, oldField):
def asChar(self):
def blocksMove(self):
return False
def __repr__(self):
return "?"
from thing import Thing
class Wall(Thing):
def __init__(self):
def onMove(self, newField):
return False
def blocksMove(self):
return True
def __str__(self):
return "a wall"
def __repr__(self):
return "#"
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