mirror of
https://github.com/Stone-Red-Code/Maze.git
synced 2026-09-04 09:06:14 +02:00
Initial commit
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using Maze.Nodes;
|
||||
using Maze.Utlis;
|
||||
|
||||
namespace Maze.Solvers;
|
||||
|
||||
internal class BreadthFirstSearchMazeSolver : IMazeSolver<BfsNode>
|
||||
{
|
||||
public List<BfsNode> Solve(BfsNode node, BfsNode[,] maze, GraphicsMode graphicsMode = GraphicsMode.None)
|
||||
{
|
||||
if (!node.Start)
|
||||
{
|
||||
throw new MazeException("Node is not a start node.");
|
||||
}
|
||||
|
||||
BfsNode currentNode;
|
||||
Queue<BfsNode> queue = new Queue<BfsNode>();
|
||||
|
||||
queue.Enqueue(node);
|
||||
|
||||
while (queue.Count > 0)
|
||||
{
|
||||
currentNode = queue.Dequeue();
|
||||
currentNode.Updated = true;
|
||||
if (currentNode.Parent is not null)
|
||||
{
|
||||
currentNode.Parent.Updated = true;
|
||||
}
|
||||
|
||||
if (currentNode.Goal)
|
||||
{
|
||||
List<BfsNode> path = new List<BfsNode>();
|
||||
BfsNode? pathNode = currentNode;
|
||||
|
||||
while (pathNode is not null && !pathNode.Start)
|
||||
{
|
||||
pathNode.Updated = true;
|
||||
path.Add(pathNode);
|
||||
pathNode = pathNode.Parent;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
foreach (BfsNode child in currentNode.Neighbors.Where(n => !n.Visited && !n.Wall))
|
||||
{
|
||||
child.Parent = currentNode;
|
||||
child.Visited = true;
|
||||
child.Updated = true;
|
||||
queue.Enqueue(child);
|
||||
}
|
||||
|
||||
if (graphicsMode != GraphicsMode.None)
|
||||
{
|
||||
Thread.Sleep(50);
|
||||
Console.SetCursorPosition(0, 0);
|
||||
}
|
||||
|
||||
if (graphicsMode == GraphicsMode.BlackWhite)
|
||||
{
|
||||
new MazePrinter().Print(maze, queue.ToList());
|
||||
}
|
||||
else if (graphicsMode == GraphicsMode.Colored)
|
||||
{
|
||||
new MazePrinter().PrintColored(maze, queue.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
return new List<BfsNode>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
using Maze.Nodes;
|
||||
using Maze.Utlis;
|
||||
|
||||
namespace Maze.Solvers;
|
||||
|
||||
internal class DepthFirstSearchMazeSolver : IMazeSolver<DfsNode>
|
||||
{
|
||||
public List<DfsNode> Solve(DfsNode node, DfsNode[,] maze, GraphicsMode graphicsMode = GraphicsMode.None)
|
||||
{
|
||||
if (!node.Start)
|
||||
{
|
||||
throw new MazeException("Node is not a start node.");
|
||||
}
|
||||
|
||||
DfsNode? currentNode = node;
|
||||
DfsNode? tempNode = null;
|
||||
Stack<DfsNode> path = new Stack<DfsNode>();
|
||||
|
||||
while (!currentNode.Goal)
|
||||
{
|
||||
currentNode.Visited = true;
|
||||
|
||||
if (currentNode.Neighbors.Count == 0)
|
||||
{
|
||||
throw new MazeException("Node has no neighbors.");
|
||||
}
|
||||
|
||||
if (path.TryPeek(out DfsNode? prevNode))
|
||||
{
|
||||
prevNode.Updated = true;
|
||||
}
|
||||
|
||||
currentNode = currentNode.Neighbors.FirstOrDefault(n => !n.Visited && !n.Wall);
|
||||
|
||||
if (currentNode is null)
|
||||
{
|
||||
if (!path.TryPop(out currentNode))
|
||||
{
|
||||
throw new MazeException("No path found.");
|
||||
}
|
||||
|
||||
if (path.TryPeek(out DfsNode? prevNode2))
|
||||
{
|
||||
prevNode2.Updated = true;
|
||||
}
|
||||
|
||||
tempNode = currentNode;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add the previous node to the path
|
||||
if (tempNode is not null)
|
||||
{
|
||||
tempNode.Updated = true;
|
||||
path.Push(tempNode);
|
||||
tempNode = null;
|
||||
}
|
||||
path.Push(currentNode);
|
||||
}
|
||||
|
||||
currentNode.Updated = true;
|
||||
|
||||
if (graphicsMode != GraphicsMode.None)
|
||||
{
|
||||
Thread.Sleep(30);
|
||||
Console.SetCursorPosition(0, 0);
|
||||
}
|
||||
|
||||
if (graphicsMode == GraphicsMode.BlackWhite)
|
||||
{
|
||||
new MazePrinter().Print(maze, path.ToList());
|
||||
}
|
||||
else if (graphicsMode == GraphicsMode.Colored)
|
||||
{
|
||||
new MazePrinter().PrintColored(maze, path.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
List<DfsNode> solution = path.ToList();
|
||||
solution.ForEach(n => n.Updated = true);
|
||||
return solution;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Maze.Nodes;
|
||||
|
||||
namespace Maze.Solvers;
|
||||
|
||||
internal interface IMazeSolver<T> where T : BaseMazeNode<T>, new()
|
||||
{
|
||||
public List<T> Solve(T node, T[,] maze, GraphicsMode graphicsMode = GraphicsMode.None);
|
||||
}
|
||||
Reference in New Issue
Block a user