-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
178 lines (140 loc) · 3.71 KB
/
main.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"math/rand"
"strconv"
)
// Maze is a struct for holding not only the grid but also the size and
// the stack used by the algorithm
type Maze struct {
grid [][]int
stack [][2]int
size int
}
func newMaze(size int) *Maze {
grid := make([][]int, size)
for i := 0; i < size; i++ {
grid[i] = make([]int, size)
for j := 0; j < size; j++ {
grid[i][j] = 15
}
}
stack := make([][2]int, 0)
return &Maze{
grid,
stack,
size,
}
}
func (m *Maze) getNeighbours(x int, y int) (neighbours [][2]int) {
neighbours = make([][2]int, 0)
if x > 0 {
neighbours = append(neighbours,
[2]int{x - 1, y},
)
}
if x < m.size-1 {
neighbours = append(neighbours,
[2]int{x + 1, y},
)
}
if y > 0 {
neighbours = append(neighbours,
[2]int{x, y - 1},
)
}
if y < m.size-1 {
neighbours = append(neighbours,
[2]int{x, y + 1},
)
}
return
}
func (m *Maze) hasBeenVisited(coords [2]int) bool {
return m.grid[coords[1]][coords[0]] >= 16
}
func leftPad(str string, pad string, length int) (padded string) {
padded = str
if len(str) < length {
for i := 0; i < length-len(str); i++ {
padded = pad + padded
}
}
return
}
func main() {
size := 10
maze := newMaze(size)
initialX := rand.Intn(size)
initialY := rand.Intn(size)
maze.grid[initialY][initialX] += 16 // Mark as visited putting the most significant bit (MSB) to 1
maze.stack = append(maze.stack, [2]int{initialX, initialY})
for length := len(maze.stack); length > 0; {
// Pop the last element of the stack
currCoords := maze.stack[length-1]
maze.stack = maze.stack[:length-1]
currX := currCoords[0]
currY := currCoords[1]
neighbours := maze.getNeighbours(currX, currY)
for _, el := range neighbours {
if !maze.hasBeenVisited(el) {
maze.stack = append(maze.stack, currCoords)
xDiff := currX - el[0]
yDiff := currY - el[1]
if xDiff < 0 { // We are in the right neighbour
maze.grid[currY][currX] -= 2 // Remove right wall of the current cell
maze.grid[el[1]][el[0]] -= 8 // Remove the left wall of the chosen neighbour
}
if xDiff > 0 { // We are in the left neighbour
maze.grid[currY][currX] -= 8 // Remove left wall of the current cell
maze.grid[el[1]][el[0]] -= 2 // Remove right wall of the chosen neighbour
}
if yDiff < 0 { // We are in the bottom neighbour
maze.grid[currY][currX]-- // Remove bottom wall of the current cell
maze.grid[el[1]][el[0]] -= 4 // Remove the top wall of the chosen neighbour
}
if yDiff > 0 { // We are in the top neighbour
maze.grid[currY][currX] -= 4 // Remove top wall of the current cell
maze.grid[el[1]][el[0]]-- // Remove the bottom wall of the chosen neighbour
}
maze.grid[el[1]][el[0]] += 16 // Mark the neighbour as visited
maze.stack = append(maze.stack, [2]int{el[0], el[1]}) // Push the cosen cell into the stack
break // Stop looking for neighbours
}
}
length = len(maze.stack)
}
for i := 0; i < size*3; i++ {
print("_")
}
println()
for i := range maze.grid {
for j := range maze.grid[i] {
binary := leftPad(strconv.FormatInt(int64(maze.grid[i][j]-16), 2), "0", 4)
if string(binary[0]) == "1" { // left wall
if j > 0 { // There's a neighbour on the left
leftBinary := leftPad(strconv.FormatInt(int64(maze.grid[i][j-1]-16), 2), "0", 4)
if string(leftBinary[2]) != "1" { // Left neighbour doesn't already have a right wall
print("|")
} else {
print("_")
}
} else {
print("|")
}
} else {
print("_")
}
if string(binary[3]) == "1" { // botom wall
print("_")
} else {
print(" ")
}
if string(binary[2]) == "1" { // right wall
print("|")
} else {
print("_")
}
}
println("")
}
}