-
Notifications
You must be signed in to change notification settings - Fork 20
/
maze.nim
60 lines (52 loc) · 1.4 KB
/
maze.nim
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
# Maze generator in Nimrod
# Joe Wingbermuehle 2013-10-01
import math
# Width and height must be odd.
const width = 39
const height = 23
type MazeT = array[0 .. height - 1, array[0 .. width - 1, int]]
proc showMaze(maze: MazeT) =
for y in countup(0, height - 1):
for x in countup(0, width - 1):
if maze[y][x] == 1:
write(stdout, "[]")
else:
write(stdout, " ")
write(stdout, "\n")
proc initMaze(maze: ref MazeT) =
for y in countup(0, height - 1):
for x in countup(0, width - 1):
maze[y][x] = 1
for x in countup(0, width - 1):
maze[0][x] = 0
maze[height - 1][x] = 0
for y in countup(0, height - 1):
maze[y][0] = 0
maze[y][width - 1] = 0
proc carveMaze(maze: ref MazeT, x, y: int) =
maze[y][x] = 0
let d = math.random(4)
for i in countup(0, 3):
var dx, dy: int
case (d + i) mod 4
of 0: dx = 1
of 1: dx = -1
of 2: dy = 1
else: dy = -1
let
nx = x + dx
ny = y + dy
nx2 = x + 2 * dx
ny2 = y + 2 * dy
if maze[ny][nx] == 1 and maze[ny2][nx2] == 1:
maze[ny][nx] = 0
carveMaze(maze, nx2, ny2)
proc generateMaze(): MazeT =
var Presult: ref MazeT
new(Presult)
initMaze(Presult)
carveMaze(Presult, 2, 2)
Presult[1][2] = 0
Presult[height - 2][width - 3] = 0
return Presult[]
showMaze(generateMaze())