Browse code

Handle mazes of any size

Robert Cranston authored on 21/05/2025 01:01:36
Showing 2 changed files

... ...
@@ -7,7 +7,7 @@ Features:
7 7
 -   Printing
8 8
 -   Algorithms
9 9
     -   Hunt And Kill
10
-        -   Handles only odd-sized mazes
10
+        -   Handles mazes of any size
11 11
         -   Random with seed
12 12
         -   Fairly naive
13 13
 
... ...
@@ -18,29 +18,29 @@ Features:
18 18
 ```
19 19
 $ make
20 20
 $ ./maze
21
-██  ██████████████████████████████████████████████████████████████████████████
22
-██  ██                          ██              ██      ██          ██      ██
23
-██  ██  ██  ██████████████  ██████  ██  ██████  ██  ██  ██  ██  ██  ██  ██  ██
24
-██      ██  ██          ██  ██      ██      ██      ██      ██  ██  ██  ██  ██
25
-██  ██████  ██  ██  ██  ██████  ██████  ██  ██  ██████████████  ██  ██  ██  ██
26
-██      ██      ██  ██      ██      ██  ██  ██          ██      ██      ██  ██
27
-██  ██  ██████████  ██  ██  ██████  ██  ██  ██████████████  ██████████  ██  ██
28
-██  ██      ██      ██  ██      ██  ██  ██              ██  ██      ██  ██  ██
29
-██  ██████  ██  ██████████████  ██  ██  ██████████████  ██████  ██  ██████  ██
30
-██  ██      ██              ██      ██  ██              ██      ██          ██
31
-██████  ██  ██████████████  ██████████████  ██  ██  ██████  ██████████████████
32
-██      ██  ██      ██      ██              ██  ██  ██  ██                  ██
33
-██  ██████  ██  ██████  ██  ██  ██████████████  ██  ██  ██  ██  ██████████  ██
34
-██  ██      ██          ██  ██      ██          ██      ██  ██  ██      ██  ██
35
-██  ██  ██████████████  ██  ██████  ██████████████████  ██  ██  ██  ██████  ██
36
-██  ██  ██          ██  ██  ██      ██              ██  ██  ██  ██  ██      ██
37
-██  ██  ██  ██  ██  ██  ██  ██  ██████  ██  ██████  ██  ██  ██████  ██  ██████
38
-██  ██  ██  ██  ██  ██  ██  ██  ██      ██      ██  ██  ██      ██  ██      ██
39
-██  ██████  ██  ██  ██████  ██  ██  ██████████████  ██  ██████  ██  ██████  ██
40
-██  ██      ██  ██  ██      ██  ██  ██      ██      ██      ██  ██      ██  ██
41
-██  ██  ██████████  ██  ██████  ██  ██  ██  ██  ██████████  ██  ██████  ██  ██
42
-██      ██          ██      ██      ██  ██      ██          ██      ██      ██
43
-██████████████████████████████████████████████████████████████████████████  ██
21
+██  ████████████████████████████████████████████████████████████████████████████
22
+██  ██                          ██              ██      ██          ██      ████
23
+██  ██  ██  ██████████████  ██████  ██  ██████  ██  ██  ██  ██  ██  ██  ██  ████
24
+██      ██  ██          ██  ██      ██      ██      ██      ██  ██  ██  ██  ████
25
+██  ██████  ██  ██  ██  ██████  ██████  ██  ██  ██████████████  ██  ██  ██  ████
26
+██      ██      ██  ██      ██      ██  ██  ██          ██      ██      ██  ████
27
+██  ██  ██████████  ██  ██  ██████  ██  ██  ██████████████  ██████████  ██  ████
28
+██  ██      ██      ██  ██      ██  ██  ██              ██  ██      ██  ██  ████
29
+██  ██████  ██  ██████████████  ██  ██  ██████████████  ██████  ██  ██████  ████
30
+██  ██      ██              ██      ██  ██              ██      ██          ████
31
+██████  ██  ██████████████  ██████████████  ██  ██  ██████  ████████████████████
32
+██      ██  ██      ██      ██              ██  ██  ██  ██                  ████
33
+██  ██████  ██  ██████  ██  ██  ██████████████  ██  ██  ██  ██  ██████████  ████
34
+██  ██      ██          ██  ██      ██          ██      ██  ██  ██      ██  ████
35
+██  ██  ██████████████  ██  ██████  ██████████████████  ██  ██  ██  ██████  ████
36
+██  ██  ██          ██  ██  ██      ██              ██  ██  ██  ██  ██      ████
37
+██  ██  ██  ██  ██  ██  ██  ██  ██████  ██  ██████  ██  ██  ██████  ██  ████████
38
+██  ██  ██  ██  ██  ██  ██  ██  ██      ██      ██  ██  ██      ██  ██      ████
39
+██  ██████  ██  ██  ██████  ██  ██  ██████████████  ██  ██████  ██  ██████  ████
40
+██  ██      ██  ██  ██      ██  ██  ██      ██      ██      ██  ██      ██  ████
41
+██  ██  ██████████  ██  ██████  ██  ██  ██  ██  ██████████  ██  ██████  ██  ████
42
+██      ██          ██      ██      ██  ██      ██          ██      ██      ████
43
+██████████████████████████████████████████████████████████████████████████  ████
44 44
 ```
45 45
 
46 46
 <style>#example ~ pre { line-height: 1; }</style>
... ...
@@ -7,6 +7,7 @@ struct Maze
7 7
     enum Cell
8 8
     {
9 9
         INVALID,
10
+        EDGE,
10 11
         WALL,
11 12
         PATH,
12 13
     };
... ...
@@ -15,7 +16,14 @@ struct Maze
15 16
         w{w},
16 17
         h{h},
17 18
         cells(w * h, initial)
18
-    {}
19
+    {
20
+        for (auto y = 0; y < h; y += h-1)
21
+        for (auto x = 0; x < w; x += 1)
22
+            cells[w * y + x] = EDGE;
23
+        for (auto y = 0; y < h; y += 1)
24
+        for (auto x = 0; x < w; x += w-1)
25
+            cells[w * y + x] = EDGE;
26
+    }
19 27
     Cell & operator ()(int x, int y)
20 28
     {
21 29
         static auto invalid = INVALID; // Yes, naughty.
... ...
@@ -84,13 +92,14 @@ Maze hunt_and_kill(int w, int h, unsigned seed)
84 92
                     }
85 93
     }
86 94
     // Exit.
87
-    maze(w-2, h-1) = Maze::PATH;
95
+    w -= (w + 1) % 2;
96
+    maze(w-2, h-2) = maze(w-2, h-1) = Maze::PATH;
88 97
     return maze;
89 98
 }
90 99
 
91 100
 int main()
92 101
 {
93 102
     auto seed = 0;
94
-    auto maze = hunt_and_kill(80/2-1, 24-1, seed);
103
+    auto maze = hunt_and_kill(80/2, 24-1, seed);
95 104
     maze.print();
96 105
 }