Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 64dc3d7f40
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 753 lines (675 sloc) 17.325 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
from __future__ import print_function

from nose.tools import assert_equal, assert_raises

import os
from textwrap import dedent
from StringIO import StringIO

from main import load_level, save_replay, load_replay
from move_shortcuts import *
from entity.polyomino import Piece
from entity.block import Block, HeavyBlock, SlideBlock

class MockDisplay(object):
    def show_bump(self):
        pass

def make_world(level_data):
    world = load_level(dedent(level_data))
    world.display = MockDisplay()
    return world

def show_world(world):
    lines = []
    height = max(y for x, y in world.terrain.keys()) + 1
    width = max(x for x, y in world.terrain.keys()) + 1
    for y in range(height):
        line = []
        for x in range(width):
            c = ' '
            pos = (x, y)
            c = world.terrain.get(pos, c)
            entity = world.entity_at(pos)
            if entity:
                c = entity.display_character
            line.append(c)
        lines.append(''.join(line))
    return '\n'.join(lines)

def test_load_level():
    world = make_world(
        '''
.$
#.
@_
__
''')
    assert_equal(world.level_completed, False)
    assert_equal(world.num_moves, 0)
    assert_equal(world.history, [])
    assert_equal(world.info_spaces, {})
    assert_equal(world.board_size, (2, 2))
    assert_equal(world.terrain, {(0, 0): '.', (1, 0): '$', (0, 1): '#', (1, 1): '.'})

    assert_equal(len(world.entities), 9) # The hero and 8 shield pieces.
    hero = world.hero
    assert_equal(hero.pos, (0, 0))
    assert_equal(len(hero.shield_pieces), 8)
    assert hero in world.entities

def test_load_level_info():
    world = make_world(
        '''
..$
.i.
#.i
@__
___
___

!i 1, 1
note one. with an exclamation! and stuff.

!i 2, 2
note two
''')
    assert_equal(world.board_size, (3, 3))
    assert_equal(world.terrain[(1, 1)], 'i')
    assert_equal(world.terrain[(2, 2)], 'i')
    assert_equal(
        world.info_spaces,
        {
            (1, 1): 'note one. with an exclamation! and stuff.',
            (2, 2): 'note two',
        },
    )

def test_load_level_whitespace():
    # Respect leading whitespace in game levels.
    # Ignore terrain in the entity layer.
    world = make_world(
        '''
####
##..#
#..##
####
####
##..#
#@.##
####
''')
    assert_equal(
        show_world(world),
        dedent(
            '''\
####
##..#
#@.##
#### ''')
    )

def test_load_level_illegal_terrain():
    # Don't allow undefined terrain types.
    assert_raises(KeyError, lambda: make_world(
        '''
K
@
'''))

def test_load_level_illegal_entities():
    # Don't allow undefined entity types.
    assert_raises(KeyError, lambda: make_world(
        '''
..
@K
'''))

def test_load_level_info_wrong_square():
    assert_raises(AssertionError, lambda: make_world(
        '''
.
@

!i 0, 0
this note is for a square with no info marker
'''))

def test_load_level_sanity():
    assert_raises(AssertionError, lambda: make_world(
        '''
#.
@_
'''))

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:
                replay = load_replay(f)
            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

def test_undo():
    world = make_world(
        '''
....$
@_O__
''')
    world.undo()
    assert_equal(
        show_world(world),
        '@.O.$',
    )
    world.update(('move', east))
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '..@O$',
    )
    world.undo()
    assert_equal(
        show_world(world),
        '.@O.$',
    )
    world.undo()
    world.undo() # Try to undo past the beginning.
    assert_equal(
        show_world(world),
        '@.O.$',
    )

def test_restart():
    world = make_world(
        '''
....$
@____
''')
    assert_equal(
        show_world(world),
        '@...$',
    )
    world.restart()
    assert_equal(
        show_world(world),
        '@...$',
    )
    world.update(('move', east))
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '..@.$',
    )
    world.restart()
    assert_equal(
        show_world(world),
        '@...$',
    )

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)
    assert_equal(
        f.buf,
        dedent(
            '''\
mn
ms
me
mw
mne
mse
msw
mnw
mc
sn
ss
se
sw
sne
sse
ssw
snw
sc
''')
    )
    new_replay = load_replay(f)
    assert_equal(replay, new_replay)

def test_move_command_goal():
    world = make_world(
        '''
..$
@__
''')
    assert_equal(world.hero.pos, (0, 0))
    world.update(('move', east))
    assert_equal(world.hero.pos, (1, 0))
    world.update(('move', east))
    assert_equal(world.hero.pos, (2, 0))
    assert_equal(world.level_completed, True)

def test_shield_bump():
    world = make_world(
        '''
....
...#
....
____
_@__
____
''')

    assert_equal(world.hero.pos, (1, 1))

    # Before opening the shield, he can move left and right.
    world.update(('move', west))
    assert_equal(world.hero.pos, (0, 1))
    world.update(('move', east))

    world.update(('move', east))
    assert_equal(world.hero.pos, (2, 1))
    world.update(('move', west))

    assert_equal(world.hero.pos, (1, 1))

    # After opening the shield right, he can move left, but not right.
    world.update(('shield', east))

    world.update(('move', west))
    assert_equal(world.hero.pos, (0, 1))
    world.update(('move', east))

    world.update(('move', east))
    assert_equal(world.hero.pos, (1, 1)) # Blocked on wall tile.

    # After opening the shield left, he can move right, but not left.
    world.update(('shield', west))

    world.update(('move', east))
    assert_equal(world.hero.pos, (2, 1))
    world.update(('move', west))

    world.update(('move', west))
    assert_equal(world.hero.pos, (1, 1)) # Blocked on level edge.

def test_close_shield():
    world = make_world(
        '''
...
...
...
___
_@_
___
''')

    # Nothing in the north square to start.
    assert_equal(world.entity_at((1, 0)), None)

    # Open the shield, the shield occupies the north square.
    world.update(('shield', north))
    piece = world.entity_at((1, 0))
    assert isinstance(piece, Piece)
    assert_equal(piece.polyomino, world.hero.polyomino)

    # Close the shield, the shield no longer occupies the north square.
    world.update(('shield', center))
    assert_equal(world.entity_at((1, 0)), None)

def test_open_shield_bump():
    world = make_world(
        '''
..
..
.#
@_
__
__
''')

    # Open the shield, it is blocked by the edge of the level.
    assert_equal(world.hero.pos, (0, 0))
    world.update(('shield', east))
    assert_equal(world.entity_at((1, 0)), None)

    # Move down.
    world.update(('move', south))
    assert_equal(world.hero.pos, (0, 1))

    # Open the shield, it is blocked by terrain.
    world.update(('shield', east))
    assert_equal(world.entity_at((1, 1)), None)

def test_block_push():
    world = make_world(
        '''
...
@O_
''')

    assert_equal(world.hero.pos, (0, 0))
    assert_equal(world.entity_at((0, 0)), world.hero)
    block = world.entity_at((1, 0))
    assert_equal(block.pos, (1, 0))

    # Push the block east.
    world.update(('move', east))

    assert_equal(world.hero.pos, (1, 0))
    assert_equal(world.entity_at((1, 0)), world.hero)
    assert_equal(block.pos, (2, 0))
    assert_equal(world.entity_at((2, 0)), block)

    # Try to push the block east, but it bumps the wall.
    world.update(('move', east))

    assert_equal(world.hero.pos, (1, 0))
    assert_equal(world.entity_at((1, 0)), world.hero)
    assert_equal(block.pos, (2, 0))
    assert_equal(world.entity_at((2, 0)), block)

def test_heavy_block_push():
    world = make_world(
        '''
....
....
....
....
....
....
@O__
_H__
_OO_
_HH_
_HO_
_OH_
''')

    # Push a single light block.
    assert_equal(world.hero.pos, (0, 0))

    world.update(('move', east))
    assert_equal(world.hero.pos, (1, 0))
    block = world.entity_at((2, 0))
    assert isinstance(block, Block)

    # Push a single heavy block.
    world.update(('move', west))
    world.update(('move', south))
    assert_equal(world.hero.pos, (0, 1))

    world.update(('move', east))
    assert_equal(world.hero.pos, (1, 1))
    block = world.entity_at((2, 1))
    assert isinstance(block, HeavyBlock)

    # Push two light blocks.
    world.update(('move', west))
    world.update(('move', south))
    assert_equal(world.hero.pos, (0, 2))

    world.update(('move', east))
    assert_equal(world.hero.pos, (1, 2))
    block = world.entity_at((3, 2))
    assert isinstance(block, Block)

    # Can't push two heavy blocks.
    world.update(('move', west))
    world.update(('move', south))
    assert_equal(world.hero.pos, (0, 3))

    world.update(('move', east))
    assert_equal(world.hero.pos, (0, 3))
    assert_equal(world.entity_at((3, 3)), None)

    # Can't push a heavy block and a light block.
    world.update(('move', west))
    world.update(('move', south))
    assert_equal(world.hero.pos, (0, 4))

    world.update(('move', east))
    assert_equal(world.hero.pos, (0, 4))
    assert_equal(world.entity_at((3, 4)), None)

    world.update(('move', west))
    world.update(('move', south))
    assert_equal(world.hero.pos, (0, 5))

    world.update(('move', east))
    assert_equal(world.hero.pos, (0, 5))
    assert_equal(world.entity_at((3, 5)), None)

