Commit 21a0f291 authored by Constantin's avatar Constantin
Browse files

changes

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