Esempio n. 1
0
def empty_maze(w, h):
    lab = Labyrinth(w, h)
    lab[0, 0] = 1
    lab.start = 0, 0
    lab[w - 2, h - 2] = 1
    lab.goal = w - 2, h - 2
    return lab
Esempio n. 2
0
__author__ = 'davide'

from labyrinth import Labyrinth, NeighborsGenerator
import numpy as np

if __name__ == "__main__":
    lab = Labyrinth(6, 5)
    lab.start = 0, 0
    lab.goal = 5, 4
    lab.walls = {(2, 2), (3, 2), (4, 2), (5, 2)}

    n = NeighborsGenerator(lab)
    current = np.array([1, 2])
    direction = np.array([1, 1])

    # the correct result is [3,4], but the third call
    # returns None
    print(n.jump_rec(current, direction, lab.goal))
    print(n.jump_it_1(current, direction, lab.goal))
    print(n.jump_it_2(current, direction, lab.goal))

    for i in range(lab.w):
        print("|", end="")
        for j in range(lab.h):
            if (i, j) == lab.start:
                print("S|", end="")
            elif (i, j) == lab.goal:
                print("G|", end="")
            else:
                print(" " if lab[i, j] else "X", end="|")
        print()
Esempio n. 3
0
def maze(w, h, size=2):
    def conv_size(n):
        return (n - 1) // size + 1

    nw, nh = conv_size(w), conv_size(h)
    ns = size // 2 - 1
    uf = UnionFind(nw * nh)
    lab = Labyrinth(w, h)

    for x in range(w):
        for y in range(h):
            lab[x, y] = 0

    edges = []
    for i in range(nh - 1):
        for j in range(nw - 1):
            f = flatten(i, j, nw, nh)
            edges.append((f, f + 1))  # right
            edges.append((f, f + nw))  # down

    for i in range(nh - 1):
        f = flatten(i, nw - 1, nw, nh)
        edges.append((f, f + nw))  # down

    for j in range(nw - 1):
        f = flatten(nh - 1, j, nw, nh)
        edges.append((f, f + 1))  # right

    shuffle(edges)

    while len(uf) > 1:
        u, v = edges.pop()
        y1, x1 = unflatten(u, nw, nh)
        y2, x2 = unflatten(v, nw, nh)
        if uf.find(u) != uf.find(v):
            uf.union(u, v)
            if x2 - x1 == 1:
                for i in range(size + 1):
                    for j in range(1, ns + 1):
                        ny = size * y1 - j
                        if ny >= 0:
                            lab[size * x1 + i, ny] = True
                        else:
                            break
                    lab[size * x1 + i, size * y1] = True
                    for j in range(1, ns + 1):
                        ny = size * y1 + j
                        if ny < h:
                            lab[size * x1 + i, ny] = True
                        else:
                            break
            else:
                for i in range(size + 1):
                    for j in range(1, ns + 1):
                        nx = size * x1 - j
                        if nx >= 0:
                            lab[nx, size * y1 + i] = True
                        else:
                            break
                    lab[size * x1, size * y1 + i] = True
                    for j in range(1, ns + 1):
                        nx = size * x1 + j
                        if nx < w:
                            lab[nx, size * y1 + i] = True
                        else:
                            break

    lab[0, 0] = 1
    lab.start = 0, 0
    lab[lab.w - 2, lab.h - 2] = 1
    lab.goal = lab.w - 2, lab.h - 2

    return lab