-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze-gen-asc.awk
66 lines (59 loc) · 1.75 KB
/
maze-gen-asc.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env gawk -f
#
# Copyright (c) 2023 Jegors Čemisovs
# License: MIT License
# Repository: https://github.com/rabestro/awk-maze-generator
#
# These variables are initialized on the command line (using '-v'):
# - Rows
# - Cols
# - Seed (optional)
BEGIN {
setup()
create_grid()
generate_maze(Width + 1)
clear_doors()
print_maze()
}
function setup() {
OFS = ""
if (!Rows) die("number of rows must be specified")
if (!Cols) die("number of columns must be specified")
Seed ? srand(Seed) : srand()
}
function create_grid() {
Width = 2 * Cols + 1
Height = 2 * Rows + 1
while (GridSize < Width * Height)
Grid[GridSize++] = 1
}
function generate_maze(source, directions,direction,target) {
Grid[source] = 0
for (directions = "NEWS"; length(directions); sub(direction, "", directions)) {
direction = substr(directions, int(rand() * length(directions)) + 1, 1)
target = next_node_index(source, direction)
if (!Grid[target]) continue
Grid[(source + target) / 2] = 0
generate_maze(target)
}
}
function clear_doors() {
Grid[(1 + 2 * int(rand() * Rows)) * Width] = 0
Grid[(1 + 2 * int(rand() * Rows)) * Width + Width - 1] = 0
}
function print_maze() {
while (GridSize--)
printf (GridSize % Width ? "%s" : "%s\n"), (Grid[GridSize] ? "##" : " ")
}
function next_node_index(i, direction) {
if (direction == "N" && i > 2 * Width)
return i - 2 * Width
if (direction == "S" && i < GridSize - 2 * Width)
return i + 2 * Width
if (direction == "E" && i % Width < Width - 2)
return i + 2
if (direction == "W" && i % Width > 2)
return i - 2
return -1
}
function die(message) {print message > "/dev/stderr"; exit 1}