Skip to content
Snippets Groups Projects
Commit 4ade9171 authored by Tessaris Sergio's avatar Tessaris Sergio
Browse files

Fixed pit placement and removed messy method

parent a3d6d8c2
No related branches found
No related tags found
No related merge requests found
......@@ -289,51 +289,6 @@ class WumpusWorld(GridWorld):
return world
@classmethod
def randomWorld(cls, size: int = 4, pitProb: float = .2, seed=None, blockProb: float = 0, world_desc: str = None) -> 'WumpusWorld':
def randomPlace(world: GridWorld) -> Coordinate:
pos = coord(random.randint(0, world.size.x - 1), random.randint(0, world.size.y - 1))
while world.isBlock(pos):
pos = coord(random.randint(0, world.size.x - 1), random.randint(0, world.size.y - 1))
return pos
# initialise random generator
random.seed(seed)
world = cls(coord(size, size), []) if world_desc is None else cls.from_string(world_desc)
agentPos = coord(0, 0)
assert not world.isBlock(agentPos)
if blockProb > 0 and world_desc is None:
# randomly place blocks
for pos in [coord(x, y) for x in range(world.size.x) for y in range(world.size.y)]:
if pos != agentPos and random.random() < blockProb:
world.addBlock(pos)
world.addObject(Hunter(), agentPos)
world.addObject(Exit(), agentPos)
goldPos = randomPlace(world)
world.addObject(Gold(), goldPos)
wumpusPos = randomPlace(world)
while wumpusPos == agentPos:
wumpusPos = randomPlace(world)
world.addObject(Wumpus(), wumpusPos)
# place the pits
for row in range(world.size.y):
for col in range(world.size.x):
pos = coord(col, row)
# place a pit with probability pitProb
# but not in the same place as the agent
if pos != agentPos and not world.isBlock(pos) and random.random() < pitProb:
world.addObject(Pit(), pos)
return world
def random_populate(self, pitProb: float = .2, seed=None, wumpus: int = 1, gold: int = 1):
"""Randomly popolate a wumpus world with pits, gold, and wumpus. Pits and wumpus are not placed in the same place where the agent is."""
......@@ -343,7 +298,7 @@ class WumpusWorld(GridWorld):
pos = coord(random.randint(0, self.size.x - 1), random.randint(0, self.size.y - 1))
return pos
agentPos = (o.location for o in self.objects if isinstance(o, Hunter))
agentPos = tuple(o.location for o in self.objects if isinstance(o, Hunter))
random.seed(seed)
# place the wumpus
......@@ -360,7 +315,7 @@ class WumpusWorld(GridWorld):
pos = coord(col, row)
# place a pit with probability pitProb
# but not in the same place as the agent
if pos not in agentPos and not self.isBlock(pos) and random.random() < pitProb:
if (random.random() < pitProb) and (pos not in agentPos) and (not self.isBlock(pos)):
self.addObject(Pit(), pos)
def _objAt(self, cls, pos: Coordinate) -> Iterable[WumpusWorldObject]:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment