Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
def test_reloading_game_with_custom_kb():
    twl = KnowledgeBase.default().logic._document
    twl += """
        type customobj : o {
            inform7 {
                type {
                    kind :: "custom-obj-like";
                }
            }
        }
    """

    logic = GameLogic.parse(twl)
    options = GameOptions()
    options.kb = KnowledgeBase(logic, "")
    M = GameMaker(options)

    room = M.new_room("room")
    M.set_player(room)

    custom_obj = M.new(type='customobj', name='customized object')
    M.inventory.add(custom_obj)

    commands = ["drop customized object"]
    quest = M.set_quest_from_commands(commands)
    assert quest.commands == tuple(commands)
    game = M.build()
    assert game == Game.deserialize(game.serialize())
Ejemplo n.º 3
0
    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')
        R1.add(carrot)

        # Add a closed chest in R2.
        chest = M.new(type='c', name='chest')
        chest.add_property("open")
        R2.add(chest)

        # The goal
        commands = ["open wooden door", "go west", "take carrot", "go east", "drop carrot"]
        cls.quest = M.set_quest_from_commands(commands)

        commands = ["open wooden door", "go west", "take carrot", "eat carrot"]
        cls.eating_quest = M.new_quest_using_commands(commands)

        cls.game = M.build()
Ejemplo n.º 4
0
    def test_cycle_in_winning_policy(cls):
        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()
        game_progression = GameProgression(game)

        def _apply_command(command, game_progression):
            valid_commands = gen_commands_from_actions(game_progression.valid_actions, game.infos)

            for action, cmd in zip(game_progression.valid_actions, valid_commands):
                if command == cmd:
                    game_progression.update(action)
                    return

            raise ValueError("Not a valid command: {}. Expected: {}".format(command, valid_commands))

        _apply_command("go south", game_progression)
        expected_commands = ["go north"] + commands
        winning_commands = gen_commands_from_actions(game_progression.winning_policy, game.infos)
        assert winning_commands == expected_commands, "{} != {}".format(winning_commands, expected_commands)

        _apply_command("go east", game_progression)

        _apply_command("go north", game_progression)
        expected_commands = ["go south", "go west", "go north"] + commands
        winning_commands = gen_commands_from_actions(game_progression.winning_policy, game.infos)
        assert winning_commands == expected_commands, "{} != {}".format(winning_commands, expected_commands)

        _apply_command("go west", game_progression)  # Found shortcut
        expected_commands = commands
        winning_commands = gen_commands_from_actions(game_progression.winning_policy, game.infos)
        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)
        expected_commands = ["go north"] + commands
        winning_commands = gen_commands_from_actions(game_progression.winning_policy, game.infos)
        assert winning_commands == expected_commands, "{} != {}".format(winning_commands, expected_commands)

        _apply_command("go east", game_progression)
        expected_commands = ["go west", "go north"] + commands
        winning_commands = gen_commands_from_actions(game_progression.winning_policy, game.infos)
        assert winning_commands == expected_commands, "{} != {}".format(winning_commands, expected_commands)

        _apply_command("go north", game_progression)  # Found shortcut
        expected_commands = commands[1:]
        winning_commands = gen_commands_from_actions(game_progression.winning_policy, game.infos)
        assert winning_commands == expected_commands, "{} != {}".format(winning_commands, expected_commands)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
