-
Notifications
You must be signed in to change notification settings - Fork 0
/
deep_first.go
55 lines (42 loc) · 1 KB
/
deep_first.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
package main
import (
"math/rand"
"time"
)
//Depth-First iterative
func GeneratePathDeepFirst(maze Maze) {
dx, dy := maze.Size()
rand.Seed(time.Now().UnixNano())
dxStart := rand.Intn(dx)
dyStart := rand.Intn(dy)
maze.cells[dxStart][dyStart].visited = true
stack := Stack{Data: make([]Position, 0, dx*dy)}
stack.push(Position{dxStart, dyStart})
for stack.hasElements() {
current := stack.pop()
neighbour, err := current.RollUnvisitedNeighbour(maze)
if err != nil {
continue
}
//Current goes back to the stack
stack.push(current)
//Remove Walls and set chosen Cell as visited
maze.RemoveWalls(current, neighbour)
maze.cells[neighbour.x][neighbour.y].visited = true
stack.push(neighbour)
}
}
type Stack struct {
Data []Position
}
func (s Stack) hasElements() bool {
return len(s.Data) > 0
}
func (s *Stack) push(pos Position) {
s.Data = append(s.Data, pos)
}
func (s *Stack) pop() Position {
cell, cdr := s.Data[len(s.Data)-1], s.Data[:len(s.Data)-1]
s.Data = cdr
return cell
}