def test_record_quest_from_commands(play_the_game=False): M = GameMaker() # The goal commands = ["go east", "insert ball into chest"] # Create a 'bedroom' room. R1 = M.new_room("bedroom") R2 = M.new_room("kitchen") M.set_player(R1) path = M.connect(R1.east, R2.west) path.door = M.new(type='d', name='wooden door') path.door.add_property("open") ball = M.new(type='o', name='ball') M.inventory.add(ball) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("open") R2.add(chest) M.set_quest_from_commands(commands) game = M.build() with make_temp_directory( prefix="test_record_quest_from_commands") as tmpdir: game_file = _compile_game(game, folder=tmpdir) if play_the_game: textworld.play(game_file) else: agent = textworld.agents.WalkthroughAgent(commands) textworld.play(game_file, agent=agent, silent=True)
def build_test_game(): M = GameMaker() # Create a 'bedroom' room. R1 = M.new_room("bedroom") R2 = M.new_room("kitchen") M.set_player(R1) path = M.connect(R1.east, R2.west) path.door = M.new(type='d', name='wooden door') path.door.add_property("open") carrot = M.new(type='f', name='carrot') M.inventory.add(carrot) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("open") R2.add(chest) commands = ["go east", "insert carrot into chest"] quest1 = M.new_quest_using_commands(commands) quest1.reward = 2 commands = ["go east", "insert carrot into chest", "close chest"] event = M.new_event_using_commands(commands) quest2 = Quest(win_events=[event]) M.quests = [quest1, quest2] game = M.build() return game
def build_test_game(): M = GameMaker() # The goal commands = ["go east", "insert carrot into chest"] # Create a 'bedroom' room. R1 = M.new_room("bedroom") R2 = M.new_room("kitchen") M.set_player(R1) path = M.connect(R1.east, R2.west) path.door = M.new(type='d', name='wooden door') path.door.add_property("open") carrot = M.new(type='f', name='carrot') M.inventory.add(carrot) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("open") R2.add(chest) quest1 = M.new_quest_using_commands(commands) quest1.reward = 2 quest2 = M.new_quest_using_commands(commands + ["close chest"]) quest2.set_winning_conditions( [M.new_fact("in", carrot, chest), M.new_fact("closed", chest)]) M._quests = [quest1, quest2] game = M.build() return game
def build_test_game(): M = GameMaker() # The goal commands = ["go east", "insert carrot into chest"] # Create a 'bedroom' room. R1 = M.new_room("bedroom") R2 = M.new_room("kitchen") M.set_player(R1) path = M.connect(R1.east, R2.west) path.door = M.new(type='d', name='wooden door') path.door.add_property("open") carrot = M.new(type='f', name='carrot') M.inventory.add(carrot) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("open") R2.add(chest) M.set_quest_from_commands(commands) game = M.build() return game
def test_manually_defined_objective(): M = GameMaker() # Create a 'bedroom' room. R1 = M.new_room("bedroom") M.set_player(R1) game = M.build() game.objective = "There's nothing much to do in here." with make_temp_directory( prefix="test_manually_defined_objective") as tmpdir: game_file = M.compile(tmpdir) env = textworld.start(game_file, infos=textworld.EnvInfos(objective=True)) state = env.reset() assert state["objective"] == "There's nothing much to do in here."
def test_making_a_small_game(play_the_game=False): M = GameMaker() # Create a 'bedroom' room. R1 = M.new_room("bedroom") M.set_player(R1) # Add a second room to the east of R1. R2 = M.new_room() # Generated name path = M.connect(R1.east, R2.west) # Undirected path # Add a closed door between R1 and R2. door = M.new_door(path, name='glass door') door.add_property("locked") # Put a matching key for the door on R1's floor. key = M.new(type='k', name='rusty key') M.add_fact("match", key, door) R1.add(key) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("closed") R2.add(chest) # Add a 3 random portable objects in the chest. objs = [M.new(type='o') for _ in range(3)] chest.add(*objs) # Add 3 food objects in the player's inventory. foods = [M.new(type='f') for _ in range(3)] M.inventory.add(*foods) ## Add 10 random objects scattered in the world. #M.populate(nb_objects=10) game = M.build() assert "GameMaker" in game.metadata["desc"] with make_temp_directory(prefix="test_making_a_small_game") as tmpdir: game_file = _compile_game(game, folder=tmpdir) if play_the_game: textworld.play(game_file)
def test_game_ended_when_no_quest(self): M = GameMaker() room = M.new_room() M.set_player(room) item = M.new(type="o") room.add(item) game = M.build() game_name = "test_game_ended_when_no_quest" with make_temp_directory(prefix=game_name) as tmpdir: game_file = textworld.generator.compile_game(game, path=tmpdir) env = textworld.start(game_file) env.activate_state_tracking() game_state = env.reset() assert not game_state.game_ended game_state, _, done = env.step("look") assert not done assert not game_state.game_ended
def test_game_ended_when_no_quest(self): M = GameMaker() room = M.new_room() M.set_player(room) item = M.new(type="o") room.add(item) game = M.build() game_name = "test_game_ended_when_no_quest" with make_temp_directory(prefix=game_name) as tmpdir: options = textworld.GameOptions() options.path = pjoin(tmpdir, "tw-no_quest.z8") game_file = textworld.generator.compile_game(game, options) env = JerichoEnv(self.infos) env.load(game_file) game_state = env.reset() assert not game_state.lost assert not game_state.won game_state, _, _ = env.step("look") assert not game_state.lost assert not game_state.won