Generate and explore a random maze. Creation uses Prim's algorithm and exploration implements a simple wall follower.
b=25;s=b*b;var c=document.body.children[0];c.width=c.height=s+b;x=c.getContext("2d");x.fillRect(0,0,s,s);x.fillStyle='#fff';function L(S){return S.length}a=Math;f=a.floor;function C(A){i=f(a.random()*L(A));r=A[i];A.splice(i,1);return r}function D(c,j,J,k,K){x.strokeStyle='rgb(255,'+c+','+c+')';x.beginPath();x.moveTo(j,J);x.lineTo(k,K);x.stroke();c<s&&window.setTimeout(function(){D(c+b,j,J,k,K)},b)}m={'-1':0};p=[0];while(L(p)){if(!(m[v=C(p)]||v<0||v>=s)){X=v%b;Y=f(v/b);n=X!=0?[v-1]:[v];n[1]=X!=b-1?v+1:v;n[2]=Y!=0?v-b:v;n[3]=Y!=b-1?v+b:v;m[v]=0;x.fillRect(b*X+3,b*Y+3,V=b-6,V);for(t=[0,1,2,3];L(t);){y=C(t);if(m[n[y]]){t=[];U=(y<2)?6:V;x.fillRect(b*X+3+((y==1)?V:(y==0)?-6:0),b*Y+3+((y==3)?V:(y==2)?-6:0),U,b-U)}}m[v]=1;p=p.concat(n)}}x.lineWidth=5;x.lineCap='round';d=u=v=f(b/2);X=a.min;Y=a.abs;Z=a.max;window.setInterval(function(){U=(d%2)?(d-2)*b:0;V=(d%2)?0:(d-1)*b;p=x.getImageData(Z(0,X(u,u+U)),Z(0,X(v,v+V)),Y(U)+1,Y(V)+1).data;for(i=1,m=255;i<L(p);m=X(m,p[i]),i+=4){}d=(d+1+2*!m)%4;m&&D(5,u,v,u+=U,v+=V)},b);
Yj0yNTtzPWIqYjt2YXIgYz1kb2N1bWVudC5ib2R5LmNoaWxkcmVuWzBdO2Mud2lkdGg9Yy5oZWlnaHQ9cytiO3g9Yy5nZXRDb250ZXh0KCIyZCIpO3guZmlsbFJlY3QoMCwwLHMscyk7eC5maWxsU3R5bGU9JyNmZmYnO2Z1bmN0aW9uIEwoUyl7cmV0dXJuIFMubGVuZ3RofWE9TWF0aDtmPWEuZmxvb3I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tKSU0O20mJkQoNSx1LHYsdSs9VSx2Kz1WKX0sYik7
// Sizes.
b = 25;
s = b*b;
// Black out and initialize.
var c = document.body.children[0];
c.width=c.height=s+b;
x = c.getContext("2d");
x.fillRect(0,0,s,s);
x.fillStyle='#fff';
// Helpers:
// Get the length of something.
function L(S) {return S.length}
// Math.
a = Math;
// Floor.
f = a.floor;
// Pick a random element of an array, remove, and return.
function C(A) {
i = f(a.random()*L(A));
r = A[i];
A.splice(i,1);
return r;
}
// Draw a fading line.
function D(c,j,J,k,K) {
//window.console.log('D('+c+','+sx+','+sy+','+fx+','+fy+')');
x.strokeStyle='rgb(255,'+c+','+c+')';
x.beginPath();
x.moveTo(j,J);
x.lineTo(k,K);
x.stroke();
c<s && window.setTimeout(function(){D(c+b,j,J,k,K);},b);
}
// Make the Maze:
m = {'-1':0};
p = [0];
while(L(p)) {
if (!(m[v=C(p)] || v < 0 || v >= s)) {
X = v%b;
Y=f(v/b);
n=X!=0?[v-1]:[v];
n[1]=X!=b-1?v+1:v;
n[2]=Y!=0?v-b:v;
n[3]=Y!=b-1?v+b:v;
m[v]=0;
x.fillRect(b*X+3,b*Y+3,V=b-6,V);
// Choose the side to open for the cell. Randomly choose until we pick one
// that opens to an explored cell.
for(t=[0,1,2,3];L(t);){
y=C(t);
if(m[n[y]]) {
t=[];
U=(y<2)?6:V;
x.fillRect(b*X+3+((y==1)?V:(y==0)?-6:0),
b*Y+3+((y==3)?V:(y==2)?-6:0),
U,
b-U);
}
}
m[v]=1;
p=p.concat(n);
}
}
// Solve it.
x.lineWidth=5;
x.lineCap='round';
// d is direction, values are 0:up,1:right, 2:down, 3:left.
// u,v is the current position.
d=u=v=f(b/2);
X=a.min;Y=a.abs;Z=a.max;
window.setInterval(function() {
// Get the proposed delta.
U=(d%2)?(d-2)*b:0;
V=(d%2)?0:(d-1)*b;
// Check if the path is clear.
p = x.getImageData(Z(0,X(u,u+U)),Z(0,X(v,v+V)),Y(U)+1,Y(V)+1).data;
for(i=1,m=255;i<L(p);m=X(m,p[i]),i+=4) {}
// Move.
d=(d+1+2*!m)%4;
m && D(5,u,v,u+=U,v+=V);
},b);