def test_slide_block():
    # A slide block slides until it hits something when it is pushed.
    world = make_world(
        '''
.....
@S___
''')
    block = world.entity_at((1, 0))
    assert isinstance(block, SlideBlock)
    assert_equal(
        show_world(world),
        '@S...',
    )
    world.update(('move', east))
    assert_equal(block.pos, (4, 0))
    assert_equal(
        show_world(world),
        '.@..S',
    )

def test_slide_block_push():
    # A slide block does not push other blocks during its slide.
    world = make_world(
        '''
......
@S__O_
''')
    assert_equal(
        show_world(world),
        '@S..O.',
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '.@.SO.',
    )

def test_slide_block_transmit_push():
    # A slide block does push other blocks before it starts sliding.
    world = make_world(
        '''
....
@SO_
''')
    assert_equal(
        show_world(world),
        '@SO.',
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '.@SO',
    )

def test_slide_block_get_pushed():
    # A slide block will continue when pushed by another block.
    world = make_world(
        '''
......
@OS___
''')
    assert_equal(
        show_world(world),
        '@OS...',
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '.@O..S',
    )

def test_slide_block_water():
    # A slide block reacts normally when pushed into water.
    world = make_world(
        '''
..~
@S_
''')
    block = world.entity_at((1, 0))
    assert_equal(
        show_world(world),
        '@S~',
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '.@.',
    )
    assert_equal(block.pos, (2, 0))
    assert_equal(block.solid, False)

def test_slide_block_water():
    # A slide block reacts normally when sliding into water.
    world = make_world(
        '''
...~
@S__
''')
    assert_equal(
        show_world(world),
        '@S.~',
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '.@..',
    )

def test_slide_carom():
    # A slide block will transfer momentum to another slide block.
    world = make_world(
        '''
......
@S__S_
''')
    assert_equal(
        show_world(world),
        '@S..S.',
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '.@.S.S',
    )

def test_slide_carom_multiple():
    # A slide block will transfer momentum to another slide block.
    world = make_world(
        '''
........
@S__SS__
''')
    assert_equal(
        show_world(world),
        '@S..SS..',
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '.@.SS..S',
    )

def test_slide_block_push_multiple_left():
    # Regression test: Push a slide block in a row with a normal block twice.
    world = make_world(
        '''
.....
@SO__
''')
    assert_equal(
        show_world(world),
        '@SO..',
    )
    world.update(('move', east))
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '..@SO',
    )

def test_slide_block_bump():
    # Push a slide block into a wall.
    world = make_world(
        '''
..#
@S_
''')
    assert_equal(
        show_world(world),
        '@S#',
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        '@S#',
    )

def test_polyomino_push_loop():
    world = make_world(
        '''
....
....
....
AA_O
@___
____
''')
    assert_equal(
        show_world(world),
        dedent(
            '''\
AA.O
@...
....''')
    )
    world.update(('shield', east))
    assert_equal(
        show_world(world),
        dedent(
            '''\
AA.O
@...
....''')
    )

    world = make_world(
        '''
.....
.....
.....
_____
A@A_O
_____
''')
    assert_equal(
        show_world(world),
        dedent(
            '''\
.....
A@A.O
.....''')
    )
    world.update(('shield', east))
    assert_equal(
        show_world(world),
        dedent(
            '''\
.....
A@A.O
.....''')
    )

def test_polyomino_water():
    world = make_world(
        '''
..~
..~
@A_
_A_
''')
    assert_equal(
        show_world(world),
        dedent(
            '''\
@A~
.A~''')
    )
    world.update(('move', east))
    assert_equal(
        show_world(world),
        dedent(
            '''\
.@.
...''')
    )
Something went wrong with that request. Please try again.