Maze generation: Difference between revisions
→{{header|jq}}: simplify
(→{{header|jq}}: simplify) |
|||
Line 5,589:
</pre>
In the following, a maze is represented by a matrix, each of whose elements
is in turn a
$box[
other points of the compass.
cell; this is just to make it clear that walk starts at the top left of the
display.
▲Note that for simplicity, in the following the "walk" always starts at the (0,0) cell.
<syntaxhighlight lang="jq">
# Output: a prn in range(0;$n) where $n is .
Line 5,622 ⟶ 5,619:
| .a
end;
# Compass is a JSON object {n,s,e,w} representing the four possible
# directions in which to move, i.e. to open a gate.
# For example, Compass.n corresponds to a move north (i.e. dx is 0, dy is 1),
# and Compass.n.gates[
def Compass:
{n: { gates:
s: { gates:
e: { gates:
w: { gates:
| .n.opposite = .s
| .s.opposite = .n
Line 5,648 ⟶ 5,637:
# Produce a matrix representing an $x x $y maze.
# .[$i][$j] represents the box in row $i, column $j.
# Initially, all the gates of all the boxes are closed
def MazeMatrix($x; $y):
[range(0;$x) |
# Input: a MazeMatrix
Line 5,662 ⟶ 5,651:
($cx + $v.dx) as $nx
| ($cy + $v.dy) as $ny
| if interior($nx; $x) and interior($ny; $y) and
then .[$cx][$cy]
| .[$nx][$ny]
| generate($nx; $ny)
end );
Line 5,676 ⟶ 5,665:
# draw the north edge
| ([range(0;$x) as $j
| if $maze[$j][$i][
# draw the west edge
([range(0;$x) as $j
| if $maze[$j][$i][
# draw the bottom line
($x * "+---") + "+"
;
#
def amaze($x;$y):
MazeMatrix($x; $y)
|