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
|
|
|
|
|
|
|
type SolverFactory struct {
|
|
|
|
Type *string
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2023-08-15 17:22:10 +02:00
|
|
|
_DFS = "dfs"
|
|
|
|
_BFS = "bfs"
|
|
|
|
_Dijkstra = "dijkstra"
|
2023-08-15 18:43:18 +02:00
|
|
|
_AStar = "a-star"
|
2023-08-11 12:30:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var TYPES = []string{
|
2023-08-14 20:00:41 +02:00
|
|
|
_DFS,
|
|
|
|
_BFS,
|
2023-08-15 17:22:10 +02:00
|
|
|
_Dijkstra,
|
2023-08-15 18:43:18 +02:00
|
|
|
_AStar,
|
2023-08-11 12:30:37 +02:00
|
|
|
}
|
|
|
|
|
2023-08-17 13:37:54 +02:00
|
|
|
func (f *SolverFactory) Get(solved_chan chan<- *maze.SolvedMaze) Solver {
|
2023-08-11 12:30:37 +02:00
|
|
|
switch *f.Type {
|
2023-08-14 20:00:41 +02:00
|
|
|
case _DFS:
|
2023-08-17 13:37:54 +02:00
|
|
|
return &DFSSolver{
|
|
|
|
solved_chan: solved_chan,
|
|
|
|
}
|
2023-08-14 20:00:41 +02:00
|
|
|
case _BFS:
|
2023-08-17 13:37:54 +02:00
|
|
|
return &BFSSolver{
|
|
|
|
solved_chan: solved_chan,
|
|
|
|
}
|
2023-08-15 18:43:18 +02:00
|
|
|
case _AStar:
|
2023-08-17 13:37:54 +02:00
|
|
|
return &AStarSolver{
|
|
|
|
solved_chan: solved_chan,
|
|
|
|
}
|
2023-08-15 17:22:10 +02:00
|
|
|
case _Dijkstra:
|
2023-08-17 13:37:54 +02:00
|
|
|
return &DijkstraSolver{
|
|
|
|
solved_chan: solved_chan,
|
|
|
|
}
|
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
|
|
|
|
2023-08-15 15:42:53 +02:00
|
|
|
func visited(node *maze.Node) bool {
|
2023-08-15 15:41:24 +02:00
|
|
|
return node == nil || node.Visited
|
2023-08-14 20:00:41 +02:00
|
|
|
}
|