#!/usr/bin/env python # Examples demonstrating the use of the Wumpus package import json import random import wumpus as wws class MyPlayer(wws.UserPlayer): """Player demonstrating the use of the start episode method to inspect the world.""" def start_episode(self): """Print the description of the world and the agent before starting (if known).""" if self.world is not None: # I know the world world_info = {k: [] for k in ('Hunter', 'Pits', 'Wumpus', 'Gold', 'Exits')} world_info['Size'] = (self.world.size.x, self.world.size.y) world_info['Blocks'] = [(c.x, c.y) for c in self.world.blocks] for obj in self.world.objects: if isinstance(obj, wws.Hunter): world_info['Hunter'].append((obj.location.x, obj.location.y)) elif isinstance(obj, wws.Pit): world_info['Pits'].append((obj.location.x, obj.location.y)) elif isinstance(obj, wws.Wumpus): world_info['Wumpus'].append((obj.location.x, obj.location.y)) elif isinstance(obj, wws.Exit): world_info['Exits'].append((obj.location.x, obj.location.y)) elif isinstance(obj, wws.Gold): world_info['Gold'].append((obj.location.x, obj.location.y)) print('World details:') for k in ('Size', 'Pits', 'Wumpus', 'Gold', 'Exits', 'Blocks'): print(' {}: {}'.format(k, world_info.get(k, None))) if self.agent is not None and isinstance(self.agent, wws.Hunter): print('Controlling hunter in position ({}, {}) with direction {}'.format(self.agent.location.x, self.agent.location.y, self.agent.orientation)) def play_classic(size: int = 0): """Play the classic version of the wumpus.""" # create the world world = wws.WumpusWorld.classic(size=size if size > 3 else random.randint(4, 8)) # get the hunter agent hunter = next(iter(o for o in world.objects if isinstance(o, wws.Hunter)), None) # Run a player without any knowledge about the world world.run_episode(hunter, wws.UserPlayer.player()) def play_classic_informed(size: int = 0): """Play the classic version of the wumpus with a player knowing the world and the agent.""" # create the world world = wws.WumpusWorld.classic(size=size if size > 3 else random.randint(4, 8)) # get the hunter agent hunter = next(iter(o for o in world.objects if isinstance(o, wws.Hunter)), None) # Run a player with knowledge about the world world.run_episode(hunter, MyPlayer.player(world=world, agent=hunter)) WUMPUS_WORLD = ''' { "id": "simple wumpus world", "size": [7, 7], "hunters": [[0, 0]], "pits": [[4, 0], [3, 1], [2, 2], [6, 2], [4, 4], [3, 5], [4, 6], [5, 6]], "wumpuses": [[1, 2]], "exits": [[0, 0]], "golds": [[6, 3]], "blocks": [] } ''' def play_fixed(world_json: str = WUMPUS_WORLD): """Play on a given world described in JSON format.""" # create the world world = wws.WumpusWorld.from_JSON(json.loads(world_json)) # get the hunter agent hunter = next(iter(o for o in world.objects if isinstance(o, wws.Hunter)), None) # Run a player with knowledge about the world world.run_episode(hunter, MyPlayer.player(world=world, agent=hunter)) EXAMPLES = (play_classic, play_classic_informed, play_fixed) def main(*args): # Randomly play one of the examples ex = random.choice(EXAMPLES) print('Example {}:'.format(ex.__name__)) print(' ' + ex.__doc__) ex() if __name__ == "__main__": main()