RosettaCodeData/Task/Maze-generation/Aime/maze-generation.aime
2019-09-12 10:33:56 -07:00

74 lines
1.4 KiB
Text

grid_maze(data b, integer N)
{
data d;
N.times(bb_cast, d, "+---");
bb_cast(d, "+\n");
N.times(bb_cast, d, "| * ");
bb_cast(d, "|\n");
N.times(bb_copy, b, d);
b_size(d, N * 4 + 2);
bb_copy(b, d);
}
void
walk_cell(data b, integer N, line_size, x, y, list x_offsets, y_offsets)
{
integer i, p, q, r;
b_replace(b, y + x, ' ');
r = drand(3);
i = 0;
while (i < 4) {
p = x + x_offsets[q = (r + i) & 3];
q = y + y_offsets[q];
if (-1 < p && p < line_size
&& -1 < q && q < line_size * (N * 2 + 1)) {
if (b[q + p] == '*') {
walk_cell(b, N, line_size, p, q, x_offsets, y_offsets);
b[(q + y) / 2 + (p + x) / 2] = ' ';
if (p == x) {
b[(q + y) / 2 + p - 1] = ' ';
b[(q + y) / 2 + p + 1] = ' ';
}
}
}
i += 1;
}
}
walk_maze(data b, integer N)
{
integer line_size, x, y;
list x_offsets, y_offsets;
line_size = N * 4 + 1 + 1;
l_bill(x_offsets, 0, 4, 0, -4, 0);
l_bill(y_offsets, 0, 0, line_size * 2, 0, line_size * -2);
x = drand(N - 1) * 4 + 2;
y = line_size * (drand(N - 1) * 2 + 1);
walk_cell(b, N, line_size, x, y, x_offsets, y_offsets);
}
main(void)
{
data b;
grid_maze(b, 10);
walk_maze(b, 10);
o_(b);
0;
}