Ejemplo n.º 1
0
def test_save_load_replay():
    replay = [
        ('move', north),
        ('move', south),
        ('move', east),
        ('move', west),
        ('move', northeast),
        ('move', southeast),
        ('move', southwest),
        ('move', northwest),
        ('move', center),
        ('shield', north),
        ('shield', south),
        ('shield', east),
        ('shield', west),
        ('shield', northeast),
        ('shield', southeast),
        ('shield', southwest),
        ('shield', northwest),
        ('shield', center),
    ]
    f = StringIO()
    save_replay(replay, f)
    f.seek(0)
    contents = f.read()
    assert_equal(
        contents,
        dedent(
            '''\
            mn
            ms
            me
            mw
            mne
            mse
            msw
            mnw
            mc
            sn
            ss
            se
            sw
            sne
            sse
            ssw
            snw
            sc
            ''')
    )
    new_replay = load_replay(contents)
    assert_equal(replay, new_replay)
Ejemplo n.º 2
0
def test_solve_all_game_levels():
    all_levels = []
    for dirpath, dirnames, filenames in os.walk('levels'):
        for filename in filenames:
            if filename.startswith('.'):
                continue
            if filename.endswith('.solution'):
                continue

            # Load the level, and make sure there are no errors.
            assert filename not in all_levels
            all_levels.append(filename)

            file_path = os.path.join(dirpath, filename)
            with open(file_path) as f:
                level_string = f.read()
            world = make_world(level_string)

            # Replay the solution, and ensure that it solves the level.
            with open(file_path + '.solution') as f:
                contents = f.read()
            replay = load_replay(contents)
            try:
                for i, move in enumerate(replay):
                    assert world.update(move), 'Solution bumped on move #%d' % (i + 1)
                assert world.level_completed, 'Level not completed'
            except AssertionError:
                print(move)
                print(show_world(world))
                raise
    # Check that all levels are listed in the level sequence.
    with open('sequence') as f:
        sequence = f.read()
    sequence_levels = []
    for line in sequence.splitlines():
        if line.strip() and not line.startswith('#'):
            sequence_levels.append(line)

    for level in sequence_levels:
        assert level in all_levels, 'Unknown level listed in level sequence: %s' % level
    for level in all_levels:
        assert level in sequence_levels, 'Level missing from sequence: %s' % level