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(k){if(k>-1&k<8&k!=h&k!=f){if(k){f=k,r=0,v(p,0,f),j=n*n/2+1,m=r>j?"win":0;do h++,h%=8;while(!h||h==f);r=0,v(0,p,h),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,d=n+3,q=[],i=-1;while(i++<d){j=-1,q[i]=[];while(j++<p)q[i][j]=i==d?j<8?j:0:i<n?Math.random()*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.fc(0,0,l,l),i=-1;while(i++<d){j=-1;while(j++<p){if(!q[i][j])continue;x=(i%2*.75+j*1.5+1)*o,y=i*.75*o,a.save(),a.ta(x,y),a[A]=j==f?"#090":j==h?"red":"#fff",i==d&&a.fx(j,0,0),a[A]="#"+"f0f00ff000cffa00a0ccc".substr(q[i][j]*3-3,3),a.rotate(.785),a.fc(0,0,o,o),a[A]="rgba(0,0,0,.1)";with(a)ba(),l(o,0),l(0,o),l(o,o),l(0,0),f(),ca(),ta(-x,-y),restore()}}}}l=c.height=c.width=1e3,A="fillStyle",f=1,h=6,s(g=m=0),addEventListener("keyup",function(a){s(a.keyCode-48)},!1)
//manually compressed code
/* global vars explanation:
a,b,c - preset vars
A - shortcut to fillStyle prop
f -player color
g - game mode (34x34 or 50X50)
h - enemy color
m - victory (victory text)
n - field side length
o - diamond side length
p = n-1 - to save couple of bytes
d = n+3 for the same reason
q - two-dimensional array (play field)
r - number of diamonds, that belong to player/enemy (after executing v() function)
s - main function (start/restart/keypress events)
v - recursive function, that "paints" diamonds to new color
i,j,k,l,x,y,z - some local vars, which function changes depending on context
*/
l=c.height=c.width=1e3;
A='fillStyle';
f=1;
h=6;
s(g=m=0);
function s(k){
if (k>-1&k<8&k!=h&k!=f){
if(k){//1-6 keypress
//player turn::
f=k;
r=0;
v(p,0,f);
j=n*n/2+1;
m=(r>j?'win':0);
//enemy turn::
do {
h++;
h%=8;
}
while (!h||h==f) //it turns out, that cycling through avaliable colors is better strategy than just picking random number
r=0;
v(0,p,h);
m=(!m&r>j?'fail':m);
if(m) {alert(m); //victory check
s(0);
}
} else { //restarting the game
//init global vars
g=(m?g:!g);
m=0;
n=(g?34:50);
o=(g?18:12);
p=n-1;
d=n+3;
q=[];
i=-1;
while(i++<d) {
j=-1;
q[i]=[];
while(j++<p) {
q[i][j]=(i==d?(j<8?j:0):(i<n?(Math.random()*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.fc(0,0, l, l);//"cleaning" the screen by drawing black rectangle
i=-1;
while(i++<d) {//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.save();
a.ta(x, y);
a[A] = (j==f?'#090':(j==h?'red':'#fff'));
if(i==d)a.fx(j,0,0);
a[A] = '#'+'f0f00ff000cffa00a0ccc'.substr(q[i][j]*3-3,3);
a.rotate(.785);///0.785an approximation of (45*PI/180)
a.fc(0,0, o, o);
a[A] ='rgba(0,0,0,.1)'; // it is the part, that makes diamonds look fancy:
with (a) {//-7
ba();
l(o, 0);
l(0, o);
l(o, o);
l(0, 0);
f();
ca();
ta(-x, -y);
restore();
}
}
}
}
}
function v(i,j,c){ //reccurent function that "paints" diamonds after color select
z=q[i][j];
q[i][j]=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);