Beispiel #1
0
 def test_syntax_checker_good_game7(self):
     game_internal = Game(
         [[
             GameObject(
                 'starting_room', [],
                 [GameObject('key', [GameObjectAttribute.INVISIBLE])]),
             GameObject('middle_room', [], [
                 GameObject('door', [GameObjectAttribute.IMMOBILE]),
                 GameObject('box', [GameObjectAttribute.IMMOBILE],
                            [GameObject('burning_candle')])
             ]),
             GameObject('ending_room')
         ], [],
          [
              GamePassage(11, 'middle_room', 'ending_room', 'N', 'S',
                          [GameObjectAttribute.INVISIBLE]),
              GamePassage(12, 'starting_room', 'middle_room', 'N', 'S')
          ], [GamePassageRevealAction('door', 'key', 11)],
          [GameObjectRevealAction('key', 'burning_candle')], 'ending_room',
          {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == '')
     solution = GameSolver().solve(game_internal)
     assert (solution == [['go', 'N'], ['open', 'box'],
                          ['take', 'burning_candle'], ['go', 'S'],
                          ['take', 'key'], ['go', 'N'],
                          ['use', 'door', 'key'], ['go', 'N']])
Beispiel #2
0
 def test_syntax_checker_wrong_game_9(self):
     game_internal = Game([[GameObject('roomA'),
                            GameObject('roomB')], [],
                           [
                               GamePassage(11, 'roomA', 'roomB', 'N', 'S'),
                               GamePassage(12, 'roomB', 'roomA', 'W', 'S')
                           ], [], [], 'roomB', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'multiple passages between the rooms roomA, roomB')
Beispiel #3
0
 def test_syntax_checker_wrong_game_10(self):
     game_internal = Game(
         [[GameObject('roomA'),
           GameObject('roomB'),
           GameObject('roomC')], [],
          [
              GamePassage(11, 'roomA', 'roomB', 'N', 'S'),
              GamePassage(11, 'roomB', 'roomC', 'W', 'S')
          ], [], [], 'roomC', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'passage identifiers are not unique, 11')
Beispiel #4
0
 def test_syntax_checker_wrong_game_15(self):
     game_internal = Game([[
         GameObject('roomA'),
         GameObject('roomC'),
         GameObject('roomB'),
         GameObject('roomC')
     ], [],
                           [
                               GamePassage(11, 'roomA', 'roomB', 'N', 'S'),
                               GamePassage(12, 'roomB', 'roomC', 'N', 'S')
                           ], [], [], 'roomC', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == 'found two objects with the same name, roomC')
Beispiel #5
0
 def test_syntax_checker_wrong_game_11(self):
     game_internal = Game([[
         GameObject('roomA'),
         GameObject('roomB'),
         GameObject('roomC'),
         GameObject('roomD')
     ], [],
                           [
                               GamePassage(11, 'roomA', 'roomB', 'N', 'S'),
                               GamePassage(12, 'roomC', 'roomD', 'N', 'S')
                           ], [], [], 'roomB', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'not all rooms are accessible, roomC')
Beispiel #6
0
 def test_syntax_checker_wrong_game_6(self):
     game_internal = Game([[
         GameObject('roomA'),
         GameObject('roomB'),
         GameObject('roomC'),
         GameObject('roomD')
     ], [],
                           [
                               GamePassage(11, 'roomA', 'roomB', 'N', 'S'),
                               GamePassage(12, 'roomB', 'roomC', 'N', 'S')
                           ], [], [], 'roomD', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'final room is not reachable')
Beispiel #7
0
 def test_syntax_checker_good_game1(self):
     # minimal valid game
     game_internal = Game(
         [[GameObject('starting_room'),
           GameObject('final_room')], [],
          [GamePassage(11, 'starting_room', 'final_room', 'N', 'S')], [],
          [], 'final_room', {}])
     assert (GameSyntaxChecker().check(game_internal) == '')
     assert (GameSolver().solve(game_internal) == [['go', 'N']])
Beispiel #8
0
 def test_syntax_checker_wrong_game29(self):
     game_internal = Game(
         [[GameObject('starting_room'),
           GameObject('final/room')], [],
          [GamePassage(11, 'starting_room', 'final/room', 'N', 'S')], [],
          [], 'final/room', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (
         verdict ==
         'game object names can contain only lower case alphabets and _, final/room'
     )
Beispiel #9
0
 def test_syntax_checker_wrong_game28(self):
     game_internal = Game(
         [[
             GameObject('starting_room', [], [
                 GameObject('door', [GameObjectAttribute.IMMOBILE]),
                 GameObject('key', [])
             ]),
             GameObject('ending_room')
         ], [GameObject('broken_key')],
          [
              GamePassage(11, 'starting_room', 'strange_room', 'W', 'E',
                          []),
              GamePassage(12, 'strange_room', 'ending_room', 'N', 'S',
                          [GameObjectAttribute.INVISIBLE])
          ], [GameObjectUseAction('door', 'key', 'broken_key')],
          [GamePassageRevealAction('broken_key', '', 12)], 'ending_room',
          {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (
         verdict == "found not existing room in a passage: strange_room")
Beispiel #10
0
 def test_syntax_checker_wrong_game16(self):
     game_internal = Game([[
         GameObject('starting_room', [],
                    [GameObject('door', [GameObjectAttribute.IMMOBILE])]),
         GameObject('ending_room')
     ], [],
                           [
                               GamePassage(11, 'starting_room',
                                           'ending_room', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ], [GamePassageRevealAction('', '', 11)], [],
                           'ending_room', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == 'found an action without actors')
Beispiel #11
0
 def test_syntax_checker_wrong_game_12(self):
     game_internal = Game([[
         GameObject(
             'roomA', [],
             [GameObject('button', [GameObjectAttribute.IMMOBILE], [])]),
         GameObject('roomB')
     ], [],
                           [
                               GamePassage(11, 'roomA', 'roomB', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ], [GamePassageRevealAction('button', '', 13)],
                           [], 'roomB', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'invalid passage identifiers in an action, 13')
Beispiel #12
0
 def test_syntax_checker_wrong_game_14(self):
     game_internal = Game([[
         GameObject('roomA', [], [
             GameObject('button1', [GameObjectAttribute.IMMOBILE], []),
             GameObject('button1', [GameObjectAttribute.IMMOBILE], [])
         ]),
         GameObject('roomB')
     ], [],
                           [
                               GamePassage(11, 'roomA', 'roomB', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ], [GamePassageRevealAction('button1', '', 11)],
                           [], 'roomB', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == 'found two objects with the same name, button1')
Beispiel #13
0
    def test_json_game_deserializer_serializer_1(self):
        game_internal_text = '[[{"obj_content": {"attributes": [], "childObjects": [], "name": "roomA"}, "obj_name": "GameObject"},\
                              {"obj_content": {"attributes": [], "childObjects": [], "name": "roomB"}, "obj_name": "GameObject"},\
                              {"obj_content": {"attributes": [], "childObjects": [], "name": "roomC"}, "obj_name": "GameObject"},\
                              {"obj_content": {"attributes": [], "childObjects": [], "name": "roomD"}, "obj_name": "GameObject"},\
                              {"obj_content": {"attributes": [], "childObjects": [], "name": "roomE"}, "obj_name": "GameObject"},\
                              {"obj_content": {"attributes": [], "childObjects": [], "name": "roomF"}, "obj_name": "GameObject"}],\
                             [],\
                             [{"obj_content": {"room_name2": "roomB", "room_name1": "roomA", "direction2": "S", "attributes": [], "direction1": "N", "identifier": 11},\
                                 "obj_name": "GamePassage"},\
                              {"obj_content": {"room_name2": "roomE", "room_name1": "roomA", "direction2": "W", "attributes": [], "direction1": "E", "identifier": 12},\
                                 "obj_name": "GamePassage"},\
                              {"obj_content": {"room_name2": "roomB", "room_name1": "roomE", "direction2": "E", "attributes": [], "direction1": "N", "identifier": 13},\
                                 "obj_name": "GamePassage"},\
                              {"obj_content": {"room_name2": "roomE", "room_name1": "roomD", "direction2": "S", "attributes": [], "direction1": "N", "identifier": 14},\
                                 "obj_name": "GamePassage"},\
                              {"obj_content": {"room_name2": "roomF", "room_name1": "roomC", "direction2": "W", "attributes": [], "direction1": "E", "identifier": 15},\
                                 "obj_name": "GamePassage"}], [], [], "roomF", {}]'

        game_from_text = Game(GameDecoder().decode(game_internal_text))
        game_internal = Game([[
            GameObject('roomA'),
            GameObject('roomB'),
            GameObject('roomC'),
            GameObject('roomD'),
            GameObject('roomE'),
            GameObject('roomF')
        ], [],
                              [
                                  GamePassage(11, 'roomA', 'roomB', 'N', 'S'),
                                  GamePassage(12, 'roomA', 'roomE', 'E', 'W'),
                                  GamePassage(13, 'roomE', 'roomB', 'N', 'E'),
                                  GamePassage(14, 'roomD', 'roomE', 'N', 'S'),
                                  GamePassage(15, 'roomC', 'roomF', 'E', 'W')
                              ], [], [], 'roomF', {}])
        assert (game_internal == game_from_text)
Beispiel #14
0
 def test_syntax_checker_good_game10(self):
     game_internal = Game(
         [[
             GameObject('starting_room', [], [
                 GameObject('door', [GameObjectAttribute.IMMOBILE]),
                 GameObject('key', [])
             ]),
             GameObject('strange_room'),
             GameObject('ending_room')
         ], [GameObject('broken_key')],
          [
              GamePassage(11, 'starting_room', 'strange_room', 'W', 'E',
                          []),
              GamePassage(12, 'strange_room', 'ending_room', 'N', 'S',
                          [GameObjectAttribute.INVISIBLE])
          ], [GameObjectUseAction('door', 'key', 'broken_key')],
          [GamePassageRevealAction('broken_key', '', 12)], 'ending_room',
          {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == "")
     assert (GameSolver().solve(game_internal) == [['take', 'key'],
                                                   ['use', 'door', 'key'],
                                                   ['go', 'W'], ['go',
                                                                 'N']])
Beispiel #15
0
 def test_syntax_checker_wrong_game24(self):
     game_internal = Game(
         [[
             GameObject(
                 'starting_room', [],
                 [GameObject('key', [GameObjectAttribute.INVISIBLE])]),
             GameObject('middle_room', [], [
                 GameObject('burning_candle'),
                 GameObject('door', [GameObjectAttribute.IMMOBILE])
             ]),
             GameObject('ending_room')
         ], [],
          [
              GamePassage(11, 'middle_room', 'ending_room', 'N', 'S',
                          [GameObjectAttribute.INVISIBLE]),
              GamePassage(12, 'starting_room', 'middle_room', 'N', 'S')
          ], [GamePassageRevealAction('door', 'key', 11)],
          [GameObjectRevealAction('burning_candle', 'key')], 'ending_room',
          {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (
         verdict ==
         'subjects of revealing actions must be invisible initially, burning_candle'
     )
Beispiel #16
0
 def test_syntax_checker_good_game3(self):
     game_internal = Game([[
         GameObject('starting_room', [],
                    [GameObject('door', [GameObjectAttribute.IMMOBILE])]),
         GameObject('ending_room')
     ], [],
                           [
                               GamePassage(11, 'starting_room',
                                           'ending_room', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ], [GamePassageRevealAction('door', '', 11)], [],
                           'ending_room', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == '')
     assert (GameSolver().solve(game_internal) == [['use', '', 'door'],
                                                   ['go', 'N']])
Beispiel #17
0
 def test_syntax_checker_wrong_game27(self):
     game_internal = Game([[
         GameObject('starting_room', [], [
             GameObject('door', [GameObjectAttribute.IMMOBILE]),
             GameObject('key', [])
         ]),
         GameObject('ending_room')
     ], [],
                           [
                               GamePassage(11, 'starting_room',
                                           'ending_room', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ], [GamePassageRevealAction('key', 'door', 11)],
                           [], 'ending_room', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == "action actor door must be mobile")
Beispiel #18
0
 def test_syntax_checker_wrong_game22(self):
     game_internal = Game([[
         GameObject('starting_room', [], [
             GameObject('door', [GameObjectAttribute.IMMOBILE]),
             GameObject('box', [GameObjectAttribute.IMMOBILE],
                        [GameObject('key', [GameObjectAttribute.IMMOBILE])])
         ]),
         GameObject('ending_room')
     ], [],
                           [
                               GamePassage(11, 'starting_room',
                                           'ending_room', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ], [GamePassageRevealAction('door', 'key', 11)],
                           [], 'ending_room', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == 'not top level stuffs cannot have attributes, key')
Beispiel #19
0
 def test_syntax_checker_wrong_game_19(self):
     game_internal = Game([[
         GameObject('roomA', [], [
             GameObject('button1', [GameObjectAttribute.IMMOBILE], []),
             GameObject('button2', [GameObjectAttribute.IMMOBILE], [])
         ]),
         GameObject('roomB')
     ], [GameObject('broken button')],
                           [
                               GamePassage(11, 'roomA', 'roomB', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ],
                           [
                               GamePassageRevealAction('button1', '', 11),
                               GameObjectUseAction('', 'button1',
                                                   'broken button')
                           ], [], 'roomB', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (
         verdict == 'found multiple actions for the same actor, button1')
Beispiel #20
0
 def test_syntax_checker_wrong_game26(self):
     game_internal = Game([[
         GameObject('starting_room', [], [
             GameObject('door', [
                 GameObjectAttribute.IMMOBILE, GameObjectAttribute.INVISIBLE
             ]),
             GameObject('key')
         ]),
         GameObject('ending_room')
     ], [],
                           [
                               GamePassage(11, 'starting_room',
                                           'ending_room', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ], [GamePassageRevealAction('door', 'key', 11)],
                           [], 'ending_room', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (
         verdict ==
         'there must be exactly one action for each invisible object which reveals it, door'
     )
Beispiel #21
0
 def test_syntax_checker_good_game_2(self):
     # testing whether final room is accessible
     game_internal = Game([[
         GameObject('roomA'),
         GameObject('roomB'),
         GameObject('roomC'),
         GameObject('roomD'),
         GameObject('roomE'),
         GameObject('roomF')
     ], [],
                           [
                               GamePassage(11, 'roomA', 'roomB', 'N', 'S'),
                               GamePassage(12, 'roomA', 'roomE', 'E', 'W'),
                               GamePassage(13, 'roomD', 'roomC', 'E', 'W'),
                               GamePassage(14, 'roomE', 'roomB', 'N', 'E'),
                               GamePassage(15, 'roomD', 'roomE', 'N', 'S'),
                               GamePassage(16, 'roomC', 'roomF', 'E', 'W')
                           ], [], [], 'roomF', {}])
     assert (GameSyntaxChecker().check(game_internal) == '')
     assert (GameSolver().solve(game_internal) == [['go', 'N'], ['go', 'E'],
                                                   ['go', 'S'], ['go', 'E'],
                                                   ['go', 'E']])
Beispiel #22
0
    def setUp(self):
        # Test game1, just to start with something

        self.game1_text_blueprints = """
            [
            [{"obj_content": {"attributes": [],
                              "childObjects": [{"obj_content": {"attributes": ["immobile"], "childObjects": [], "name": "table"}, "obj_name": "GameObject"},
                                               {"obj_content": {"attributes": [], "childObjects": [], "name": "candle"}, "obj_name": "GameObject"},
                                               {"obj_content": {"attributes": [], "childObjects": [], "name": "match"}, "obj_name": "GameObject"},
                                               {"obj_content": {"attributes": [], "childObjects": [], "name": "bird"}, "obj_name": "GameObject"},
                                               {"obj_content": {"attributes": [], "childObjects": [], "name": "stone"}, "obj_name": "GameObject"},
                                               {"obj_content": {"attributes": ["immobile", "invisible"], "childObjects": [], "name": "picture"}, "obj_name": "GameObject"}],
                              "name": "dark_room"},
              "obj_name": "GameObject"},
             {"obj_content": {"attributes": [],
                              "childObjects": [{"obj_content": {"attributes": ["immobile"],
                                                                "childObjects": [{"obj_content": {"attributes": [], "childObjects": [], "name": "knife"}, "obj_name": "GameObject"}],
                                                                "name": "cabinet"},
                                                "obj_name": "GameObject"}],
                              "name": "bathroom"},
              "obj_name": "GameObject"},
             {"obj_content": {"attributes": [],
                              "childObjects": [],
                              "name": "secret_room"},
              "obj_name": "GameObject"}],
            [{"obj_content": {"attributes": [],
                              "childObjects": [],
                              "name": "burning_candle"},
              "obj_name": "GameObject"},
             {"obj_content": {"attributes": [],
                              "childObjects": [],
                              "name": "injured_bird"},
              "obj_name": "GameObject"}],
            [{"obj_content": {"room_name2": "bathroom", "room_name1": "dark_room", "direction2": "S", "attributes": [], "direction1": "N", "identifier": 11},
              "obj_name": "GamePassage"},
             {"obj_content": {"room_name2": "secret_room", "room_name1": "dark_room", "direction2": "E", "attributes": ["invisible"], "direction1": "W", "identifier": 12},
              "obj_name": "GamePassage"}],
            [{"obj_content": {"subjectname": "candle", "toolname": "match", "resultname": "burning_candle"},
              "obj_name": "GameObjectUseAction"},
             {"obj_content": {"subjectname": "bird", "toolname": "stone", "resultname": "injured_bird"},
              "obj_name": "GameObjectUseAction"},
             {"obj_content": {"subjectname": "picture", "toolname": "", "identifier": 12},
              "obj_name": "GamePassageRevealAction"}],
            [{"obj_content": {"subjectname": "picture", "toolname": "burning_candle"},
              "obj_name": "GameObjectRevealAction"}],
            "secret_room",
            {"go#dark_room": "dark_room", "go#bathroom": "bathroom"}
            ]
      """

        self.game1 = Game([[
            GameObject('dark_room', [], [
                GameObject('table', [GameObjectAttribute.IMMOBILE], []),
                GameObject('candle'),
                GameObject('match'),
                GameObject('bird'),
                GameObject('stone'),
                GameObject('picture', [
                    GameObjectAttribute.IMMOBILE, GameObjectAttribute.INVISIBLE
                ])
            ]),
            GameObject('bathroom', [], [
                GameObject('cabinet', [GameObjectAttribute.IMMOBILE],
                           [GameObject('knife')])
            ]),
            GameObject('secret_room')
        ], [GameObject('burning_candle'),
            GameObject('injured_bird')],
                           [
                               GamePassage(11, 'dark_room', 'bathroom', 'N',
                                           'S'),
                               GamePassage(12, 'dark_room', 'secret_room', 'W',
                                           'E',
                                           [GameObjectAttribute.INVISIBLE])
                           ],
                           [
                               GameObjectUseAction('candle', 'match',
                                                   'burning_candle'),
                               GameObjectUseAction('bird', 'stone',
                                                   'injured_bird'),
                               GamePassageRevealAction('picture', '', 12)
                           ],
                           [
                               GameObjectRevealAction('picture',
                                                      'burning_candle')
                           ], 'secret_room', {
                               'go#dark_room': 'dark_room',
                               'go#bathroom': 'bathroom'
                           }])
        assert (self.game1.look() == 'dark_room')
        assert (self.game1.has('burning_candle') is None)
        assert (self.game1.has('candle') is None)
        assert (self.game1.has('match') is None)
        assert ('candle' in self.game1.stuffs())
        assert ('match' in self.game1.stuffs())
        assert ('table' in self.game1.stuffs())
        assert (not 'picture' in self.game1.stuffs())
        assert (self.game1.directions() == [['N', 'bathroom']])
        assert (self.game1.won() == 0)