Anonymous user
Maze generation: Difference between revisions
→{{header|C}}: refactor
(Added second Python version) |
(→{{header|C}}: refactor) |
||
Line 300:
#define DOUBLE_SPACE 1
typedef unsigned char cell;
enum { U = 1, D = 2, L = 4, R = 8,
wchar_t
L" │││─┘┐┤─└┌├─┴┬┼ ┆┆┆┄╯╮ ┄╰╭ ┄";
int irand(int n)
Line 316 ⟶ 315:
#define FOR(i, h) for(i = 0; i <= h; i++)
cell **
{
int i, j;
cell **x = malloc(sizeof(cell*) * h + h * w);
x[0] = (cell*)(x + h);
FOR(i, h
FOR(i, h-1) FOR(j, w-1) x[i][j] = 0;
return x;
}
int d, ofx[] = {1, -1, 0, 0}, ofy[] = {0, 0, -1, 1};
int i, j,
FOR(d, 3) {
j = x + ofx[d], i = y + ofy[d];
if (j < 0 || j >= w || i < 0 || i >= h || (c[i][j] & V))
continue;
}
return cnt;
}
int build(cell **c, int w, int h, int x, int y, int avail) {
int x2, y2;
c[y][x] |= V;
while (avail) {
if (!rand_neighbor(c, w, h, x, y, &x2, &y2)) break;
if (x2 > x) c[y][x] |= R, c[y][x2] |= L; else
if (x2 < x) c[y][x] |= L, c[y][x2] |= R; else
if (y2 > y) c[y][x] |= D, c[y2][x] |= U; else
if (y2 < y) c[y][x] |= U, c[y2][x] |= D;
avail = build(c, w, h, x2, y2, avail - 1);
}
return avail;
}
{
cell **cells = grid(w, h);
build(cells, w, h, 0, 0, w * h - 1);
return cells;
}
int
{
int i, j, d, dirs[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
if (y == y2 && x == x2) return 1;
FOR(d, 3) {
if (!(c[y][x] & (1 << d))) continue;
i = y + dirs[d][1];
j = x + dirs[d][0];
if (
if (walk(c, s, w, h, j, i, x2, y2)) break;
}
s[y][x] |= (1 << d);
if (d <= 1) s[i][j] |= 1 << (1 - d);
else s[i][j] |= 1 << (5 - d);
return 1;
}
return
}
void show_maze(cell **c, cell **s,
{
int i, j, x, y;
cell **
FOR(i, 2*h) FOR(j, 2*w)
FOR(
FOR(i, h-1) FOR(j, w-1) {
y = 2 * i + 1, x = 2 * j + 1;
tile[y-1][x+1] &= ~D;
tile[ y ][x+1] = 0;
tile[y+1][x+1] &= ~U;
}
if (c[i][j] & D) {
tile[y+1][x-1] &= ~R;
tile[y+1][ x ] = 0;
tile[y+1][x+1] &= ~L;
}
if(!s) continue;
if (s[i][j] & U) tile[y-1][ x ] |= V|D;
if (s[i][j] & D) tile[y+1][ x ] |= V|U;
if (s[i][j] & L) tile[ y ][x-1] |= V|R;
if (s[i][j] & R) tile[ y ][x+1] |= V|L;
}
FOR(j, 2*w)
printf((tile[i][j] & V) ? "\033[31m%lc\033[m" : "%lc",
glyph[ tile[i][j] ]);
putchar('\n');
}
free(tile);
}
int main(int c, char *v[])
{
cell **solu, **maze;
int w = 0, h = 0;
setlocale(LC_CTYPE, "");
if (!DOUBLE_SPACE) glyph[0] = glyph[16] = L' ';
if (c > 2) w = atoi(v[1]), h = atoi(v[2]);
if (w <= 0) w =
if (h <= 0) h = 8;
show_maze(maze, solu, w, h);
/* free(
return 0;
}</lang>output<lang>┌─┬───────┬───────┬─────────────┐
│┆└─┐ │┆└───┐┆└─┤ │ │ │ └─────┐ │
├─┐┆│ └─┬──┆├─┐┆│ │ │ │ │ ┌───┤ │
│
│ │┆├── │┆──┤ │┆│ ──┴─┘ ├─┘┆│┆│ │
│ │┆│ │╰┄╮│╭┄╯│ │╭┄╯│╰┄╮│
│ │┆└─┬─┴──┆│┆┌─┴───┬───┘┆┌─┴──┆│
│ │╰┄╮│╭┄┄┄╯│┆│ ╭┄╮│╭┄┄┄╯│╭┄┄┄╯│
│ └─┐┆│┆┌───┤┆└─┐┆│┆│┆────┤┆┌───┤
│ │╰┄╯│ │╰┄╮│┆│┆│╰┄┄┄╮│┆│ │
│ │ └───┘ │ └──┆│┆│┆└────┆│┆└── │
│ │
└─┴───────┴───────┴───────┴─────┘</lang>
=={{header|D}}==
|