Ejemplo n.º 1
0
def _verify_apply_gravity(board, exp_board_s, exp_moved):
    post_g_board, moved = apply_gravity(board)
    eq_(re.sub('\s', '', exp_board_s),
        re.sub('\s', '', unparse_board(post_g_board)))
    eq_(sorted(exp_moved), moved)

    # verify that gravity is idempotent
    post_gg_board, gg_moved = apply_gravity(post_g_board)
    eq_(re.sub('\s', '', unparse_board(post_g_board)),
        re.sub('\s', '', unparse_board(post_gg_board)))
    eq_([], gg_moved)
Ejemplo n.º 2
0
def _verify_apply_gravity(board, exp_board_s, exp_moved):
    post_g_board, moved = apply_gravity(board)
    eq_(re.sub('\s', '', exp_board_s),
        re.sub('\s', '', unparse_board(post_g_board)))
    eq_(sorted(exp_moved), moved)

    # verify that gravity is idempotent
    post_gg_board, gg_moved = apply_gravity(post_g_board)
    eq_(re.sub('\s', '', unparse_board(post_g_board)),
        re.sub('\s', '', unparse_board(post_gg_board)))
    eq_([], gg_moved)
Ejemplo n.º 3
0
def rand_stable_board(board_side=BOARD_SIDE, no_teamups=False):
    """
    Generate a random stable board (meaning with no current matches) of side
    `board_side`.

    The board will have at least one legal move.

    Throws an exception if it can't randomly and stably fill a given square in
    MAX_TRIES tries, or can't generate a board with at least one move after
    MAX_TRIES tries.
    """
    board_tries = 0
    while True:
        board_tries += 1
        if board_tries >= MAX_TRIES:
            raise Exception("No moves on stable board after %d tries" %
                            MAX_TRIES)

        board = empty_board(board_side)
        for (row, col) in board.squares_from_bottom_right():
            sq_tries = 0
            while True:
                sq_tries += 1
                if sq_tries >= MAX_TRIES:
                    raise Exception(
                        "Unstable cell (%d, %d) after %d tries %s" %
                        (row, col, MAX_TRIES, unparse_board(board)))
                board.set_at(row, col, new_rand_tile(no_teamups=no_teamups))

                if not find_matches_at(row, col, board):
                    break
        if find_moves(board, stop_after=1):
            return board
Ejemplo n.º 4
0
def print_board(game_state):
    print
    print >> sys.stderr, '***'
    print >> sys.stderr, unparse_board(game_state.board)
    print >> sys.stderr, game_state.board.md5()
    print >> sys.stderr, '***'
    print
Ejemplo n.º 5
0
def print_board(game_state):
    print
    print >> sys.stderr, '***'
    print >> sys.stderr, unparse_board(game_state.board)
    print >> sys.stderr, game_state.board.md5()
    print >> sys.stderr, '***'
    print
Ejemplo n.º 6
0
def rand_stable_board(board_side=BOARD_SIDE, no_teamups=False):
    """
    Generate a random stable board (meaning with no current matches) of side
    `board_side`.

    The board will have at least one legal move.

    Throws an exception if it can't randomly and stably fill a given square in
    MAX_TRIES tries, or can't generate a board with at least one move after
    MAX_TRIES tries.
    """
    board_tries = 0
    while True:
        board_tries += 1
        if board_tries >= MAX_TRIES:
            raise Exception(
                "No moves on stable board after %d tries" % MAX_TRIES)

        board = empty_board(board_side)
        for (row, col) in board.squares_from_bottom_right():
            sq_tries = 0
            while True:
                sq_tries += 1
                if sq_tries >= MAX_TRIES:
                    raise Exception(
                        "Unstable cell (%d, %d) after %d tries %s" %
                        (row, col, MAX_TRIES, unparse_board(board)))
                board.set_at(row, col, new_rand_tile(no_teamups=no_teamups))

                if not find_matches_at(row, col, board):
                    break
        if find_moves(board, stop_after=1):
            return board
Ejemplo n.º 7
0
def test_nulls():
    board_s = dedent("""\
                     |   | R | R |
                     | Y |   | G |
                     | Y | R |   |
                     """)
    parser = create_board_parser(side=3)
    eq_(re.sub('\s', '', board_s),
        re.sub('\s', '', unparse_board(parse_board(board_s, parser))))
