generate-maze
Version:
Maze generator using a JavaScript implementation of Eller's Algorithm, as described here: http://www.neocomputer.org/projects/eller.html
3 lines (2 loc) • 2.21 kB
JavaScript
function t(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function e(e){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?t(Object(o),!0).forEach(function(t){r(e,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))})}return e}function r(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function n(t){return t.slice(0,-1)}function o(t,e=0){return e?Array.from(Array(e-t).keys()).map(e=>e+t):Array.from(Array(t).keys())}function c(t){return[...new Set(t)]}function u(t,e){const r=c(t.map(t=>t.set)).filter(Boolean),n=(u=o(1,t.length+1),f=r,[u,f].reduce((t,e)=>t.filter(t=>!e.includes(t)))).sort(()=>.5-e());var u,f;t.filter(t=>!t.set).forEach((t,e)=>t.set=n[e])}function f(t,e,r=.5){n(t).forEach((n,o)=>{const c=t[o+1],u=n.set!==c.set,f=e()<=r;u&&f&&(function(t,e,r){t.forEach(t=>{t.set===e&&(t.set=r)})}(t,c.set,n.set),n.right=!1,c.left=!1)})}module.exports=function(t=8,r=t,i=!0,s=1){const l=function(t){return function(){let e=t+=1831565813;return e=Math.imul(e^e>>>15,1|e),e^=e+Math.imul(e^e>>>7,61|e),((e^e>>>14)>>>0)/4294967296}}(s),a=[],p=o(t);for(let e=0;e<r;e+=1){const n=p.map(n=>({x:n,y:e,top:i||e>0,left:i||n>0,bottom:i||e<r-1,right:i||n<t-1}));a.push(n)}n(a).forEach((t,r)=>{u(t,l),f(t,l),function(t,r,n){const o=Object.values(function(t,r){let n=c(t.map(t=>t.set)).reduce((t,r)=>e(e({},t),{},{[r]:[]}),{});return t.forEach(t=>n[t.set].push(t)),n}(t)),{ceil:u}=Math;o.forEach(t=>{(function(t,e,r){e=null==e?1:e;const n=null==t?0:t.length;if(!n||e<1)return[];e=e>n?n:e;let o=-1;const c=n-1,u=[...t];for(;++o<e;){const t=o+Math.floor(r()*(c-o+1)),e=u[t];u[t]=u[o],u[o]=e}return u.slice(0,e)})(t,u(n()*t.length),n).forEach(t=>{if(t){const e=r[t.x];t.bottom=!1,e.top=!1,e.set=t.set}})})}(t,a[r+1],l)});const h=(b=a)[b.length-1];var b;return u(h,l),f(h,l,1),a};
//# sourceMappingURL=generate-maze.cjs.map