Mercurial > public > maze-solver
changeset 10:e3addd013d36
refactor wall
author | Dennis <denniscmartin@protonmail.com> |
---|---|
date | Sun, 30 Oct 2022 17:36:42 +0100 |
parents | 22cf01362b45 |
children | 9f91739eb1a3 |
files | .github/resources/maze.png .github/resources/sol.png README.md src/algos.c |
diffstat | 4 files changed, 24 insertions(+), 115 deletions(-) [+] |
line wrap: on
line diff
--- a/README.md Sun Oct 30 17:03:24 2022 +0100 +++ b/README.md Sun Oct 30 17:36:42 2022 +0100 @@ -6,7 +6,7 @@ - Only PNG files - Mazes must be square - Walls must be black `rgb(0, 0, 0)` and path white `rgb(255, 255, 255)` -- Walls and path must be 1 px width +- Walls and path must be `1 px` width - The starting point must be at `(x: 0, y: 1)` - The ending point must be at `(x: width, y: height - 1)` @@ -15,6 +15,7 @@ 1. Build executable 2. Make a folder names `mazes` and place your mazes there. 3. Make a folder named `sols`. The script place the solutions here. +4. Run the program `./maze_solver maze1.png`  
--- a/src/algos.c Sun Oct 30 17:03:24 2022 +0100 +++ b/src/algos.c Sun Oct 30 17:36:42 2022 +0100 @@ -4,7 +4,7 @@ #include "algos.h" -int is_path(unsigned x, unsigned y, png_bytep* pRows) { +int is_path(int x, int y, png_bytep* pRows) { png_byte *pRow = pRows[y]; png_byte *pPixel = &pRow[x * 4]; @@ -19,9 +19,19 @@ } void wall_follower(png_bytep* pRows, unsigned int width) { - unsigned int x = 0; - unsigned int y = 1; - char direction = 'R'; + int x = 0; + int y = 1; + + enum { R, L, U, D } direction = R; + + static const struct { + int dx, dy, next; + } lut[][4] = { + [R] = {{+0, +1, D}, {+1, +0, R}, {+0, -1, U}, {-1, +0, L}}, + [L] = {{+0, -1, U}, {-1, +0, L}, {+0, +1, D}, {+1, +0, R}}, + [U] = {{+1, +0, R}, {+0, -1, U}, {-1, +0, L}, {+0, +1, D}}, + [D] = {{-1, +0, L}, {+0, +1, D}, {+1, +0, R}, {+0, -1, U}}, + }; is_path(x, y, pRows); @@ -31,118 +41,16 @@ break; } - switch (direction) { // NOLINT(hicpp-multiway-paths-covered) - case 'R': - - // Check if down position is white - if (is_path(x, y + 1, pRows)) { - ++y; - direction = 'D'; - } - - // Check if right position is white - else if (is_path(x + 1, y, pRows)) { - ++x; - direction = 'R'; - } - - // Check if up position is white - else if (is_path(x, y - 1, pRows)) { - --y; - direction = 'U'; - } - - // Turn 180 - else { - --x; - direction = 'L'; - } + for (int i = 0; i < 4; i++) { + int tx = x + lut[direction][i].dx; + int ty = y + lut[direction][i].dy; - break; - - case 'L': - - // Check if up position is white - if (is_path(x, y - 1, pRows)) { - --y; - direction = 'U'; - } - - // Check if left position is white - else if (is_path(x - 1, y, pRows)) { - --x; - direction = 'L'; - } - - // Check if down position is white - else if (is_path(x, y + 1, pRows)) { - ++y; - direction = 'D'; - } - - // Turn 180 - else { - ++x; - direction = 'R'; - } - + if (is_path(tx, ty, pRows)) { + x = tx; + y = ty; + direction = lut[direction][i].next; break; - - case 'U': - - // Check if right position is white - if (is_path(x + 1, y, pRows)) { - ++x; - direction = 'R'; - } - - // Check if up position is white - else if (is_path(x, y - 1, pRows)) { - --y; - direction = 'U'; - } - - // Check if left position is white - else if (is_path(x - 1, y, pRows)) { - --x; - direction = 'L'; - } - - // Turn 180 - else { - ++y; - direction = 'D'; - } - - break; - - case 'D': - - // Check if left position is white - if (is_path(x - 1, y, pRows)) { - --x; - direction = 'L'; - } - - // Check if down position is white - else if (is_path(x, y + 1, pRows)) { - ++y; - direction = 'D'; - } - - // Check if right position is white - else if (is_path(x + 1, y, pRows)) { - ++x; - direction = 'R'; - } - - // Turn 180 - else { - --y; - direction = 'U'; - } - - break; + } } } } \ No newline at end of file