feat: Implemented visualizer

This commit is contained in:
Karma Riuk
2023-08-17 13:37:54 +02:00
parent 399bb9f137
commit 5124a4b98b
10 changed files with 846 additions and 35 deletions

View File

@ -7,6 +7,7 @@ import (
)
type AStarSolver struct {
solved_chan chan<- *maze.SolvedMaze
dist_from_start map[*maze.Node]int
dist_from_end map[*maze.Node]int
parent map[*maze.Node]*maze.Node
@ -28,6 +29,12 @@ func (s *AStarSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
for current != end {
current.Visited = true
if s.solved_chan != nil {
s.solved_chan <- &maze.SolvedMaze{
Maze: m,
Solution: s.generateSolution(current, m),
}
}
for _, child := range []*maze.Node{current.Left, current.Right, current.Up, current.Down} {
if child != nil {
@ -48,19 +55,21 @@ func (s *AStarSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
current = s.stack.pop()
}
return &maze.SolvedMaze{
Maze: m,
Solution: s.generateSolution(current, m),
}
}
func (s *AStarSolver) generateSolution(current *maze.Node, m *maze.Maze) []*maze.Node {
solution := make([]*maze.Node, 0, len(m.Nodes))
for current != m.Nodes[0] {
solution = append(solution, current)
current = s.parent[current]
}
solution = append(solution, m.Nodes[0])
for i, j := 0, len(solution)-1; i < j; i, j = i+1, j-1 {
solution[i], solution[j] = solution[j], solution[i]
}
return &maze.SolvedMaze{
Maze: m,
Solution: solution,
}
return solution
}

View File

@ -9,7 +9,8 @@ import (
)
type BFSSolver struct {
queue *Queue
solved_chan chan<- *maze.SolvedMaze
queue *Queue
}
type Queue struct {
@ -87,6 +88,12 @@ func (s *BFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
var err error
for current != end {
current.Visited = true
if s.solved_chan != nil {
s.solved_chan <- &maze.SolvedMaze{
Maze: m,
Solution: current_history,
}
}
s.addIfNotVisited(current.Down, current_history)
s.addIfNotVisited(current.Left, current_history)
@ -99,6 +106,12 @@ func (s *BFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
}
current = current_history[len(current_history)-1]
}
if s.solved_chan != nil {
s.solved_chan <- &maze.SolvedMaze{
Maze: m,
Solution: current_history,
}
}
return &maze.SolvedMaze{
Maze: m,

View File

@ -5,7 +5,9 @@ import (
"maze-solver/utils"
)
type DFSSolver struct{}
type DFSSolver struct {
solved_chan chan<- *maze.SolvedMaze
}
func (s *DFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
defer utils.Timer("DFS algorithm", 2)()
@ -17,6 +19,12 @@ func (s *DFSSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
for current != end {
current.Visited = true
if s.solved_chan != nil {
s.solved_chan <- &maze.SolvedMaze{
Maze: m,
Solution: stack,
}
}
left_visited, right_visited, up_visited, down_visited := visited(current.Left), visited(current.Right), visited(current.Up), visited(current.Down)

View File

@ -7,6 +7,7 @@ import (
)
type DijkstraSolver struct {
solved_chan chan<- *maze.SolvedMaze
dist_from_start map[*maze.Node]int
parent map[*maze.Node]*maze.Node
stack sorted_stack
@ -25,7 +26,6 @@ func (s *DijkstraSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
for current != end {
current.Visited = true
for _, child := range []*maze.Node{current.Left, current.Right, current.Up, current.Down} {
if child != nil {
dist := s.dist_from_start[current] + int(current.Coords.Distance(child.Coords))
@ -43,21 +43,30 @@ func (s *DijkstraSolver) Solve(m *maze.Maze) *maze.SolvedMaze {
}
}
current = s.stack.pop()
if s.solved_chan != nil {
s.solved_chan <- &maze.SolvedMaze{
Maze: m,
Solution: s.generateSolution(current, m),
}
}
}
return &maze.SolvedMaze{
Maze: m,
Solution: s.generateSolution(current, m),
}
}
func (s *DijkstraSolver) generateSolution(current *maze.Node, m *maze.Maze) []*maze.Node {
solution := make([]*maze.Node, 0, len(m.Nodes))
for current != m.Nodes[0] {
solution = append(solution, current)
current = s.parent[current]
}
solution = append(solution, m.Nodes[0])
for i, j := 0, len(solution)-1; i < j; i, j = i+1, j-1 {
solution[i], solution[j] = solution[j], solution[i]
}
return &maze.SolvedMaze{
Maze: m,
Solution: solution,
}
return solution
}

View File

@ -27,16 +27,24 @@ var TYPES = []string{
_AStar,
}
func (f *SolverFactory) Get() Solver {
func (f *SolverFactory) Get(solved_chan chan<- *maze.SolvedMaze) Solver {
switch *f.Type {
case _DFS:
return &DFSSolver{}
return &DFSSolver{
solved_chan: solved_chan,
}
case _BFS:
return &BFSSolver{}
return &BFSSolver{
solved_chan: solved_chan,
}
case _AStar:
return &AStarSolver{}
return &AStarSolver{
solved_chan: solved_chan,
}
case _Dijkstra:
return &DijkstraSolver{}
return &DijkstraSolver{
solved_chan: solved_chan,
}
}
panic(fmt.Sprintf("Unrecognized solver type %q", *f.Type))
}