Ejemplo n.º 8
0
def test_ensure_playable_board():
    rand_state = random.getstate()
    try:
        random.seed(100)
        board_s = dedent("""\
                         | Y | G | BL | P |
                         | R | Y | BL | R |
                         | G | P | BK | G |
                         | Y | G | BK | P |
                         """)
        board = parse_board(board_s, FOUR_SIDE_PARSER)
        offense = TestPlayer(strategy=first_move_strat)
        defense = TestPlayer(strategy=first_move_strat)
        game = Game(board=board,
                    offense=offense,
                    defense=defense,
                    stop_condition=_allow_one_move())

        game.play()
        eq_(
            re.sub(
                '\s', '',
                dedent("""\
                          | P   | G   | G   | R   |
                          | BK  | BK  | BL  | BL  |
                          | R   | P   | G   | P   |
                          | Y   | Y   | P   | Y   |
                          """)), re.sub('\s', '', unparse_board(game.board)))
        # it takes 3 shuffles with this seed to get a playable board with no
        # matches
        expected_swaps = [
            (3, 3, 0, 0), (3, 2, 2, 2), (3, 1, 3, 2), (3, 0, 0, 1), (2, 3, 0,
                                                                     3),
            (2, 2, 2, 0), (2, 1, 3, 0), (2, 0, 3, 3), (1, 3, 0, 2),
            (1, 2, 1, 0), (1, 1, 2, 3), (1, 0, 1, 3), (0, 3, 1, 2),
            (0, 2, 1, 1), (0, 1, 2, 1), (0, 0, 3, 1), (3, 3, 0, 0), (3, 2, 1,
                                                                     1),
            (3, 1, 1, 3), (3, 0, 3, 3), (2, 3, 1, 0), (2, 2, 2, 2), (2, 1, 0,
                                                                     1),
            (2, 0, 2, 0), (1, 3, 3, 2), (1, 2, 1, 2), (1, 1, 2, 1), (1, 0, 2,
                                                                     3),
            (0, 3, 0, 2), (0, 2, 3, 1), (0, 1, 3, 0), (0, 0, 0, 3), (3, 3, 2,
                                                                     1),
            (3, 2, 3, 2), (3, 1, 0, 3), (3, 0, 1, 3), (2, 3, 0, 0), (2, 2, 0,
                                                                     1),
            (2, 1, 2, 3), (2, 0, 3, 0), (1, 3, 1, 0), (1, 2, 0, 2), (1, 1, 1,
                                                                     2),
            (1, 0, 3, 1), (0, 3, 2, 0), (0, 2, 1, 1), (0, 1, 2, 2),
            (0, 0, 3, 3), (0, 0, 1, 0)
        ]
        eq_(expected_swaps, offense.update_tiles_swapped_calls)
        eq_(expected_swaps, defense.update_tiles_swapped_calls)
    finally:
        random.setstate(rand_state)
