2023-08-03 21:07:58 +02:00
|
|
|
package solver
|
|
|
|
|
2023-08-11 12:30:37 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"maze-solver/maze"
|
|
|
|
)
|
2023-08-03 21:07:58 +02:00
|
|
|
|
|
|
|
type Solver interface {
|
|
|
|
Solve(*maze.Maze) *maze.SolvedMaze
|
|
|
|
}
|
2023-08-11 12:30:37 +02:00
|
|
|
|
2023-08-14 20:00:41 +02:00
|
|
|
type solver struct {
|
|
|
|
visited map[*maze.Node]bool
|
|
|
|
}
|
|
|
|
|
2023-08-11 12:30:37 +02:00
|
|
|
type SolverFactory struct {
|
|
|
|
Type *string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2023-08-14 20:00:41 +02:00
|
|
|
_DFS = "dfs"
|
|
|
|
_BFS = "bfs"
|
2023-08-11 12:30:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var TYPES = []string{
|
2023-08-14 20:00:41 +02:00
|
|
|
_DFS,
|
|
|
|
_BFS,
|
2023-08-11 12:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *SolverFactory) Get() Solver {
|
|
|
|
switch *f.Type {
|
2023-08-14 20:00:41 +02:00
|
|
|
case _DFS:
|
2023-08-13 21:31:47 +02:00
|
|
|
return &DFSSolver{}
|
2023-08-14 20:00:41 +02:00
|
|
|
case _BFS:
|
|
|
|
return &BFSSolver{}
|
2023-08-11 12:30:37 +02:00
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("Unrecognized solver type %q", *f.Type))
|
|
|
|
}
|
2023-08-14 20:00:41 +02:00
|
|
|
|
|
|
|
func (s *solver) wasVisited(node *maze.Node) bool {
|
|
|
|
if node == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
visited, _ := s.visited[node]
|
|
|
|
return visited
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *solver) initVisited(m *maze.Maze) {
|
|
|
|
s.visited = make(map[*maze.Node]bool, len(m.Nodes))
|
|
|
|
|
|
|
|
for _, node := range m.Nodes {
|
|
|
|
s.visited[node] = false
|
|
|
|
}
|
|
|
|
}
|