-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmaze.vim
86 lines (80 loc) · 2.1 KB
/
maze.vim
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
" Maze generator in VIM.
" Joe Wingbermuehle 20130328
"
" To use this, load the generator into vim (run "vim -S maze.vim") then
" call the GenerateMaze function with the maze dimensions, for example:
" :call GenerateMaze(15, 10)
"
" Generate a random direction.
fu! RandDir()
let r=system("echo $RANDOM")
return eval(r % 4)
endfu
" Initialize the maze.
fu! InitMaze(width, height)
for i in range(0, eval(a:width * a:height - 1))
let g:maze_{i} = 1
endfor
for i in range(0, eval(a:width - 1))
let g:maze_{i} = 0
let g:maze_{eval(i + (a:height - 1) * a:width)} = 0
endfor
for i in range(0, eval(a:height - 1))
let g:maze_{eval(i * a:width)} = 0
let g:maze_{eval(i * a:width + a:width - 1)} = 0
endfor
endfu
" Display the maze.
fu! ShowMaze(width, height)
put ='Maze generator in VIM'
put ='Joe Wingbermuehle 20130328'
for y in range(0, eval(a:height - 1))
let line = ''
for x in range(0, eval(a:width - 1))
let i = eval(y * a:width + x)
if g:maze_{i} == 1
let line .= '[]'
else
let line .= ' '
endif
endfor
put =line
endfor
endfu
" Carve the maze.
fu! CarveMaze(width, height, x, y)
let d = RandDir()
for i in range(0, 3)
let dx = 0
let dy = 0
if d == 0
let dx = 1
elseif d == 1
let dx = -1
elseif d == 2
let dy = 1
else
let dy = -1
endif
let a = eval((a:y + dy) * a:width + a:x + dx)
let nx = eval(a:x + 2 * dx)
let ny = eval(a:y + 2 * dy)
let b = eval(ny * a:width + nx)
if g:maze_{a} == 1 && g:maze_{b} == 1
let g:maze_{a} = 0
let g:maze_{b} = 0
call CarveMaze(a:width, a:height, nx, ny)
endif
let d = eval((d + 1) % 4)
endfor
endfu
" Generate and display a random maze.
fu! GenerateMaze(width, height)
let w = eval(a:width * 2 + 1)
let h = eval(a:height * 2 + 1)
call InitMaze(w, h)
call CarveMaze(w, h, 2, 2)
let g:maze_{eval(1 * w + 2)} = 0
let g:maze_{eval((h - 2) * w + (w - 3))} = 0
call ShowMaze(w, h)
endfu