Ejemplo n.º 9
0
def test_one_turn_game():
    rand_state = random.getstate()
    try:
        random.seed(100)
        board_s = dedent("""\
                         | Y | G | BL | P |
                         | Y | R | BL | R |
                         | G | G | BK | G |
                         | Y | G | BL | P |
                         """)
        board = parse_board(board_s, FOUR_SIDE_PARSER)
        offense = TestPlayer(strategy=first_move_strat)
        defense = TestPlayer(strategy=first_move_strat)
        game = Game(board=board,
                    offense=offense,
                    defense=defense,
                    stop_condition=_allow_one_move())

        game.play()

        eq_(1, game.move_count)
        eq_(1, game.turn_count)
        eq_(1, offense.moves)
        eq_(0, defense.moves)
        eq_([(0, 1, 1, 1)],
            offense.update_tiles_swapped_calls)
        eq_([(0, 1, 1, 1)],
            defense.update_tiles_swapped_calls)
        eq_([(0, 1, 3, 1)],
            offense.update_tile_position_calls)
        eq_([(0, 1, 3, 1)],
            defense.update_tile_position_calls)
        eq_([(1, 1, True),
             (2, 1, True),
             (3, 1, True)],
            offense.update_destroyed_tile_calls)
        eq_([(1, 1, False),
             (2, 1, False),
             (3, 1, False)],
            defense.update_destroyed_tile_calls)
        eq_(dict(G=3), offense.ap)
        eq_(dict(), defense.ap)

        eq_(re.sub('\s', '',
                   dedent("""\
                          | Y | G  | BL | P |
                          | Y | P  | BL | R |
                          | G | BL | BK | G |
                          | Y | R  | BL | P |
                          """)),
            re.sub('\s', '', unparse_board(game.board)))

        game.stop_condition = _allow_one_move()
        offense.reset_calls()
        defense.reset_calls()

        game.play()

        eq_(2, game.move_count)
        eq_(2, game.turn_count)
        eq_(1, offense.moves)
        eq_(1, defense.moves)
        eq_([(2, 0, 3, 0)],
            offense.update_tiles_swapped_calls)
        eq_([(2, 0, 3, 0)],
            defense.update_tiles_swapped_calls)
        eq_([],
            offense.update_tile_position_calls)
        eq_([],
            defense.update_tile_position_calls)
        eq_([(0, 0, False),
             (1, 0, False),
             (2, 0, False)],
            offense.update_destroyed_tile_calls)
        eq_([(0, 0, True),
             (1, 0, True),
             (2, 0, True)],
            defense.update_destroyed_tile_calls)
        eq_(dict(G=3), offense.ap)
        eq_(dict(Y=3), defense.ap)

        eq_(re.sub('\s', '',
                   dedent("""\
                          | P | G  | BL | P |
                          | G | P  | BL | R |
                          | R | BL | BK | G |
                          | G | R  | BL | P |
                          """)),
            re.sub('\s', '', unparse_board(game.board)))
    finally:
        # be friendly to other downstream tests that actually want some
        # randomness
        random.setstate(rand_state)
Ejemplo n.º 10
0
def test_ensure_playable_board():
    rand_state = random.getstate()
    try:
        random.seed(100)
        board_s = dedent("""\
                         | Y | G | BL | P |
                         | R | Y | BL | R |
                         | G | P | BK | G |
                         | Y | G | BK | P |
                         """)
        board = parse_board(board_s, FOUR_SIDE_PARSER)
        offense = TestPlayer(strategy=first_move_strat)
        defense = TestPlayer(strategy=first_move_strat)
        game = Game(board=board,
                    offense=offense,
                    defense=defense,
                    stop_condition=_allow_one_move())

        game.play()
        eq_(re.sub('\s', '',
                   dedent("""\
                          | P   | G   | G   | R   |
                          | BK  | BK  | BL  | BL  |
                          | R   | P   | G   | P   |
                          | Y   | Y   | P   | Y   |
                          """)),
            re.sub('\s', '', unparse_board(game.board)))
        # it takes 3 shuffles with this seed to get a playable board with no
        # matches
        expected_swaps = [(3, 3, 0, 0),
                          (3, 2, 2, 2),
                          (3, 1, 3, 2),
                          (3, 0, 0, 1),
                          (2, 3, 0, 3),
                          (2, 2, 2, 0),
                          (2, 1, 3, 0),
                          (2, 0, 3, 3),
                          (1, 3, 0, 2),
                          (1, 2, 1, 0),
                          (1, 1, 2, 3),
                          (1, 0, 1, 3),
                          (0, 3, 1, 2),
                          (0, 2, 1, 1),
                          (0, 1, 2, 1),
                          (0, 0, 3, 1),

                          (3, 3, 0, 0),
                          (3, 2, 1, 1),
                          (3, 1, 1, 3),
                          (3, 0, 3, 3),
                          (2, 3, 1, 0),
                          (2, 2, 2, 2),
                          (2, 1, 0, 1),
                          (2, 0, 2, 0),
                          (1, 3, 3, 2),
                          (1, 2, 1, 2),
                          (1, 1, 2, 1),
                          (1, 0, 2, 3),
                          (0, 3, 0, 2),
                          (0, 2, 3, 1),
                          (0, 1, 3, 0),
                          (0, 0, 0, 3),

                          (3, 3, 2, 1),
                          (3, 2, 3, 2),
                          (3, 1, 0, 3),
                          (3, 0, 1, 3),
                          (2, 3, 0, 0),
                          (2, 2, 0, 1),
                          (2, 1, 2, 3),
                          (2, 0, 3, 0),
                          (1, 3, 1, 0),
                          (1, 2, 0, 2),
                          (1, 1, 1, 2),
                          (1, 0, 3, 1),
                          (0, 3, 2, 0),
                          (0, 2, 1, 1),
                          (0, 1, 2, 2),
                          (0, 0, 3, 3),
                          (0, 0, 1, 0)]
        eq_(expected_swaps, offense.update_tiles_swapped_calls)
        eq_(expected_swaps, defense.update_tiles_swapped_calls)
    finally:
        random.setstate(rand_state)
Ejemplo n.º 11
0
def test_one_turn_game():
    rand_state = random.getstate()
    try:
        random.seed(100)
        board_s = dedent("""\
                         | Y | G | BL | P |
                         | Y | R | BL | R |
                         | G | G | BK | G |
                         | Y | G | BL | P |
                         """)
        board = parse_board(board_s, FOUR_SIDE_PARSER)
        offense = TestPlayer(strategy=first_move_strat)
        defense = TestPlayer(strategy=first_move_strat)
        game = Game(board=board,
                    offense=offense,
                    defense=defense,
                    stop_condition=_allow_one_move())

        game.play()

        eq_(1, game.move_count)
        eq_(1, game.turn_count)
        eq_(1, offense.moves)
        eq_(0, defense.moves)
        eq_([(0, 1, 1, 1)], offense.update_tiles_swapped_calls)
        eq_([(0, 1, 1, 1)], defense.update_tiles_swapped_calls)
        eq_([(0, 1, 3, 1)], offense.update_tile_position_calls)
        eq_([(0, 1, 3, 1)], defense.update_tile_position_calls)
        eq_([(1, 1, True), (2, 1, True), (3, 1, True)],
            offense.update_destroyed_tile_calls)
        eq_([(1, 1, False), (2, 1, False), (3, 1, False)],
            defense.update_destroyed_tile_calls)
        eq_(dict(G=3), offense.ap)
        eq_(dict(), defense.ap)

        eq_(
            re.sub(
                '\s', '',
                dedent("""\
                          | Y | G  | BL | P |
                          | Y | P  | BL | R |
                          | G | BL | BK | G |
                          | Y | R  | BL | P |
                          """)), re.sub('\s', '', unparse_board(game.board)))

        game.stop_condition = _allow_one_move()
        offense.reset_calls()
        defense.reset_calls()

        game.play()

        eq_(2, game.move_count)
        eq_(2, game.turn_count)
        eq_(1, offense.moves)
        eq_(1, defense.moves)
        eq_([(2, 0, 3, 0)], offense.update_tiles_swapped_calls)
        eq_([(2, 0, 3, 0)], defense.update_tiles_swapped_calls)
        eq_([], offense.update_tile_position_calls)
        eq_([], defense.update_tile_position_calls)
        eq_([(0, 0, False), (1, 0, False), (2, 0, False)],
            offense.update_destroyed_tile_calls)
        eq_([(0, 0, True), (1, 0, True), (2, 0, True)],
            defense.update_destroyed_tile_calls)
        eq_(dict(G=3), offense.ap)
        eq_(dict(Y=3), defense.ap)

        eq_(
            re.sub(
                '\s', '',
                dedent("""\
                          | P | G  | BL | P |
                          | G | P  | BL | R |
                          | R | BL | BK | G |
                          | G | R  | BL | P |
                          """)), re.sub('\s', '', unparse_board(game.board)))
    finally:
        # be friendly to other downstream tests that actually want some
        # randomness
        random.setstate(rand_state)
Ejemplo n.º 12
0
def _verify_destroy_tiles(board_s, parser, e_new_board_s, e_destroyed):
    board = parse_board(board_s, parser)
    new_board, destroyed = destroy_tiles(board)
    eq_(sorted(list(set(e_destroyed))), destroyed)
    eq_(re.sub('\s', '', e_new_board_s),
        re.sub('\s', '', unparse_board(new_board)))
Ejemplo n.º 13
0
def _verify_parse_and_unparse(board_s):
    eq_(board_s, unparse_board(parse_board(board_s)))