a.f=a.fillRect;a.s=a.strokeRect;lv=0;ls=["4113311325022312141203020202","00032112501303032412331345023202","211230135112531214122402250245020202"];c.width=c.height=321;p=53;b=[];function v(){l=ls[lv];ll=l.length-1;h=ll/4|0;for(i=0;i<ll;i++)b[i/4]={x:l[i]|0,y:l[++i]|0,d:l[++i]|0,l:l[++i]|0};g=s=b[h];r()}function r(){a.fillStyle="#3BF";a.f(0,0,321,321);for(i in b){z=b[i];a.fillStyle=g==z?'#FC6':'#05A';x=z.x*p+3;y=z.y*p+3;j=p*(z.l-1);w=50+(1-z.d)*j;j=50+j*z.d;a.f(x,y,w,j);if(s==z)a.s(x,y,w,j)}}addEventListener("keydown",function(e){k=e.keyCode;j=k%2;if(k>36&&k<41&&j==1-s.d){x=j*(k-38);y=(1-j)*(k-39);m=s.x+x;n=s.y+y;t=s.l-1;m+=x>0?t:0;n+=y>0?t:0;for(o in b){w=b[o];if(w!=s)for(u=0;u<w.l;u++)if(m<0||n<0||m>5||n>5||w.y+u*w.d==n&&w.x+u*(1-w.d)==m)return}s.x+=x;s.y+=y}r();if(g.x>3){if(++lv<3)v();else{alert("River Forded!")}}},!1);addEventListener("click",function(e){x=(e.pageX-c.offsetLeft)/p|0;y=(e.pageY-c.offsetTop)/p|0;for(i=0;o=b[i],t=o.l-1;i++)if(x>=o.x&&y>=o.y&&x<o.x+t*(1-o.d)+1&&y<o.y+t*o.d+1){s=o;r()}},!1);v()
//GLOBALS
a.f=a.fillRect; // REASSIGN CANVAS DRAWING FUNCTIONS TO SHORTER NAMES
a.s=a.strokeRect;
lv=0; // LEVEL DATA, 4 #s per block, last block is the wagon block.
ls=["4113311325022312141203020202","00032112501303032412331345023202","211230135112531214122402250245020202"];
c.width=c.height=321;
p=53;
b=[];
//LOAD LEVEL FROM STRING
function v(){
l=ls[lv];
ll=l.length-1;
h=ll/4|0;
for(i=0;i<ll;i++)
b[i/4]={x:l[i]|0,y:l[++i]|0,d:l[++i]|0,l:l[++i]|0}; //BLOCK OBJECT x,y,direction,length
g=s=b[h];
r()
}
//DRAW BOARD ONTO CANVAS
function r(){
a.fillStyle="#3BF";
a.f(0,0,321,321);
for(i in b){
z=b[i];
a.fillStyle=g==z?'#FC6':'#05A'; //WAGON BLOCK vs NORMAL BLOCK FILL
x=z.x*p+3;
y=z.y*p+3;
j=p*(z.l-1);
w=50+(1-z.d)*j;
j=50+j*z.d;
a.f(x,y,w,j);
if(s==z) //SELECTED BLOCK GETS A BORDER
a.s(x,y,w,j)
}
}
//KEYPRESSES TO MOVE BLOCKS
addEventListener("keydown",
function(e){
k=e.keyCode;
j=k%2;
if(k>36&&k<41&&j==1-s.d){ //ARROW KEY AND IN DIRECTION BLOCK CAN MOVE
//CALCULATE ADJUSTMENTS
x=j*(k-38);
y=(1-j)*(k-39);
m=s.x+x; //TEMP POSITIONS
n=s.y+y;
t=s.l-1;
m+=x>0?t:0;
n+=y>0?t:0;
//COLLISON DETECTION
for(o in b) {
w=b[o];
if(w!=s)
for(u=0;u<w.l;u++)
if(m<0||n<0||m>5||n>5 // BOUNDS - BUG FIX'D
||w.y+u*w.d==n&&w.x+u*(1-w.d)==m) // BLOCK INTERSECTION
return // RETURN IF COLLIDED SO WE DON'T...
}
s.x+=x; //...APPLY ADJUSTMENTS
s.y+=y
}
r();
if(g.x>3) { //IF THE WAGON BLOCK IS TOUCHING THE RIGHT
if(++lv<3)
v();
else {
alert("River Forded!")
}
}
},!1);
// CLICK BLOCK SELECTION
addEventListener("click",
function(e){
// WHICH GRID ARE WE OVER
x=(e.pageX-c.offsetLeft)/p|0;
y=(e.pageY-c.offsetTop)/p|0;
// CHECK BLOCKS FOR BEING OVER IT
for(i=0;o=b[i],t=o.l-1;i++)
if(x>=o.x&&y>=o.y&&x<o.x+t*(1-o.d)+1&&y<o.y+t*o.d+1) {
s=o; // CHANGE SELECTION
r() // REDRAW
}
},!1);
v(); //DO IT!