algoalgo-world
algoalgo-world/maze/origin-shift
maze/origin-shift

Origin Shift

pick a language to open the code

def origin_shift_maze(w, h, rng, steps):
    links = [0] * (w * h)
    arrow = [1 if room % w + 1 < w else 2 for room in range(w * h)]
    origin = w * h - 1
    arrow[origin] = -1
    for _ in range(steps):
        d = rng.below(4)
        other = neighbor(origin, d)
        if other < 0:
            continue
        arrow[origin] = d
        origin = other
        arrow[origin] = -1
    for room in range(w * h):
        d = arrow[room]
        if d >= 0:
            carve(links, room, neighbor(room, d), d)
    return links
function originShiftMaze(w, h, rng, steps) {
  const links = new Array(w * h).fill(0);
  const arrow = new Array(w * h).fill(0);
  for (let room = 0; room < w * h; room++) {
    arrow[room] = room % w === w - 1 ? 2 : 1;
  }
  let origin = w * h - 1;
  arrow[origin] = -1;
  for (let step = 0; step < steps; step++) {
    const d = rng.below(4);
    const other = neighbor(origin, d);
    if (other < 0) continue;
    arrow[origin] = d;
    origin = other;
    arrow[origin] = -1;
  }
  for (let room = 0; room < w * h; room++) {
    const d = arrow[room];
    if (d >= 0) carve(links, room, neighbor(room, d), d);
  }
  return links;
}
void origin_shift_maze(int links[], int w, int h, int steps) {
    int n = w * h;
    int* arrow = malloc(sizeof(int) * n);
    for (int room = 0; room < n; room++) {
        arrow[room] = room % w == w - 1 ? 2 : 1;
    }
    int origin = n - 1;
    arrow[origin] = -1;
    for (int step = 0; step < steps; step++) {
        int d = rand() % 4;
        int other = neighbor(origin, d);
        if (other < 0) continue;
        arrow[origin] = d;
        origin = other;
        arrow[origin] = -1;
    }
    for (int room = 0; room < n; room++) {
        int d = arrow[room];
        if (d >= 0) carve(links, room, neighbor(room, d), d);
    }
    free(arrow);
}
void origin_shift_maze(std::vector<int>& links, int w, int h, int steps,
                       std::mt19937& rng) {
    int n = w * h;
    std::vector<int> arrow(n, 1);
    for (int room = w - 1; room < n; room += w) arrow[room] = 2;
    int origin = n - 1;
    arrow[origin] = -1;
    std::uniform_int_distribution<int> any_dir(0, 3);
    for (int step = 0; step < steps; step++) {
        int d = any_dir(rng);
        int other = neighbor(origin, d);
        if (other < 0) continue;
        arrow[origin] = d;
        origin = other;
        arrow[origin] = -1;
    }
    for (int room = 0; room < n; room++) {
        int d = arrow[room];
        if (d >= 0) carve(links, room, neighbor(room, d), d);
    }
}
static int[] OriginShiftMaze(int w, int h, Random rng, int steps) {
    int n = w * h;
    int[] links = new int[n];
    int[] arrow = new int[n];
    for (int room = 0; room < n; room++) {
        arrow[room] = room % w == w - 1 ? 2 : 1;
    }
    int origin = n - 1;
    arrow[origin] = -1;
    for (int step = 0; step < steps; step++) {
        int dir = rng.Next(4);
        int next = Neighbor(origin, dir);
        if (next < 0) continue;
        arrow[origin] = dir;
        origin = next;
        arrow[origin] = -1;
    }
    for (int room = 0; room < n; room++) {
        int dir = arrow[room];
        if (dir >= 0) Carve(links, room, Neighbor(room, dir), dir);
    }
    return links;
}
static int[] originShiftMaze(int w, int h, Random rng, int steps) {
    int n = w * h;
    int[] links = new int[n];
    int[] arrow = new int[n];
    for (int room = 0; room < n; room++) {
        arrow[room] = room % w == w - 1 ? 2 : 1;
    }
    int origin = n - 1;
    arrow[origin] = -1;
    for (int step = 0; step < steps; step++) {
        int dir = rng.nextInt(4);
        int next = neighbor(origin, dir);
        if (next < 0) continue;
        arrow[origin] = dir;
        origin = next;
        arrow[origin] = -1;
    }
    for (int room = 0; room < n; room++) {
        int dir = arrow[room];
        if (dir >= 0) carve(links, room, neighbor(room, dir), dir);
    }
    return links;
}
watch it run, then read it