Exemple #1
0
def test_bad_coordinate():
    box = generate_empty_box(1, 2, 2)
    coordinate = PanelCoordinate(
        level=0, y=2, x=1, panel=TOP,
    )
    with pytest.raises(IndexError):
        is_edge(coordinate, box)
Exemple #2
0
def generate_box(levels, depth, width):
    box = generate_empty_box(levels, depth, width)
    level = 0 if levels == 1 else 1
    entrance_x = 0 if width == 1 else 1
    entrance_y = depth - 1

    entrance = PanelCoordinate(level, entrance_y, entrance_x, 'back')

    entrance_color = get_coordinate_color(entrance)

    for x in range(width, -1, -1):
        coord = PanelCoordinate(level, 0, x, 'front')
        coord_color = get_coordinate_color(coord)
        if get_cell_count(box) % 2 and entrance_color != coord_color:
            continue
        elif not get_cell_count(box) % 2 and entrance_color == coord_color:
            continue
        else:
            _exit = coord
            break
    else:
        raise ValueError("Couldn't find an exit panel")

    path = generate_path(entrance, _exit, box)
    return apply_path_to_box(entrance, path, box)
def test_generation():
    box = generate_empty_box(2, 3, 4)
    assert len(box) == 2
    assert all(len(layer) == 3 for layer in box)
    assert all(all(len(row) == 4 for row in layer) for layer in box)
    assert all(
        all(all(not any(cell) for cell in row) for row in layer)
        for layer in box)
Exemple #4
0
def find_paths():
    entrance = PanelCoordinate(1, 3, 1, 'back')
    _exit = PanelCoordinate(1, 0, 1, 'front')

    while True:
        path = generate_path(
            entrance,
            _exit,
            generate_empty_box(4, 4, 4),
            tracker_getter=get_learning_tracker,
            get_choices_callback=get_learning_move_choices,
        )
        filename = path_to_file_path(path)
        if os.path.exists(filename):
            continue
        print "writing: {0}".format(filename)
        box = apply_path_to_box(entrance, path, generate_empty_box(4, 4, 4))
        export_box(filename, box)
Exemple #5
0
def test_bad_exit():
    box = generate_empty_box(2, 2, 2)
    entrance = PanelCoordinate(level=0, y=0, x=0, panel=LEFT)
    exit = PanelCoordinate(level=1, y=1, x=1, panel=LEFT)

    with pytest.raises(ValueError) as err:
        generate_path(entrance, exit, box)

    assert err.value.message == INVALID_EXIT
Exemple #6
0
def test_small_two_by_two_box():
    box = generate_empty_box(1, 2, 2)
    entrance = PanelCoordinate(0, 0, 0, LEFT)
    _exit = PanelCoordinate(0, 1, 0, LEFT)

    path = generate_path(
        entrance=entrance,
        _exit=_exit,
        box=box,
    )
    assert path == (RIGHT, BACK, LEFT, LEFT)
Exemple #7
0
def test_impossible_entrance_exit_combo_for_odd_box():
    box = generate_empty_box(3, 3, 3)
    entrance = PanelCoordinate(level=0, y=0, x=0, panel=LEFT)
    exit = PanelCoordinate(level=0, y=1, x=0, panel=LEFT)

    # sainity check
    assert get_coordinate_color(entrance) != get_coordinate_color(exit)

    with pytest.raises(ValueError) as err:
        generate_path(entrance, exit, box)

    assert err.value.message == IMPOSSIBLE_TO_FILL_SPACE
Exemple #8
0
def test_get_neighbors():
    box = generate_empty_box(2, 2, 2)

    left, right, top, bottom, front, back = get_neighbors(
        CellCoordinate(level=0, y=0, x=0),
        box,
    )
    assert not left
    assert right
    assert top
    assert not bottom
    assert not front
    assert back
Exemple #9
0
def test_three_by_three_box():
    solutions = (
        ('right', 'right', 'back', 'left', 'left', 'back', 'right', 'right',
         'right'),
        ('back', 'back', 'right', 'front', 'front', 'right', 'back', 'back',
         'right'),
    )
    box = generate_empty_box(1, 3, 3)
    entrance = PanelCoordinate(0, 0, 0, LEFT)
    _exit = PanelCoordinate(0, 2, 2, RIGHT)

    path = generate_path(
        entrance=entrance,
        _exit=_exit,
        box=box,
    )
    assert path in solutions
def test_apply_path_to_box():
    path = (RIGHT, BACK, LEFT, LEFT)


    box = apply_path_to_box(
        entrance=PanelCoordinate(0, 0, 0, LEFT),
        path=path,
        box=generate_empty_box(1, 2, 2),
    )

    front_left, front_right = box[0][0]
    back_left, back_right = box[0][1]

    assert front_left.left == IN
    assert front_left.right == OUT

    assert front_right.left == IN
    assert front_right.back == OUT

    assert back_right.front == IN
    assert back_right.left == OUT

    assert back_left.right == IN
    assert back_left.left == OUT
Exemple #11
0
def test_good_entrance_and_exit():
    box = generate_empty_box(2, 2, 2)
    entrance = PanelCoordinate(level=0, y=0, x=0, panel=LEFT)
    exit = PanelCoordinate(level=1, y=1, x=1, panel=RIGHT)

    generate_path(entrance, exit, box)
Exemple #12
0
def test_with_non_edges():
    box = generate_empty_box(1, 2, 2)

    #  ------
    #  | || |
    #  -x----
    #  ------
    #  | || |
    #  ------
    assert not is_edge(PanelCoordinate(
        level=0, y=0, x=0, panel=BACK,
    ), box)

    #  ------
    #  | x| |
    #  ------
    #  ------
    #  | || |
    #  ------
    assert not is_edge(PanelCoordinate(
        level=0, y=0, x=0, panel=RIGHT,
    ), box)

    #  ------
    #  | |x |
    #  ------
    #  ------
    #  | || |
    #  ------
    assert not is_edge(PanelCoordinate(
        level=0, y=0, x=1, panel=LEFT,
    ), box)

    #  ------
    #  | || |
    #  ----x-
    #  ------
    #  | || |
    #  ------
    assert not is_edge(PanelCoordinate(
        level=0, y=0, x=1, panel=BACK,
    ), box)

    #  ------
    #  | || |
    #  ------
    #  -x----
    #  | || |
    #  ------
    assert not is_edge(PanelCoordinate(
        level=0, y=1, x=0, panel=FRONT,
    ), box)

    #  ------
    #  | || |
    #  ------
    #  ------
    #  | x| |
    #  ------
    assert not is_edge(PanelCoordinate(
        level=0, y=1, x=0, panel=RIGHT,
    ), box)

    #  ------
    #  | || |
    #  ------
    #  ------
    #  | |x |
    #  ------
    assert not is_edge(PanelCoordinate(
        level=0, y=1, x=1, panel=LEFT,
    ), box)

    #  ------
    #  | || |
    #  ------
    #  ----x-
    #  | || |
    #  ------
    assert not is_edge(PanelCoordinate(
        level=0, y=1, x=1, panel=FRONT,
    ), box)
Exemple #13
0
def test_with_edges():
    box = generate_empty_box(1, 2, 2)

    #  -x----
    #  | || |
    #  ------
    #  ------
    #  | || |
    #  ------
    assert is_edge(PanelCoordinate(
        level=0, y=0, x=0, panel=FRONT,
    ), box)

    #  ------
    #  x || |
    #  ------
    #  ------
    #  | || |
    #  ------
    assert is_edge(PanelCoordinate(
        level=0, y=0, x=0, panel=LEFT,
    ), box)

    # TOP and BOTTOM
    assert is_edge(PanelCoordinate(
        level=0, y=0, x=0, panel=TOP,
    ), box)
    assert is_edge(PanelCoordinate(
        level=0, y=0, x=0, panel=BOTTOM,
    ), box)

    #  ------
    #  | || x
    #  ------
    #  ------
    #  | || |
    #  ------
    assert is_edge(PanelCoordinate(
        level=0, y=0, x=1, panel=RIGHT,
    ), box)

    #  ----x-
    #  | || |
    #  ------
    #  ------
    #  | || |
    #  ------
    assert is_edge(PanelCoordinate(
        level=0, y=0, x=1, panel=FRONT,
    ), box)

    # TOP and BOTTOM
    assert is_edge(PanelCoordinate(
        level=0, y=0, x=1, panel=TOP,
    ), box)
    assert is_edge(PanelCoordinate(
        level=0, y=0, x=1, panel=BOTTOM,
    ), box)


    #  ------
    #  | || |
    #  ------
    #  ------
    #  | || |
    #  -x----
    assert is_edge(PanelCoordinate(
        level=0, y=1, x=0, panel=BACK,
    ), box)

    #  ------
    #  | || |
    #  ------
    #  ------
    #  x || |
    #  ------
    assert is_edge(PanelCoordinate(
        level=0, y=1, x=0, panel=LEFT,
    ), box)

    # TOP and BOTTOM
    assert is_edge(PanelCoordinate(
        level=0, y=1, x=0, panel=TOP,
    ), box)
    assert is_edge(PanelCoordinate(
        level=0, y=1, x=0, panel=BOTTOM,
    ), box)

    #  ------
    #  | || |
    #  ------
    #  ------
    #  | || x
    #  ------
    assert is_edge(PanelCoordinate(
        level=0, y=1, x=1, panel=RIGHT,
    ), box)

    #  ------
    #  | || |
    #  ------
    #  ------
    #  | || |
    #  ----x-
    assert is_edge(PanelCoordinate(
        level=0, y=1, x=1, panel=BACK,
    ), box)

    # TOP and BOTTOM
    assert is_edge(PanelCoordinate(
        level=0, y=1, x=1, panel=TOP,
    ), box)
    assert is_edge(PanelCoordinate(
        level=0, y=1, x=1, panel=BOTTOM,
    ), box)
Exemple #14
0
def test_cell_counts():
    assert get_cell_count(generate_empty_box(4, 4, 4)) == 64
    assert get_cell_count(generate_empty_box(1, 3, 3)) == 9
    assert get_cell_count(generate_empty_box(1, 1, 1)) == 1