private bool GetNextSquare(NaiveMazeImage maze, Tuple <int, int> currentSquare, bool[,] visitedSquares, out Tuple <int, int> nextSquare) { //Move Up if (currentSquare.Item2 - 1 > 0 && maze.Points[currentSquare.Item1][currentSquare.Item2 - 1] && !visitedSquares[currentSquare.Item1, currentSquare.Item2 - 1]) { nextSquare = new Tuple <int, int>(currentSquare.Item1, currentSquare.Item2 - 1); return(true); } //Move Right if (currentSquare.Item1 + 1 < maze.MazeWidth && maze.Points[currentSquare.Item1 + 1][currentSquare.Item2] && !visitedSquares[currentSquare.Item1 + 1, currentSquare.Item2]) { nextSquare = new Tuple <int, int>(currentSquare.Item1 + 1, currentSquare.Item2); return(true); } //Move Down if (currentSquare.Item2 + 1 < maze.MazeHeight && maze.Points[currentSquare.Item1][currentSquare.Item2 + 1] && !visitedSquares[currentSquare.Item1, currentSquare.Item2 + 1]) { nextSquare = new Tuple <int, int>(currentSquare.Item1, currentSquare.Item2 + 1); return(true); } //Move Left if (currentSquare.Item1 - 1 > 0 && maze.Points[currentSquare.Item1 - 1][currentSquare.Item2] && !visitedSquares[currentSquare.Item1 - 1, currentSquare.Item2]) { nextSquare = new Tuple <int, int>(currentSquare.Item1 - 1, currentSquare.Item2); return(true); } nextSquare = null; return(false); }
public NaiveMazeImage CreateNaiveMaze(string filePath) { var newMaze = new NaiveMazeImage(); var img = new Bitmap(filePath); newMaze.MazeHeight = img.Height; newMaze.MazeWidth = img.Width; for (var i = 0; i < img.Width; i++) { newMaze.Points.Add(new List <bool>()); for (var j = 0; j < img.Height; j++) { var pixel = img.GetPixel(i, j); if (IsPixelSpace(pixel)) { if (j == 0) { newMaze.StartPoint = new Tuple <int, int>(i, j); } if (j == img.Height - 1) { newMaze.EndPoint = new Tuple <int, int>(i, j); } newMaze.Points[i].Add(true); } else { newMaze.Points[i].Add(false); } } } return(newMaze); }
public bool NaiveSolve(NaiveMazeImage maze, out Stack <Tuple <int, int> > route) { var hasFoundRoute = false; var hasSeenAllSquares = false; var currentSquare = maze.StartPoint; var visitedSquares = new bool[maze.MazeHeight, maze.MazeWidth]; var currentRoute = new Stack <Tuple <int, int> >(); currentRoute.Push(maze.StartPoint); while (!hasFoundRoute && !hasSeenAllSquares) { var hasNextSquare = GetNextSquare(maze, currentSquare, visitedSquares, out Tuple <int, int> nextSquare); if (hasNextSquare) { if (currentRoute.Count > 0 && !currentRoute.First().Equals(currentSquare)) { currentRoute.Push(currentSquare); } currentRoute.Push(nextSquare); visitedSquares[nextSquare.Item1, nextSquare.Item2] = true; currentSquare = nextSquare; if (nextSquare.Equals(maze.EndPoint)) { hasFoundRoute = true; } } else { hasSeenAllSquares = CheckAllVisitedSquares(visitedSquares); currentSquare = currentRoute.Pop(); } } if (hasFoundRoute) { route = currentRoute; return(true); } route = null; return(false); }