Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Hacksaar
Haxotel
Commits
21a0f291
Commit
21a0f291
authored
Jul 31, 2014
by
Constantin
Browse files
changes
parent
674ea1c3
Changes
2
Hide whitespace changes
Inline
Side-by-side
game.py
View file @
21a0f291
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
)
...
...
labyrinth.py
View file @
21a0f291
# 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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment