Example #1
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)
Example #2
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
Example #3
0
def test_contains_with_missing_node():
    entrance = PanelCoordinate(0, 0, 0, LEFT)
    _exit = PanelCoordinate(0, 1, 0, LEFT)
    dimensions = (1, 2, 2)
    tracker = FailedChoices(entrance, _exit, dimensions)

    path = [BACK, LEFT, BACK, LEFT]

    assert path not in tracker
Example #4
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)
Example #5
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
Example #6
0
def test_contains_with_existing_node():
    entrance = PanelCoordinate(0, 0, 0, LEFT)
    _exit = PanelCoordinate(0, 1, 0, LEFT)
    dimensions = (1, 2, 2)
    tracker = FailedChoices(entrance, _exit, dimensions)

    path = [BACK, LEFT, BACK, LEFT]

    Node.add_root(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="blbl",
    )

    assert path in tracker
Example #7
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)
Example #8
0
def test_adding_child_with_some_existing_and_some_missing():
    entrance = PanelCoordinate(0, 0, 0, LEFT)
    _exit = PanelCoordinate(0, 1, 0, LEFT)
    dimensions = (1, 2, 2)
    tracker = FailedChoices(entrance, _exit, dimensions)

    path = [BACK, LEFT, BACK, LEFT]

    root = Node.add_root(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="b",
    )
    root.add_child(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="bl",
    )
    assert not Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="blb",
    ).exists()
    assert not Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="blbl",
    ).exists()

    tracker.add(path)

    assert Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="blb",
    ).exists()
    assert Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="blbl",
    ).exists()
Example #9
0
def test_adding_child_creates_parent_chain():
    entrance = PanelCoordinate(0, 0, 0, LEFT)
    _exit = PanelCoordinate(0, 1, 0, LEFT)
    dimensions = (1, 2, 2)
    tracker = FailedChoices(entrance, _exit, dimensions)

    path = [BACK, LEFT, BACK]

    assert not Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="blb",
    ).exists()
    assert not Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="bl",
    ).exists()
    assert not Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="b",
    ).exists()

    tracker.add(path)

    assert Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="blb",
    ).exists()
    assert Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="bl",
    ).exists()
    assert Node.objects.filter(
        entrance="0:0:0:left",
        exit="0:1:0:left",
        dimensions="1:2:2",
        value="b",
    ).exists()
Example #10
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
Example #11
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)
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
Example #13
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)
Example #14
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)
Example #15
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)