I want to create a function
that generating a maze map with my own pattern and resulting in an array
, for the function
parameter is the dimension of the maze map that the user can insert a dimension they want.
The value of array
from resulting that function only have 2 type value where 1
it's mean a Wall and 0
its a path.
Here's the maze map pattern that I want to convert to an array With a function
As you can see the dimension of that maze map is 15x15
.
So far I have tried to use the spiral matrix method by following the answer here but, I am very stuck on how to make the path Or differentiate the wall and the path from the result of spiral method that I use.
I've been searching for a couple of hours but what I got is only a solve maze path with an available maze map in array, not a create a maze map itself.
Any idea? At least please give me what specific algorithm for converting that maze into an array with a function not manually.
window.onload = function () { const S = prompt('Please enter the Maze Dimension you want', '15'); function spiral(length) { var upper = 0, lower = length - 1, left = 0, right = length - 1, i = 0, j = 0, result = Array.from({length}, (_) => []), value = 1; while (true) { if (upper++ > lower) break; for (; j < right; j++) result[i][j] = value++; if (right-- < left) break; for (; i < lower; i++) result[i][j] = value++; if (lower-- < upper) break; for (; j > left; j--) result[i][j] = value++; if (left++ > right) break; for (; i > upper; i--) result[i][j] = value++; } result[i][j] = value++; return result; } var target = document.getElementById('maze'), i = S; while (--i) target.innerHTML += spiral(i) .map((a) => a.map((v) => v.toString().padStart(2)).join(' ')) .join('\n') + '\n\n'; };
<pre id="maze"></pre>
没有评论:
发表评论