def make_example_game(args):
    """
    This game takes place in a typical house and consists in
    three puzzles:
    1) Escape the room;
    2) Find the missing ingredient;
    3) Finish preparing the dinner.

    Here's the map of the house.
                Bathroom
                    +
                    |
                    +
    Bedroom +--+ Kitchen +----+ Backyard
                    +              +
                    |              |
                    +              +
               Living Room       Garden
    """
    # Make the generation process reproducible.
    g_rng.set_seed(2018)

    M = GameMaker()
    # Start by building the layout of the world.
    bedroom = M.new_room("bedroom")
    kitchen = M.new_room("kitchen")
    livingroom = M.new_room("living room")
    bathroom = M.new_room("bathroom")
    backyard = M.new_room("backyard")
    garden = M.new_room("garden")

    # Connect rooms together.
    bedroom_kitchen = M.connect(bedroom.east, kitchen.west)
    kitchen_bathroom = M.connect(kitchen.north, bathroom.south)
    kitchen_livingroom = M.connect(kitchen.south, livingroom.north)
    kitchen_backyard = M.connect(kitchen.east, backyard.west)
    backyard_garden = M.connect(backyard.south, garden.north)

    # Add doors.
    bedroom_kitchen.door = M.new(type='d', name='wooden door')
    kitchen_backyard.door = M.new(type='d', name='screen door')

    kitchen_backyard.door.add_property("closed")

    # Design the bedroom.
    drawer = M.new(type='c', name='chest drawer')
    trunk = M.new(type='c', name='antique trunk')
    bed = M.new(type='s', name='king-size bed')
    bedroom.add(drawer, trunk, bed)

    # - The bedroom's door is locked
    bedroom_kitchen.door.add_property("locked")

    # - and the key is in the drawer.
    bedroom_key = M.new(type='k', name='old key')
    drawer.add(bedroom_key)
    drawer.add_property("closed")
    M.add_fact("match", bedroom_key, bedroom_kitchen.door)

    # - Describe the room.
    bedroom.desc = ""

    # Design the kitchen.
    counter = M.new(type='s', name='counter')
    stove = M.new(type='s', name='stove')
    kitchen_island = M.new(type='s', name='kitchen island')
    refrigerator = M.new(type='c', name='refrigerator')
    kitchen.add(counter, stove, kitchen_island, refrigerator)

    # - Add note on the kitchen island.
    objective = "The dinner is almost ready! It's only missing a grilled carrot."
    note = M.new(type='o', name='note', desc=objective)
    kitchen_island.add(note)

    # - Add some food in the refrigerator.
    apple = M.new(type='f', name='apple')
    milk = M.new(type='f', name='milk')
    refrigerator.add(apple, milk)

    # Design the bathroom.
    toilet = M.new(type='c', name='toilet')
    sink = M.new(type='s', name='sink')
    bath = M.new(type='c', name='bath')
    bathroom.add(toilet, sink, bath)

    toothbrush = M.new(type='o', name='toothbrush')
    sink.add(toothbrush)
    soap_bar = M.new(type='o', name='soap bar')
    bath.add(soap_bar)

    # Design the living room.
    couch = M.new(type='s', name='couch')
    low_table = M.new(type='s', name='low table')
    tv = M.new(type='s', name='tv')
    livingroom.add(couch, low_table, tv)

    remote = M.new(type='o', name='remote')
    low_table.add(remote)
    bag_of_chips = M.new(type='f', name='half of a bag of chips')
    couch.add(bag_of_chips)

    # Design backyard.
    bbq = M.new(type='s', name='bbq')
    patio_table = M.new(type='s', name='patio table')
    chairs = M.new(type='s', name='set of chairs')
    backyard.add(bbq, patio_table, chairs)

    # Design garden.
    shovel = M.new(type='o', name='shovel')
    tomato = M.new(type='f', name='tomato plant')
    carrot = M.new(type='f', name='carrot')
    lettuce = M.new(type='f', name='lettuce')
    garden.add(shovel, tomato, carrot, lettuce)

    # Close all containers
    for container in M.findall(type='c'):
        container.add_property("closed")

    # Set uncooked property for to all food items.
    # NOT IMPLEMENTED YET
    # for food in M.findall(type='f'):
    #     food.add_property("edible")
    #     food.add_property("raw")

    # The player starts in the bedroom.
    M.set_player(bedroom)

    # To define a quest we are going to record it by playing the game.
    # print("******* Recording a quest. Hit 'Ctrl + C' when done. *******")
    # M.record_quest()
    commands = [
        "open chest drawer", "take old key from chest drawer",
        "unlock wooden door with old key", "open wooden door", "go east"
    ]

    if args.type in ["short", "medium", "long", "last", "human"]:
        commands.append("open screen door")

    if args.type in ["medium", "long", "last", "human"]:
        commands.extend(["go east", "go south", "take carrot"])

    if args.type in ["long", "last", "human"]:
        commands.extend([
            "go north",
            "go west",
            "put carrot on stove",
            # "cook carrot"  # Not supported yet.
        ])

    quest = M.set_quest_from_commands(commands)

    # TODO: Provide better API to specify failing conditions.
    quest.set_failing_conditions([Proposition("eaten", [carrot.var])])

    if args.type == "human":
        # Use a very high-level description of the objective.
        quest.desc = ""

    elif args.type == "last":
        # Use a very high-level description of the objective.
        quest.desc = objective

    print(quest.desc)

    game_file = M.compile(args.filename)
    print("*** Game created: {}".format(game_file))
    return game_file