Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wumpus-tessaris
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lanti Davide
wumpus-tessaris
Commits
ce12d020
Commit
ce12d020
authored
5 years ago
by
Tessaris Sergio
Browse files
Options
Downloads
Patches
Plain Diff
added access to location/objects indexes
parent
723125e1
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
wumpus/gridworld.py
+29
-19
29 additions, 19 deletions
wumpus/gridworld.py
with
29 additions
and
19 deletions
wumpus/gridworld.py
+
29
−
19
View file @
ce12d020
...
...
@@ -117,7 +117,7 @@ class GridWorld(object):
self
.
_size
=
size
self
.
_blocks
=
set
(
blocks
)
self
.
_objects
:
Dict
[
WorldObject
,
Coordinate
]
=
{}
self
.
_location
:
Dict
[
Coordinate
,
Iterable
[
Coordinate
]]
=
{}
self
.
_location
:
Dict
[
Coordinate
,
Iterable
[
WorldObject
]]
=
{}
@classmethod
def
from_string
(
cls
,
world_desc
:
str
)
->
'
GridWorld
'
:
...
...
@@ -159,6 +159,16 @@ class GridWorld(object):
"""
Return the set of coordinates where blocks are placed.
"""
return
self
.
_blocks
@property
def
object_locations
(
self
)
->
Dict
[
WorldObject
,
Coordinate
]:
"""
Return a dictionary associating objects to their coordinate.
"""
return
self
.
_objects
@property
def
location_objects
(
self
)
->
Dict
[
Coordinate
,
Iterable
[
WorldObject
]]:
"""
Return a dictionary associating locations to the objects at the given coordinate.
"""
return
self
.
_location
def
isBlock
(
self
,
pos
:
Coordinate
)
->
bool
:
"""
Return true if in the coordinate there
'
s a block.
"""
return
pos
in
self
.
blocks
...
...
@@ -169,24 +179,24 @@ class GridWorld(object):
def
objects_at
(
self
,
pos
:
Coordinate
)
->
Iterable
[
WorldObject
]:
"""
Return an iterable over the objects at the given coordinate.
"""
return
self
.
_
location
.
get
(
pos
,
[])
return
self
.
location
_objects
.
get
(
pos
,
[])
def
location_of
(
self
,
obj
:
WorldObject
)
->
Coordinate
:
"""
Return the coordinate of the object within the world, or none if it
'
s not in it.
"""
return
self
.
_
objects
.
get
(
obj
,
None
)
return
self
.
object
_location
s
.
get
(
obj
,
None
)
def
empty_cells
(
self
,
count_objects
=
False
)
->
Iterable
[
Coordinate
]:
"""
Return an iterable object over the cells without blocks. If count_objects is not False then also other objects are taken into account.
"""
all_cells
=
set
([
coord
(
x
,
y
)
for
x
in
range
(
0
,
self
.
size
.
x
)
for
y
in
range
(
0
,
self
.
size
.
y
)])
all_cells
.
difference_update
(
self
.
blocks
)
if
count_objects
:
all_cells
.
difference_update
(
self
.
_
location
.
keys
())
all_cells
.
difference_update
(
self
.
location
_objects
.
keys
())
return
all_cells
@property
def
objects
(
self
)
->
Iterable
[
WorldObject
]:
"""
Return an iterable over the objects within the world.
"""
return
self
.
_
objects
.
keys
()
return
self
.
object
_location
s
.
keys
()
def
removeBlock
(
self
,
pos
:
Coordinate
):
self
.
_blocks
.
discard
(
pos
)
...
...
@@ -199,17 +209,17 @@ class GridWorld(object):
raise
OutOfBounds
(
'
Placing {} outside the world at {}
'
.
format
(
obj
,
pos
))
if
self
.
isBlock
(
pos
):
raise
Collision
(
'
Placing {} inside a block {}
'
.
format
(
obj
,
pos
))
if
pos
in
self
.
_
location
:
self
.
_
location
[
pos
].
append
(
obj
)
if
pos
in
self
.
location
_objects
:
self
.
location
_objects
[
pos
].
append
(
obj
)
else
:
self
.
_
location
[
pos
]
=
[
obj
]
self
.
_
objects
[
obj
]
=
pos
self
.
location
_objects
[
pos
]
=
[
obj
]
self
.
object
_location
s
[
obj
]
=
pos
obj
.
_setWorld
(
self
)
def
removeObject
(
self
,
obj
:
WorldObject
):
try
:
self
.
_
location
[
self
.
location_of
(
obj
)].
remove
(
obj
)
del
self
.
_
objects
[
obj
]
self
.
location
_objects
[
self
.
location_of
(
obj
)].
remove
(
obj
)
del
self
.
object
_location
s
[
obj
]
obj
.
_setWorld
(
None
)
except
KeyError
:
pass
...
...
@@ -221,14 +231,14 @@ class GridWorld(object):
raise
OutOfBounds
(
'
Moving {} outside the world at {}
'
.
format
(
obj
,
pos
))
if
self
.
isBlock
(
pos
):
raise
Collision
(
'
Moving {} inside a block {}
'
.
format
(
obj
,
pos
))
old_pos
=
self
.
_
objects
.
get
(
obj
,
None
)
if
old_pos
in
self
.
_
location
:
self
.
_
location
[
old_pos
].
remove
(
obj
)
if
pos
in
self
.
_
location
:
self
.
_
location
[
pos
].
append
(
obj
)
old_pos
=
self
.
object
_location
s
.
get
(
obj
,
None
)
if
old_pos
in
self
.
location
_objects
:
self
.
location
_objects
[
old_pos
].
remove
(
obj
)
if
pos
in
self
.
location
_objects
:
self
.
location
_objects
[
pos
].
append
(
obj
)
else
:
self
.
_
location
[
pos
]
=
[
obj
]
self
.
_
objects
[
obj
]
=
pos
self
.
location
_objects
[
pos
]
=
[
obj
]
self
.
object
_location
s
[
obj
]
=
pos
obj
.
_setWorld
(
self
)
def
__str__
(
self
):
...
...
@@ -240,7 +250,7 @@ class GridWorld(object):
for
pos
in
self
.
blocks
:
maze_strs
[
pos
.
y
][
pos
.
x
]
=
BLOCK
for
obj
,
pos
in
self
.
_
objects
.
items
():
for
obj
,
pos
in
self
.
object
_location
s
.
items
():
maze_strs
[
pos
.
y
][
pos
.
x
]
=
obj
.
charSymbol
().
ljust
(
CELL_WIDTH
)
top_frame
=
'
┌
'
+
'
─
'
*
CELL_WIDTH
*
self
.
size
.
x
+
'
┐
'
+
'
\n
'
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment