def test_valid_moves():
    field = BottomField(1)
    for top in range(1 << 16):
        field.data[0] = top & 255
        field.data[1] = top >> 8

        assert (_reference_valid_moves(field.data) == field._valid_moves())
def test_render_in_place():
    field = BottomField(2)
    for i in range(16):
        field.data[i] = ((i + 4234)**5) % 256

    for i in range(8):
        field.data[i] &= ~field.data[i + 8]

    for i in range(10):
        print(i)
    util.print_up(8)
    print("Let's shift this a bit!", end="")
    field.render(in_place=True)
    print("hello")
    field.render(width=6, height=7)
Exemple #3
0
    def __init__(self,
                 height,
                 width,
                 num_layers,
                 num_deals=None,
                 tsu_rules=False,
                 has_garbage=False,
                 deals=None,
                 seed=None):
        if height not in ALLOWED_HEIGHTS:
            raise NotImplementedError(
                "Only heights {} supported".format(ALLOWED_HEIGHTS))
        if width > BottomField.WIDTH:
            raise NotImplementedError("Maximum width is {}".format(
                BottomField.WIDTH))
        if height == 13 and not tsu_rules:
            raise NotImplementedError(
                "Height 13 only available with tsu ruleset")
        if tsu_rules and not height == 13:
            raise NotImplementedError(
                "Tsu ruleset available only for height 13")
        if num_deals and deals:
            raise ValueError("Cannot use a partial number of fixed deals")

        if height == BottomField.HEIGHT:
            self.field = BottomField(num_layers, has_garbage=has_garbage)
        else:
            self.field = TallField(num_layers,
                                   tsu_rules=tsu_rules,
                                   has_garbage=has_garbage)
        self.width = width
        self.height = height
        self.num_deals = num_deals
        self.garbage_x = 0 if has_garbage else None
        self.make_actions()
        self.seed(seed)
        if deals is None:
            self.make_deals()
        else:
            self.deals = list(deals)
def test_gravity():
    _ = None
    stack = [_] * 8 * 6
    stack += [
        1, 2, _, 1, _, _, _, _,
        3, _, _, _, _, _, _, _,
    ]

    state = State(8, 4, 4, deals=[])
    state.field = BottomField.from_list(stack)

    state = AnimationState(state)

    assert state.step_gravity()

    stack = [_] * 4 * 6
    stack += [
        1, _, _, _,
        3, 2, _, 1,
    ]

    assert state.to_list() == stack

    assert not state.step_gravity()
Exemple #5
0
def test_action_mask():
    state = State(8, 7, 2, 1)
    stack = [
        R,
        _,
        R,
        _,
        _,
        _,
        G,
        _,
    ]
    state.field = BottomField.from_list(stack, num_layers=state.num_layers)
    state.render()
    mask = state.get_action_mask()
    print(mask)
    assert (len(mask) == 6 + 6 + 7 + 7)
    for i, (x, orientation) in enumerate(state.actions):
        if x in (3, 4):
            assert (mask[i])
        elif orientation in (1, 3) and x in (1, 5):
            assert (mask[i])
        else:
            assert (not mask[i])
def test_too_big_from_list():
    bad_stack = [R, G, B] * 80
    with pytest.raises(ValueError):
        BottomField.from_list(bad_stack)
Exemple #7
0
def test_resolve():
    state = State(8, 7, 2, 1)
    state.deals[0] = (0, 0)
    stack = [
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        G,
        G,
        _,
        _,
        _,
        _,
        _,
        R,
        R,
        R,
        G,
        G,
        G,
        _,
    ]
    state.field = BottomField.from_list(stack, num_layers=state.num_layers)
    state.render()
    reward = state.step(0, 1)
    state.render()
    assert (reward == 4)
Exemple #8
0
def test_no_moves():
    state = State(8, 2, 4, 1)
    stack = [
        _,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        B,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        B,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        B,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        B,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        B,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
    ]
    state.field = BottomField.from_list(stack, num_layers=state.num_layers)
    state.render()
    assert (not state.get_children())
def test_positive_shift():
    stack = [
        R,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        R,
        _,
        _,
        _,
        _,
    ]
    field = BottomField.from_list(stack)
    field.render()
    print()
    field.shift(1)
    field.render()
    stack = field.to_list()
    assert (stack == [
        _,
        R,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        R,
        _,
        _,
        _,
    ])
def test_gravity():
    stack = [
        R,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
    ]
    field = BottomField.from_list(stack)
    field.render()
    print()
    field.handle_gravity()
    field.render()
    stack = field.to_list()
    assert (stack == [
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
    ])
def test_mirror():
    stack = [
        R,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        R,
    ]
    field = BottomField.from_list(stack)
    field.render()
    print()
    field.mirror()
    field.render()
    stack = field.to_list()
    assert (stack == [
        _,
        _,
        _,
        _,
        _,
        _,
        R,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
    ])
def test_uneven_from_list():
    bad_stack = [R, G, B]
    with pytest.raises(ValueError):
        BottomField.from_list(bad_stack)
def test_clear_groups():
    O = Y + 1  # noqa
    stack = [
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        G,
        _,
        _,
        _,
        _,
        O,
        _,
        G,
        Y,
        _,
        _,
        R,
        R,
        O,
        _,
        G,
        Y,
        _,
        _,
        R,
        R,
        O,
        _,
        G,
        Y,
        _,
        _,
    ]
    field = BottomField.from_list(stack, has_garbage=True)
    field.render()
    print()
    score = field.clear_groups(2)
    field.render()
    stack = field.to_list()
    assert (score == 9)
    assert (stack == [
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        O,
        _,
        _,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        _,
        _,
    ])
def test_encode():
    stack = [
        _,
        R,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        R,
    ]
    field = BottomField.from_list(stack)
    field.render()
    encoded = field.encode()
    expected = [
        [
            [0, 1, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 1],
        ],
        [
            [0, 0, 0, 0, 0, 0, 0, 0],
            [1, 0, 1, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
        ],
    ]
    assert (encoded == expected).all()
def test_overlapping_overlay():
    field = BottomField(4)
    field.overlay([R, B, _, _, _, _, _, _])
    field.render()
    print()
    field.overlay([G, Y, G, _, _, _, _, _])
    field.render()
    stack = field.to_list()
    assert (stack[:8] == [R, B, G, _, _, _, _, _])
def test_overlay():
    field = BottomField(3)
    field.overlay([R, G, _, _, _, _, _, _])
    field.handle_gravity()
    field.render()
    print()
    field.overlay([
        _,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
    ])
    field.handle_gravity()
    field.render()
    stack = field.to_list()
    assert (stack == [
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
        R,
        G,
        _,
        _,
        _,
        _,
        _,
        _,
    ])
def test_resolve_garbage():
    O = Y + 1  # noqa
    stack = [
        R,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
        O,
        O,
        O,
        _,
        _,
        _,
        _,
        R,
        G,
        B,
        O,
        O,
        _,
        _,
        _,
        R,
        R,
        G,
        B,
        O,
        O,
        _,
        _,
        G,
        G,
        B,
        B,
        O,
        O,
        O,
        _,
    ]
    field = BottomField.from_list(stack, has_garbage=True)
    field.render()
    print()
    chain = field.resolve()[1]
    field.render()
    stack = field.to_list()
    assert (stack == [
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        O,
        O,
        _,
        _,
        _,
        _,
        _,
        Y,
        O,
        O,
        O,
        _,
        _,
        _,
        Y,
        O,
        O,
        O,
        O,
        O,
        _,
    ])
    assert (chain == 2)
def test_resolve():
    stack = [
        R,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        R,
        G,
        B,
        _,
        _,
        _,
        _,
        _,
        R,
        R,
        G,
        B,
        _,
        _,
        _,
        _,
        G,
        G,
        B,
        B,
        _,
        _,
        _,
        _,
    ]
    field = BottomField.from_list(stack)
    field.render()
    print()
    chain = field.resolve()[1]
    field.render()
    stack = field.to_list()
    assert (stack == [
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        _,
        Y,
        _,
        _,
        _,
        _,
        _,
        _,
    ])
    assert (chain == 3)