Implement strategy pattern

This commit is contained in:
Stone_Red
2023-05-31 09:24:32 +02:00
parent 1b1b568bb0
commit 1775929ced
10 changed files with 80 additions and 43 deletions
+6 -6
View File
@@ -4,18 +4,18 @@ namespace Maze.Utlis;
internal class MazeParser
{
public T[,] FromFile<T>(string filePath, out T startNode) where T : BaseMazeNode<T>, new()
public MazeNode[,] FromFile(string filePath, out MazeNode startNode)
{
return FromString(File.ReadAllText(filePath), out startNode);
}
public T[,] FromString<T>(string input, out T startNode) where T : BaseMazeNode<T>, new()
public MazeNode[,] FromString(string input, out MazeNode startNode)
{
string[] lines = input.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
startNode = null!;
T? goalNode = null;
T[,] nodes = new T[lines.Length, lines[0].Length];
MazeNode? goalNode = null;
MazeNode[,] nodes = new MazeNode[lines.Length, lines[0].Length];
// Create nodes from input
for (int y = 0; y < lines.Length; y++)
@@ -24,7 +24,7 @@ internal class MazeParser
{
char c = lines[y][x];
T node = new T
MazeNode node = new MazeNode
{
Wall = c == '+',
Start = c == 'S',
@@ -69,7 +69,7 @@ internal class MazeParser
{
for (int x = 0; x < lines[0].Length; x++)
{
T node = nodes[y, x];
MazeNode node = nodes[y, x];
if (x < lines[0].Length - 1)
{
+4 -4
View File
@@ -6,13 +6,13 @@ namespace Maze.Utlis;
internal class MazePrinter
{
public void Print<T>(T[,] nodes, List<T>? solution = null, bool redrawAll = false) where T : BaseMazeNode<T>
public void Print(MazeNode[,] nodes, List<MazeNode>? solution = null, bool redrawAll = false)
{
for (int y = 0; y < nodes.GetLength(0); y++)
{
for (int x = 0; x < nodes.GetLength(1); x++)
{
T node = nodes[y, x];
MazeNode node = nodes[y, x];
if (!node.Updated && !redrawAll)
{
@@ -51,7 +51,7 @@ internal class MazePrinter
}
}
public void PrintColored<T>(T[,] nodes, List<T>? solution = null, bool redrawAll = false) where T : BaseMazeNode<T>
public void PrintColored(MazeNode[,] nodes, List<MazeNode>? solution = null, bool redrawAll = false)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.CursorVisible = false;
@@ -60,7 +60,7 @@ internal class MazePrinter
{
for (int x = 0; x < nodes.GetLength(1); x++)
{
T node = nodes[y, x];
MazeNode node = nodes[y, x];
if (!node.Updated && !redrawAll)
{
+19
View File
@@ -0,0 +1,19 @@
using Maze.Nodes;
using Maze.Solvers;
namespace Maze.Utlis;
internal class PathFinder
{
public IMazeSolver MazeSolver { get; set; }
public PathFinder(IMazeSolver mazeSolver)
{
MazeSolver = mazeSolver;
}
public List<MazeNode> FindPath(MazeNode node, MazeNode[,] maze, GraphicsMode graphicsMode = GraphicsMode.None)
{
return MazeSolver.Solve(node, maze, graphicsMode);
}
}