def setUpClass(cls): M = GameMaker() # The goal commands = ["take carrot", "insert carrot into chest"] R1 = M.new_room("room") M.set_player(R1) carrot = M.new(type='f', name='carrot') R1.add(carrot) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("open") R1.add(chest) cls.event = M.new_event_using_commands(commands) cls.actions = cls.event.actions cls.conditions = {M.new_fact("in", carrot, chest)}
def test_game_without_a_quest(self): M = GameMaker() room = M.new_room() M.set_player(room) item = M.new(type="o") room.add(item) game = M.build() game_progress = GameProgression(game) assert not game_progress.done assert game_progress.winning_policy is None # Simulate action that doesn't change the world. action = game_progress.valid_actions[0] game_progress.update(action) assert not game_progress.done
def test_game_with_multiple_quests(self): M = GameMaker() # The subgoals (needs to be executed in order). commands = [ [ "open wooden door", "go west", "take carrot", "go east", "drop carrot" ], # Now, the player is back in the kitchen and the wooden door is open. ["go west", "take lettuce", "go east", "drop lettuce"], # Now, the player is back in the kitchen, there are a carrot and a lettuce on the floor. [ "take lettuce", "take carrot", "insert carrot into chest", "insert lettuce into chest", "close chest" ] ] # Create a 'bedroom' room. R1 = M.new_room("bedroom") R2 = M.new_room("kitchen") M.set_player(R2) path = M.connect(R1.east, R2.west) path.door = M.new(type='d', name='wooden door') path.door.add_property("closed") carrot = M.new(type='f', name='carrot') lettuce = M.new(type='f', name='lettuce') R1.add(carrot, lettuce) # 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[0]) quest1.desc = "Fetch the carrot and drop it on the kitchen's ground." quest2 = M.new_quest_using_commands(commands[0] + commands[1]) quest2.desc = "Fetch the lettuce and drop it on the kitchen's ground." quest3 = M.new_quest_using_commands(commands[0] + commands[1] + commands[2]) winning_facts = [ M.new_fact("in", lettuce, chest), M.new_fact("in", carrot, chest), M.new_fact("closed", chest), ] quest3.win_events[0].set_conditions(winning_facts) quest3.desc = "Put the lettuce and the carrot into the chest before closing it." M.quests = [quest1, quest2, quest3] assert len(M.quests) == len(commands) game = M.build() inform7 = Inform7Game(game) game_progress = GameProgression(game) assert len(game_progress.quest_progressions) == len(game.quests) # Following the actions associated to the last quest actually corresponds # to solving the whole game. for action in game_progress.winning_policy: assert not game_progress.done game_progress.update(action) assert game_progress.done assert all(quest_progression.done for quest_progression in game_progress.quest_progressions) # Try solving the game by greedily taking the first action from the current winning policy. game_progress = GameProgression(game) while not game_progress.done: action = game_progress.winning_policy[0] game_progress.update(action) # print(action.name, [c.name for c in game_progress.winning_policy]) # Try solving the second quest (i.e. bringing back the lettuce) first. game_progress = GameProgression(game) for command in [ "open wooden door", "go west", "take lettuce", "go east", "drop lettuce" ]: _apply_command(command, game_progress, inform7) assert not game_progress.quest_progressions[0].done assert game_progress.quest_progressions[1].done for command in ["go west", "take carrot", "go east", "drop carrot"]: _apply_command(command, game_progress, inform7) assert game_progress.quest_progressions[0].done assert game_progress.quest_progressions[1].done for command in [ "take lettuce", "take carrot", "insert carrot into chest", "insert lettuce into chest", "close chest" ]: _apply_command(command, game_progress, inform7) assert game_progress.done # Game is done whenever a quest has failed or is unfinishable. game_progress = GameProgression(game) for command in [ "open wooden door", "go west", "take carrot", "eat carrot" ]: assert not game_progress.done _apply_command(command, game_progress, inform7) assert game_progress.done
def test_cycle_in_winning_policy(self): M = GameMaker() # Create a map. # r0 # | # r1 -- r2 # | | # r3 -- r4 R0 = M.new_room("r0") R1 = M.new_room("r1") R2 = M.new_room("r2") R3 = M.new_room("r3") R4 = M.new_room("r4") M.set_player(R1) M.connect(R0.south, R1.north), M.connect(R1.east, R2.west), M.connect(R3.east, R4.west) M.connect(R1.south, R3.north) M.connect(R2.south, R4.north) carrot = M.new(type='f', name='carrot') R0.add(carrot) apple = M.new(type='f', name='apple') R2.add(apple) commands = ["go north", "take carrot"] M.set_quest_from_commands(commands) game = M.build() inform7 = Inform7Game(game) game_progression = GameProgression(game) _apply_command("go south", game_progression, inform7) expected_commands = ["go north"] + commands winning_commands = inform7.gen_commands_from_actions( game_progression.winning_policy) assert winning_commands == expected_commands, "{} != {}".format( winning_commands, expected_commands) _apply_command("go east", game_progression, inform7) _apply_command("go north", game_progression, inform7) expected_commands = ["go south", "go west", "go north"] + commands winning_commands = inform7.gen_commands_from_actions( game_progression.winning_policy) assert winning_commands == expected_commands, "{} != {}".format( winning_commands, expected_commands) _apply_command("go west", game_progression, inform7) # Found shortcut expected_commands = commands winning_commands = inform7.gen_commands_from_actions( game_progression.winning_policy) assert winning_commands == expected_commands, "{} != {}".format( winning_commands, expected_commands) # Quest where player's has to pick up the carrot first. commands = [ "go east", "take apple", "go west", "go north", "drop apple" ] M.set_quest_from_commands(commands) game = M.build() game_progression = GameProgression(game) _apply_command("go south", game_progression, inform7) expected_commands = ["go north"] + commands winning_commands = inform7.gen_commands_from_actions( game_progression.winning_policy) assert winning_commands == expected_commands, "{} != {}".format( winning_commands, expected_commands) _apply_command("go east", game_progression, inform7) expected_commands = ["go west", "go north"] + commands winning_commands = inform7.gen_commands_from_actions( game_progression.winning_policy) assert winning_commands == expected_commands, "{} != {}".format( winning_commands, expected_commands) _apply_command("go north", game_progression, inform7) # Found shortcut expected_commands = commands[1:] winning_commands = inform7.gen_commands_from_actions( game_progression.winning_policy) assert winning_commands == expected_commands, "{} != {}".format( winning_commands, expected_commands)
def setUpClass(cls): M = GameMaker() # Create a 'bedroom' room. R1 = M.new_room("bedroom") R2 = M.new_room("kitchen") M.set_player(R2) path = M.connect(R1.east, R2.west) path.door = M.new(type='d', name='wooden door') path.door.add_property("closed") carrot = M.new(type='f', name='carrot') lettuce = M.new(type='f', name='lettuce') R1.add(carrot) R1.add(lettuce) # Add a closed chest in R2. chest = M.new(type='c', name='chest') chest.add_property("open") R2.add(chest) # The goals commands = [ "open wooden door", "go west", "take carrot", "go east", "drop carrot" ] cls.eventA = M.new_event_using_commands(commands) commands = [ "open wooden door", "go west", "take lettuce", "go east", "insert lettuce into chest" ] cls.eventB = M.new_event_using_commands(commands) cls.losing_eventA = Event(conditions={M.new_fact("eaten", carrot)}) cls.losing_eventB = Event(conditions={M.new_fact("eaten", lettuce)}) cls.questA = Quest(win_events=[cls.eventA], fail_events=[cls.losing_eventA]) cls.questB = Quest(win_events=[cls.eventB], fail_events=[cls.losing_eventB]) cls.questC = Quest(win_events=[], fail_events=[cls.losing_eventA, cls.losing_eventB]) commands = [ "open wooden door", "go west", "take lettuce", "go east", "insert lettuce into chest" ] cls.questC = M.new_quest_using_commands(commands) commands = ["open wooden door", "go west", "take carrot", "eat carrot"] cls.eating_carrot = M.new_event_using_commands(commands) commands = [ "open wooden door", "go west", "take lettuce", "eat lettuce" ] cls.eating_lettuce = M.new_event_using_commands(commands) commands = [ "open wooden door", "go west", "take lettuce", "go east", "insert lettuce into chest" ] M.quests = [cls.questA, cls.questB] cls.game = M.build()
def setUpClass(cls): M = GameMaker() room = M.new_room("room") M.set_player(room) carrot = M.new(type='f', name='carrot') lettuce = M.new(type='f', name='lettuce') room.add(carrot) room.add(lettuce) chest = M.new(type='c', name='chest') chest.add_property("open") room.add(chest) # The goals commands = ["take carrot", "insert carrot into chest"] cls.eventA = M.new_event_using_commands(commands) commands = ["take lettuce", "insert lettuce into chest", "close chest"] event = M.new_event_using_commands(commands) cls.eventB = Event(actions=event.actions, conditions={ M.new_fact("in", lettuce, chest), M.new_fact("closed", chest) }) cls.fail_eventA = Event(conditions={M.new_fact("eaten", carrot)}) cls.fail_eventB = Event(conditions={M.new_fact("eaten", lettuce)}) cls.quest = Quest(win_events=[cls.eventA, cls.eventB], fail_events=[cls.fail_eventA, cls.fail_eventB]) commands = ["take carrot", "eat carrot"] cls.eating_carrot = M.new_event_using_commands(commands) commands = ["take lettuce", "eat lettuce"] cls.eating_lettuce = M.new_event_using_commands(commands) commands = ["take lettuce", "insert lettuce into chest"] M.quests = [cls.quest] cls.game = M.build()
def setUpClass(cls): 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) cls.game = M.build()
def setUpClass(cls): 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) cls.eventA = M.new_event_using_commands(commands) cls.eventB = Event(conditions={ M.new_fact("at", carrot, R1), M.new_fact("closed", path.door) }) cls.eventC = Event(conditions={M.new_fact("eaten", carrot)}) cls.eventD = Event(conditions={ M.new_fact("closed", chest), M.new_fact("closed", path.door) }) cls.quest = Quest(win_events=[cls.eventA, cls.eventB], fail_events=[cls.eventC, cls.eventD], reward=2) M.quests = [cls.quest] cls.game = M.build() cls.inform7 = Inform7Game(cls.game)