#include "Dijkstra.h" void Dijkstra::MazeSearch(char** maze, uint32_t mazeHeight, uint32_t mazeWidth, PathGenerator::Location startpoint, uint32_t exit_y, uint32_t exit_x) { std::vector distances; uint32_t distance_cnt = 3u; distances.push_back({ startpoint.row, startpoint.col}); bool new_distance = true; uint32_t y = 0u; uint32_t x = 0u; /* Walk away from the entrace and save distance (from entrance). */ while (new_distance) { new_distance = false; uint32_t distance_max = distances.size(); distance_cnt++; /* With the for loop, we can walk "parellel". */ for (uint32_t i = 0u; i < distance_max; i++) { y = distances[0u].y; x = distances[0u].x; maze[y][x] = distance_cnt; /* north is valid, save. */ if ((y > 0u) && (Maze_Char_Space == maze[y - 1u][x])) { distances.push_back({ y - 1u,x }); new_distance = true; } /* south is valid, save. */ if (((y + 1u) < (mazeHeight)) && (Maze_Char_Space == maze[y + 1u][x])) { distances.push_back({ y + 1u,x }); new_distance = true; } /* west is valid, save. */ if ((x > 0u) && (Maze_Char_Space == maze[y][x - 1u])) { distances.push_back({ y,x - 1u }); new_distance = true; } /* east is valid, save. */ if (((x + 1u) < mazeWidth) && (Maze_Char_Space == maze[y][x + 1u])) { distances.push_back({ y,x + 1u }); new_distance = true; } /* Stop at the end. */ if ((y == exit_y) && (x == exit_x)) { new_distance = false; break; } distances.erase(distances.begin()); } } /* Walk back from the exit to the entrance. */ y = exit_y; x = exit_x; distance_cnt = maze[y][x]; /* Loop until we aren't at the beginning. */ while (3u != distance_cnt) { /* Mark everything as a solution on the way. */ maze[y][x] = Maze_Char_Path; distance_cnt--; if ((y > 0u) && (distance_cnt == maze[y - 1u][x])) { y--; } else if (((y + 1u) < mazeHeight) && (distance_cnt == maze[y + 1u][x])) { y++; } else if ((x > 0u) && (distance_cnt == maze[y][x - 1u])) { x--; } else if (((x + 1u) < mazeWidth) && (distance_cnt == maze[y][x + 1u])) { x++; } else { /* Do nothing. */ } } for (uint32_t y = 0u; y < mazeHeight; y++) { for (uint32_t x = 0u; x < mazeWidth; x++) { if ((Maze_Char_Wall != maze[y][x]) && (Maze_Char_Path != maze[y][x])) { maze[y][x] = Maze_Char_Space; } } } }