예제 #1
0
파일: sidewinder.py 프로젝트: nwtnni/maze
def generate(w, h):
    maze = Maze(w, h)
    yield maze

    # Directional bias
    hd = choice([D.E, D.W])
    vd = choice([D.S, D.N])
    d = [hd, vd]

    rows = range(h) if vd == D.S else range(h - 1, -1, -1)
    cols = range(w) if hd == D.E else range(w - 1, -1, -1)

    for row in rows[:-1]:
        run = []

        for col in cols:
            run.append(col)
            if col == cols[-1] or choice(d) == vd:
                maze.carve(Point(choice(run), row), vd)
                run.clear()
            else:
                maze.carve(Point(col, row), hd)
            yield maze

    for col in cols:
        maze.carve(Point(col, rows[-1]), hd)
        yield maze
예제 #2
0
def parse_output(frames, w, h):
    out = argv[4]

    if out == "--print":
        handle_print(frames)

    elif out == "--step":
        try:
            delay = pos_float(argv[5])
        except ValueError:
            print("Error: <SEC> must be positive float")
            return
        handle_step(frames, delay)

    elif out == "--outline":
        path = argv[5]
        handle_outline(frames, path)

    elif out == "--solution" or out == "--paired" or out == "--heatmap":
        try:
            s = Point(int(argv[6]), int(argv[7]))
            if out == "--heatmap":
                f = Point(0, 0)
            else:
                f = Point(int(argv[8]), int(argv[9]))
        except ValueError:
            print("Error: coordinates must be integers")
            return

        def lesser(s, f):
            return s.x < 0 or s.y < 0 or f.x < 0 or f.y < 0

        def greater(s, f):
            return s.x >= w or f.x >= w or s.y >= h or f.y >= h

        if lesser(s, f) or greater(s, f):
            print("Error: coordinates out of bounds")
            return

        path = argv[5]
        if out == "--solution":
            handle_solution(frames, path, s, f)
        elif out == "--paired":
            handle_paired(frames, path, s, f)
        else:
            handle_heat(frames, path, s)

    elif out == "--gif":
        path = argv[5]
        try:
            fps = pos_float(argv[6])
        except ValueError:
            print("Error: <FPS> must be positive float")
        handle_gif(frames, path, fps)

    else:
        print("Error: unknown <OUTPUT> flag")
        return
예제 #3
0
def outline(maze):
    lines = [[(0, 0), (maze.w, 0)]]
    for y in range(maze.h):
        v = [[(x, y), (x, y + 1)] for x in range(maze.w)
             if maze.is_wall(Point(x, y), D.W)]
        v.append([(maze.w, y), (maze.w, y + 1)])
        h = [[(x, y + 1), (x + 1, y + 1)] for x in range(maze.w)
             if maze.is_wall(Point(x, y), D.S)]
        lines.extend(v)
        lines.extend(h)
    return mc.LineCollection(lines, colors="blue")
예제 #4
0
def heat(maze, s):
    patches = []
    colors = []
    d, r = search(maze, s)
    scale = max(d.values()) * 1.5

    for y in range(maze.h):
        for x in range(maze.w):
            patches.append(p.Rectangle((x, y), 1, 1))
            colors.append((0, 0, 1, d[Point(x, y)] / scale))
    return mc.PatchCollection(patches, facecolors=colors)
예제 #5
0
파일: kruskal.py 프로젝트: nwtnni/maze
def generate(w, h):
    maze = Maze(w, h)
    yield maze
    ids = {
        Point(x, y): str(x) + " " + str(y)
        for x in range(w) for y in range(h)
    }
    visit = [(Point(x, y), d) for x in range(w) for y in range(h)
             for d in maze.neighbors(Point(x, y))]
    shuffle(visit)

    for n, d in visit:
        f = ids[n.adj(d)]
        t = ids[n]

        if f == t:
            continue

        for k, v in ids.items():
            if v == f:
                ids[k] = t

        maze.carve(n, d)
        yield maze
예제 #6
0
def generate(w, h):
    maze = Maze(w, h)
    yield maze
    n = maze.rand_point()
    unknown = set([Point(x, y) for x in range(w) for y in range(h)])
    unknown.remove(n)

    while len(unknown) > 0:
        path = [choice(list(unknown))]
        dirpath = []

        while path[-1] in unknown:
            last = path[-1]
            dirs = maze.neighbors(last)
            path.append(last.adj(dirs[0]))
            dirpath.append(dirs[0])
            path, dirpath = prune(path, dirpath)

        for i in range(len(path) - 1):
            maze.carve(path[i], dirpath[i])
            unknown.remove(path[i])
            yield maze
예제 #7
0
def generate(w, h):
    maze = Maze(w, h)
    yield maze

    # Carving directions
    hd = choice([D.W, D.E])
    vd = choice([D.N, D.S])
    d = [hd, vd]

    # Fill in opposite wall
    x = 0 if hd == D.W else w - 1
    y = 0 if vd == D.N else h - 1

    for row in range(h):
        for col in range(w):
            p = Point(col, row)
            if col == x:
                maze.carve(p, vd)
            elif row == y:
                maze.carve(p, hd)
            else:
                maze.carve(p, choice(d))
            yield maze