function v(a,b,c){z=q[a][b],q[a][b]=c,r++;var d,e;d=b-(a+1)%2,e=a-1,a&&q[e][d]==z&&v(e,d,c),d++,a&&q[e][d]==z&&v(e,d,c),e+=2,d=b-e%2,q[e][d]==z&&v(e,d,c),d++,q[e][d]==z&&v(e,d,c)}function s(b){if(b>-1&b<8&b!=h&b!=f){if(b){f=b,r=0,v(p,0,f),j=n*n/2+1,m=r>j?"win":0,i=h;while(i==f|i==h)i=l()*7+1|0;r=0,v(0,p,h=i),m=!m&r>j?"fail":m,m&&(alert(m),s(0))}else{g=m?g:!g,m=0,n=g?34:50,o=g?18:12,p=n-1,q=[],i=-1;while(i++<n+3){j=-1,q[i]=[];while(j++<p)q[i][j]=i>n+2?j<8?j:0:i<n?l()*7+1|0:0}q[p][0]=q[n-2][0]=f,q[0][p]=q[1][p]=h}for(e in a)a[e[0]+(e[6]||"")]=a[e];a.fillStyle="#000",a.fc(0,0,Z,Z),i=-1;while(i++<n+3){j=-1;while(j++<p){if(!q[i][j])continue;x=(i%2*.75+j*1.5+1)*o,y=i*.75*o,a.fillStyle="#"+"f0f00ff000cffa00a0ccc".substr(q[i][j]*3-3,3),a.strokeStyle=j==f?"#090":"#900",a.save(),a.ta(x,y),a.rotate(.785),a.fc(0,0,o,o),a.lineWidth=4,i>n+2&&j==f|j==h&&a.sR(0,0,o,o),a.ta(-x,-y),a.restore()}}}}Z=c.width=1e3,c.height=560,f=1,h=6,l=Math.random,s(g=m=0),addEventListener("keyup",function(a){s(a.keyCode-48)},!1)
//manually compressed code, too sleepy to write comments right now
Z=c.width=1e3;
c.height=560;
f=1;
h=6;
l=Math.random;
s(g=m=0);
function s(k){
if (k>-1&k<8&k!=h&k!=f){
if(k){//1-6 keypress
f=k;
r=0;
v(p,0,f);
j=n*n/2+1;
m=(r>j?'win':0);
i=h;
while (i==f|i==h) {
i=(l()*7+1)|0;
}
r=0;
v(0,p,h=i);
m=(!m&r>j?'fail':m);
if(m) {alert(m);
s(0);
}
} else { //restarting the game
g=(m?g:!g);
m=0;
n=(g?34:50);
o=(g?18:12);
// n=50;
// o=12;
p=n-1;
q=[];
i=-1;
while(i++<n+3) {
j=-1;
q[i]=[];
while(j++<p) {
q[i][j]=(i>n+2?(j<8?j:0):(i<n?(l()*7+1)|0:0));
}
}
q[p][0]=q[n-2][0]=f;
q[0][p]=q[1][p]=h;
}
// a.clearRect(0, 0, 1e3, 1e3);
for(e in a)a[e[0]+(e[6]||'')]=a[e];
a.fillStyle ='#000';//"cleaning" the screen
a.fc(0,0, Z, Z);
i=-1;
while(i++<n+3) {//drawing
j=-1;
while(j++<p) {
if (!q[i][j]) continue;
x=(i%2*.75+j*1.5+1)*o;
y=i*.75*o;
a.fillStyle = '#'+'f0f00ff000cffa00a0ccc'.substr(q[i][j]*3-3,3);
a.strokeStyle = (j==f?'#090':'#900');
a.save();
a.ta(x, y);
a.rotate(.785);
a.fc(0,0, o, o);
a.lineWidth = 4;
if(i>n+2&&(j==f|j==h)) a.sR(0, 0, o, o);
/* a.fillStyle ='rgba(255,255,255,.3)'; // it is the part, that makes diamonds look fancy, but unfortunately eats bytes. Try to uncomment it
a.ba();
a.m(0, 0);
a.l(o, 0);
a.l(0, o);
a.l(0, 0);
a.l(o, o);
a.l(o, 0);
a.l(0, 0);
a.f();
a.ca(); */
a.ta(-x, -y);
a.restore();
}
}
}
}
function v(i,j,c){ //function that "paints" diamonds after color select
z=q[i][j];
q[i][j]=c;// c
r++;var y,x;
y=j-(i+1)%2;
x=i-1;
if (i&&q[x][y]==z)v(x,y,c);
y++;
if (i&&q[x][y]==z)v(x,y,c);
x+=2;
y=j-x%2;
if (q[x][y]==z)v(x,y,c);
y++;
if (q[x][y]==z)v(x,y,c);
}
addEventListener("keyup",function(e) {
s(e.keyCode-48);
},!1);