//GenerateMaze() called when there isn't a existing grid public void GenerateGrid() { Random random = new Random(); int randomNum; for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { //Line used to print a boarder around the maze if (i == 0 || i == grid.GetLength(0) - 1 || j == 0 || j == grid.GetLength(1) - 1) { GridSquare temp = new GridSquare(ConsoleColor.Red, "Obstacle"); grid[i, j] = temp; continue; } randomNum = random.Next(1, 100); if (randomNum > 75)//25 percent chance to spawn a obstacle { GridSquare temp = new GridSquare(ConsoleColor.Red, "Obstacle"); grid[i, j] = temp; } else { GridSquare temp = new GridSquare(ConsoleColor.Yellow, "Walkable"); grid[i, j] = temp; } } } }
//GenerateMaze(nodeGrid) overload used to generate a node grid based on visual grid public void GenerateGrid(Node[,] nodeGrid, GridSquare[,] _grid) { Console.ResetColor(); Console.Clear(); grid = _grid; for (int i = 0; i < _grid.GetLength(0); i++) { for (int j = 0; j < _grid.GetLength(1); j++) { GridSquare tempGrid = _grid[i, j]; Node tempNode = new Node(tempGrid.type, i, j); nodeGrid[i, j] = tempNode; } } }
//Used to draw a path in the console public void VisualisePath(List <Node> path) { if (path == null) { Console.WriteLine("No path found between start node and goal node"); return; } foreach (Node n in path) { GridSquare temp = grid[n.posI, n.posJ]; temp.colour = ConsoleColor.Yellow; temp.type = "path"; grid[n.posI, n.posJ] = temp; } GenerateGrid(grid); }
public GridSquare[,] generateHeightMapFromImage() { //Reference a bitmap image, @ + a string of it's location Bitmap img = new Bitmap(@"C:\Users\Noa_t\source\repos\AstarAlgorithm\BitmapAltitude.bmp"); //Bitmap img = new Bitmap(@"C:\Users\Noa_t\source\repos\AstarAlgorithm\MyMaze.bmp"); //Generate a grid to represent the maze in the console GridSquare[,] grid = new GridSquare[img.Height, img.Width]; //Depending on the colours of a pixel do different things. for (int i = 0; i < img.Height; i++) { for (int j = 0; j < img.Width; j++) { Color pixel = img.GetPixel(j, i); //If the pixel RGB = black, add it as a obstacle in the array if (pixel.R == Color.Black.R && pixel.B == Color.Black.B && pixel.G == Color.Black.G) { GridSquare temp = new GridSquare(ConsoleColor.Black, "Obstacle"); grid[i, j] = temp; } //If the pixel RGB = blue, add it as a goal in the array else if (pixel.R == Color.Blue.R && pixel.B == Color.Blue.B && pixel.G == Color.Blue.G) { GridSquare temp = new GridSquare(ConsoleColor.Blue, "Goal"); grid[i, j] = temp; } //If the pixel RGB = green, add it as a start in the array else if (pixel.R == Color.Green.R && pixel.B == Color.Green.B && pixel.G == 255) { GridSquare temp = new GridSquare(ConsoleColor.DarkGreen, "Start"); grid[i, j] = temp; } //If the pixel RGB = yellow, add it as a walkable in the array else { GridSquare temp = new GridSquare(ConsoleColor.White, "Walkable"); grid[i, j] = temp; } } } return(grid); }
public void generateNodeGraph(GridSquare[,] _grid) { grid = _grid; nodes = new List <Node>(); for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { //Skip current square if it = obstacle if (grid[i, j].type == "Obstacle") { continue; } else if (grid[i, j].type == "Walkable") { //Place node at index based on the combination of it's neighbours if (ShouldNodeBePlaced(i, j) == true) { Node tempNode = new Node(grid[i, j].type, i, j); nodes.Add(tempNode); grid[i, j] = new GridSquare(ConsoleColor.Gray, "Walkable"); } else { continue; } } //if not obstacle or walkable AKA starting and goal square, place a node. else { Node tempNode = new Node(grid[i, j].type, i, j); nodes.Add(tempNode); } } } }
//Prints the grid squares void PrintSquare(GridSquare gridSquare) { Console.BackgroundColor = gridSquare.colour; Console.Write(" "); //Console.ResetColor(); }