Commit 21a0f291 authored by Constantin Berhard's avatar Constantin Berhard

changes

parent 674ea1c3
from player import Player
import selectors
import socket
import labyrinth
class Game:
def __init__(self):
def __init__(self, labyrinthFileName, port=1234):
self.players = []
with open(labyrinthFileName,'r') as f:
self.labyrinth = labyrinth.Labyrinth(stream=f)
self.sock = socket.socket()
self.sock.bind(('0.0.0.0', 1234))
self.sock.bind(('0.0.0.0', port))
self.sock.listen(100)
self.sel = selectors.DefaultSelector()
self.sel.register(self.sock, selectors.EVENT_READ, self.accept)
......@@ -14,6 +17,7 @@ class Game:
def accept(self, sock, mask):
assert sock == self.sock
conn, addr = sock.accept() # Should be ready
player = Player(self, conn)
self.players.append(player)
self.sel.register(conn, selectors.EVENT_READ, player.read)
......
# class for the labyrinth
from enum import Enum
from functools import reduce
class Labyrinth:
class GroundType(Enum):
......@@ -58,8 +59,22 @@ class Labyrinth:
tiles.append(row)
return tiles
def onThingMoved (self, thing, oldField, newField):
assert oldField.labyrinth == self
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 moveThing (self, thing, newField):
if thing.onMove(newField) == False:
return False
assert thing.field.labyrinth == self
assert newField.labyrinth == self
oldField._removeThing(thing)
newField._addThing(thing)
\ No newline at end of file
thing.field._removeThing(thing)
thing.field = newField
newField._addThing(thing)
return True
def createThing (self, thing, field):
thing.field = field
field._addThing(thing)
\ No newline at end of file
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