private SolidBrush GetColor(MazeRepresentaion maze, List <Coord> path, int i, int j) { if (path.Any(e => e.X == i && e.Y == j)) { return(new SolidBrush(Color.Violet)); } var c = maze.AtPos(i, j); if (c == 's') { return(new SolidBrush(Color.Cyan)); } else if (c == 'g') { return(new SolidBrush(Color.Cyan)); } else if (c == '1') { return(new SolidBrush(Color.Green)); } else if (c == '0') { return(new SolidBrush(Color.Red)); } return(new SolidBrush(Color.White)); }
private ConsoleColor GetColor(MazeRepresentaion maze, List <Coord> path, int i, int j) { if (path.Any(e => e.X == i && e.Y == j)) { return(ConsoleColor.Magenta); } var c = maze.AtPos(i, j); if (c == 's') { return(ConsoleColor.Cyan); } else if (c == 'g') { return(ConsoleColor.Cyan); } else if (c == '1') { return(ConsoleColor.Green); } else if (c == '0') { return(ConsoleColor.Red); } return(ConsoleColor.White); }
private static void RunMaze(MazeRepresentaion maze, string imageTitle) { var frontiterResult = new Traverser().FrontierCounter(maze); var path = new ShortestPathFinder().GetCoordinatesUsed(frontiterResult, maze); new Printer().PrintTraversalResult(frontiterResult, path, maze); new ImageGenerator().GenerateImage(frontiterResult, path, maze, imageTitle); Console.WriteLine(); }
public Dictionary <Tuple <int, int>, int> FrontierCounter(MazeRepresentaion maze) { var marked = new Dictionary <Tuple <int, int>, int>(); var moves = 0; var queue = new Queue <Coord>(); queue.Enqueue(maze.GetStart()); while (queue.Count > 0) { var current = queue.Dequeue(); if (marked.Any(e => e.Key.Item1 == current.X && e.Key.Item2 == current.Y)) { continue; } marked[new Tuple <int, int>(current.X, current.Y)] = moves++; var right = current.Right(); var down = current.Down(); var left = current.Left(); var up = current.Up(); if (maze.Legal(right) && maze.AtPos(right) != '0') { queue.Enqueue(right); } if (maze.Legal(down) && maze.AtPos(down) != '0') { queue.Enqueue(down); } if (maze.Legal(left) && maze.AtPos(left) != '0') { queue.Enqueue(left); } if (maze.Legal(up) && maze.AtPos(up) != '0') { queue.Enqueue(up); } } return(marked); }
public List <Coord> GetCoordinatesUsed(Dictionary <Tuple <int, int>, int> frontiterResult, MazeRepresentaion maze) { var coords = new List <Coord>(); var start = maze.GetStart(); coords.Add(GetLowest(frontiterResult, maze.GetEnd())); while (true) { var next = GetLowest(frontiterResult, coords.Last()); if (next.X == start.X && next.Y == start.Y) { break; } coords.Add(next); } return(coords); }
public void GenerateImage(Dictionary <Tuple <int, int>, int> result, List <Coord> path, MazeRepresentaion maze, string imageTitle) { Image resultImage = new Bitmap(maze.Cols * 100, maze.Rows * 100, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(resultImage); Font fontFroniterValue = new Font(new FontFamily("arial"), 28, FontStyle.Bold, GraphicsUnit.Pixel); Font fontContent = new Font(new FontFamily("arial"), 20, FontStyle.Bold, GraphicsUnit.Pixel); for (var i = 0; i < maze.Rows; i++) { for (var j = 0; j < maze.Cols; j++) { graphics.FillRectangle(GetColor(maze, path, i, j), new Rectangle(j * 100, i * 100, 100, 100)); var tuple = new Tuple <int, int>(i, j); if (result.ContainsKey(tuple)) { graphics.DrawString(result[tuple].ToString("D2"), fontFroniterValue, new SolidBrush(Color.Black), new PointF(j * 100 + 25, i * 100 + 25)); } else { graphics.DrawString("xx", fontFroniterValue, new SolidBrush(Color.Black), new PointF(j * 100 + 25, i * 100 + 25)); } graphics.DrawString(maze.AtPos(i, j).ToString(), fontContent, new SolidBrush(Color.Black), new PointF(j * 100, i * 100)); } } resultImage.Save(Path.Combine(Path.GetTempPath(), imageTitle), ImageFormat.Jpeg); graphics.Dispose(); fontFroniterValue.Dispose(); fontContent.Dispose(); resultImage.Dispose(); }
public void PrintTraversalResult(Dictionary <Tuple <int, int>, int> result, List <Coord> path, MazeRepresentaion maze) { for (var i = 0; i < maze.Rows; i++) { for (var j = 0; j < maze.Cols; j++) { Console.ForegroundColor = GetColor(maze, path, i, j); Console.Write($" {maze.AtPos(i, j)} "); } Console.WriteLine(); for (var j = 0; j < maze.Cols; j++) { Console.ForegroundColor = GetColor(maze, path, i, j); var tuple = new Tuple <int, int>(i, j); if (result.ContainsKey(tuple)) { Console.Write($" ({result[tuple].ToString("D2")}) "); } else { Console.Write($" (xx) "); } } Console.WriteLine("\n"); } Console.ForegroundColor = ConsoleColor.White; }