Beispiel #1
0
 def test_syntax_checker_good_game8(self):
     game_internal = Game([[
         GameObject('starting_room', [], [
             GameObject('door', [GameObjectAttribute.IMMOBILE]),
             GameObject('keypart1'),
             GameObject('box', [GameObjectAttribute.IMMOBILE],
                        [GameObject('keypart2')])
         ]),
         GameObject('ending_room')
     ], [GameObject('key')],
                           [
                               GamePassage(11, 'starting_room',
                                           'ending_room', 'N', 'S',
                                           [GameObjectAttribute.INVISIBLE])
                           ],
                           [
                               GamePassageRevealAction('door', 'key', 11),
                               GameObjectUseAction('keypart1', 'keypart2',
                                                   'key')
                           ], [], 'ending_room', {}])
     verdict = GameSyntaxChecker().check(game_internal)
     assert (verdict == '')
     solution = GameSolver().solve(game_internal)
     assert (solution == [['take', 'keypart1'], ['open', 'box'],
                          ['take', 'keypart2'],
                          ['use', 'keypart1', 'keypart2'],
                          ['use', 'door', 'key'], ['go', 'N']])
Beispiel #2
0
def main(argv):
    if len(argv) != 2:
        print "USAGE:", os.path.basename(argv[0]), "<game dat file>"
        return
    gamefile = argv[1]
    exit_commands = ['quit', 'exit']
    inventory_commands = ['inventory']
    game = None
    try:
        with open(gamefile, 'r') as myfile:
            game_blueprints = myfile.read().replace('\n', '')
            game = Game(GameDecoder().decode(game_blueprints))
    except IOError as e:
        print "I/O error({0}): {1}".format(e.errno, e.strerror)
        return
    except Exception as e:
        print "Could not parse game file."
        print e
        return
    result = GameSyntaxChecker().check(game)
    if result != "":
        print "Error: ", result
        return
    else:
        print "Game description in", gamefile, "is error-free."
    solution = GameSolver().solve(game)
    print
    print "The solution is:"
    print solution
Beispiel #3
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 #4
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 #5
0
 def test_solver_on_full_game(self):
     verdict = GameSyntaxChecker().check(self.game1)
     assert (verdict == '')
     solution = GameSolver().solve(self.game1)
     assert (solution == [['take', 'candle'], ['take', 'match'],
                          ['take', 'bird'], ['take', 'stone'],
                          ['use', 'candle', 'match'],
                          ['use', 'bird', 'stone'], ['use', '', 'picture'],
                          ['go', 'W']])
Beispiel #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
0
 def test_syntax_checker_wrong_game_1(self):
     # there is no room
     game_internal = Game([[], [], [], [], [], '', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'must have at least one room')
Beispiel #24
0
def main(argv):
    if len(argv) != 2:
        print "USAGE:", os.path.basename(argv[0]), "<game dat file>"
        return
    gamefile = argv[1]
    exit_commands = ['quit', 'exit']
    inventory_commands = ['inventory']
    print "Opening file", gamefile
    print
    game = None
    try:
        with open(gamefile, 'r') as myfile:
            game_blueprints = myfile.read().replace('\n', '')
            game = Game(GameDecoder().decode(game_blueprints))
    except IOError as e:
        print "I/O error({0}): {1}".format(e.errno, e.strerror)
        return
    except Exception as e:
        print "Could not parse game file."
        print e
        return
    result = GameSyntaxChecker().check(game)
    if result != "":
        print "Error: ", result
        return

    print "Commands to use:",
    for command in game.commands.keys() + exit_commands + inventory_commands:
        print command,
    print
    print

    looked = {}
    while True:

        if not game.current_room() in looked:
            print game.look()
            looked[game.current_room()] = True
        else:
            print "You are in a", game.current_room()

        if game.won():
            break

        if len(game.stuffs()) > 0:
            print "The following objects are in the room:",
            for stuff in game.stuffs():
                print stuff,
            print "."
        if len(game.directions()) > 0:
            print "You can go to the following directions:",
            for dirdesc in game.directions():
                print dirdesc[0],
            print "."

        input_fields = []
        while len(input_fields) <= 0:
            input_fields = raw_input('>').split()
            # better than split('')

        if len(input_fields) > 3:
            print "You cannot do that."

        input_fields += [
            '', ''
        ]  # to set a default value for not given params in an easy way

        if input_fields[1] == '' and input_fields[2] == '':
            if input_fields[0] in exit_commands:
                break
            if input_fields[0] in inventory_commands:
                if len(game.inventory()) > 0:
                    print 'You have the following items:',
                    for item in game.inventory():
                        print item,
                    print
                else:
                    print 'You have no items.'
                continue

        try:
            game.do_it(input_fields[0], input_fields[1], input_fields[2])
        except Exception:
            print "You cannot do that."
Beispiel #25
0
 def test_syntax_checker_wrong_game_3(self):
     # starting in the ending room
     game_internal = Game([[GameObject('room1', [], [])], [], [], [], [],
                           'final_room', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'final room does not exist')
Beispiel #26
0
 def test_syntax_checker_wrong_game_4(self):
     game_internal = Game(
         [[GameObject('starting_room'),
           GameObject('final_room')], [], [], [], [], 'final_room', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'final room is not reachable')
Beispiel #27
0
 def test_syntax_checker_wrong_game_2(self):
     # starting in the ending room
     game_internal = Game([[GameObject('room1', [], [])], [], [], [], [],
                           'room1', {}])
     assert (GameSyntaxChecker().check(game_internal) ==
             'cannot start in the ending room')