protected static void WriteLine(string message) { Loggers.ForEach(logger => { logger.WriteLine(message); }); }
public override bool Solve() { // Will leave you with a boolean array (correctPath) // with the path indicated by true values. // If returns false, there is no solution to the maze. Loggers.ForEach(log => log.Log($"Starting maze solver at location: {StartPt.ToString()}")); return(RecursiveSolve(StartPt.X, StartPt.Y)); }
public void Disable() => Loggers.ForEach(c => c.Disable());
/// <summary> /// Recursively solves the given maze by starting at the specified location /// provided by integer X and Y arguments. /// </summary> /// <param name="x">X coordinate of the starting position.</param> /// <param name="y">Y coordinate of the starting position.</param> /// <returns>Success of the operation.</returns> private bool RecursiveSolve(int x, int y) { // If you reached the end foreach (var pt in endPoints) { if (x == pt.X && y == pt.Y) { correctPath[x, y] = true; Loggers.ForEach(log => log.Log($"Step to: x = {x.ToString()}, y = {y.ToString()}")); return(true); } } if (maze[x, y] == 1 || wasHere[x, y]) { return(false); } // If you are on a wall or already were here wasHere[x, y] = true; // Checks if not on left edge if (x != 0) { if (RecursiveSolve(x - 1, y)) { // Recalls method one to the left correctPath[x, y] = true; // Sets that path value to true; Loggers.ForEach(log => log.Log($"Step to: x = {x.ToString()}, y = {y.ToString()}")); return(true); } } // Checks if not on right edge if (x != Width - 1) { if (RecursiveSolve(x + 1, y)) { // Recalls method one to the right correctPath[x, y] = true; Loggers.ForEach(log => log.Log($"Step to: x = {x.ToString()}, y = {y.ToString()}")); return(true); } } // Checks if not on top edge if (y != 0) { if (RecursiveSolve(x, y - 1)) { // Recalls method one up correctPath[x, y] = true; Loggers.ForEach(log => log.Log($"Step to: x = {x.ToString()}, y = {y.ToString()}")); return(true); } } // Checks if not on bottom edge if (y != Height - 1) { if (RecursiveSolve(x, y + 1)) { // Recalls method one down correctPath[x, y] = true; Loggers.ForEach(log => log.Log($"Step to: x = {x.ToString()}, y = {y.ToString()}")); return(true); } } return(false); }