Maze generation: Difference between revisions
Content added Content deleted
m (→{{header|jq}}: (top)) |
(Added Chapel language implementation) |
||
Line 2,150: | Line 2,150: | ||
//-------------------------------------------------------------------------------------------------- |
//-------------------------------------------------------------------------------------------------- |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Chapel}}== |
|||
<syntaxhighlight lang="chapel"> |
|||
use Random; |
|||
enum direction {N = 1, E = 2, S = 3, W = 4}; |
|||
record Cell { |
|||
var spaces: [direction.N .. direction.W] bool; |
|||
var visited: bool; |
|||
} |
|||
const dirs = [ |
|||
((-1, 0), direction.N, direction.S), // ((rowDir, colDir), myWall, neighbourWall) |
|||
((0, 1), direction.E, direction.W), |
|||
((1, 0), direction.S, direction.N), |
|||
((0, -1), direction.W, direction.E) |
|||
]; |
|||
var maze: [1..9, 1..16] Cell; |
|||
var startingCell = (choose(maze.dim(0)), choose(maze.dim(1))); |
|||
checkCell(maze, startingCell); |
|||
displayMaze(maze); |
|||
proc checkCell(ref maze, cell) { |
|||
maze[cell].visited = true; |
|||
for dir in permute(dirs) { |
|||
var (offset, thisDir, neighbourDir) = dir; |
|||
var neighbour = cell + offset; |
|||
if maze.domain.contains(neighbour) && !maze[neighbour].visited { |
|||
maze[cell].spaces[thisDir] = true; |
|||
maze[neighbour].spaces[neighbourDir] = true; |
|||
checkCell(maze, neighbour); |
|||
} |
|||
} |
|||
} |
|||
proc displayMaze(maze) { |
|||
for row in maze.dim(0) { |
|||
for col in maze.dim(1) { |
|||
var cell = maze[row, col]; |
|||
if cell.spaces[direction.N] then write("+ "); else write("+---"); |
|||
} |
|||
writeln("+"); |
|||
for col in maze.dim(1) { |
|||
var cell = maze[row, col]; |
|||
if cell.spaces[direction.W] then write(" "); else write("| "); |
|||
} |
|||
writeln("|"); |
|||
} |
|||
write("+---" * maze.dim(1).size); |
|||
writeln("+"); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
|||
| | | | | |
|||
+ + + +---+---+ + +---+ + +---+---+ +---+---+ + |
|||
| | | | | | | | | | |
|||
+ +---+---+---+ +---+---+ + +---+ +---+ + + +---+ |
|||
| | | | | | | |
|||
+---+---+ + +---+---+---+---+---+---+ + +---+---+---+ + |
|||
| | | | | | | |
|||
+ + +---+---+---+---+---+ + +---+---+---+---+---+ + + |
|||
| | | | | | | | |
|||
+ +---+---+---+ + +---+---+ + +---+ +---+ +---+ + |
|||
| | | | | | | | | | | |
|||
+ +---+ + +---+---+ + +---+---+ + + +---+ + + |
|||
| | | | | | | | | |
|||
+---+---+---+ + + + +---+---+ + +---+---+ +---+ + |
|||
| | | | | | | | | | | | |
|||
+ + +---+ + + +---+ + + +---+ + +---+ + + |
|||
| | | | | | | |
|||
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
|||
</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |