// JavaScript TicTacToe
// Copyright (C) Klaus Reimer <k@ailis.de>
// $Id: tictactoe.js 214 2007-07-05 08:12:48Z k $
function TicTacToe(S){this._k=TicTacToe.instances.length;
TicTacToe.instances.push(this);this._q=[0,0];this._C(S);this.init();
}TicTacToe.instances=[];TicTacToe._l=["Easy","Normal","Impossible"];
TicTacToe._T=TicTacToe.prototype;TicTacToe._T._k=null;TicTacToe._T._m=1;
TicTacToe._T._n=1;TicTacToe._T._o=0;TicTacToe._T._p=0;TicTacToe._T._q=null;
TicTacToe._T._r=null;TicTacToe._T._s=null;TicTacToe._T._t=null;
TicTacToe._T._u=false;TicTacToe._T._v=1000;TicTacToe._T._w=null;
TicTacToe._T._x=null;TicTacToe._T._y=null;TicTacToe._T._z=null;
TicTacToe._T._A=1;TicTacToe._T._B=1;
TicTacToe._T._C=function(S){var _,a,b,c,d,e,f,g,h,i;
_=document.createElement("div");_.className="ticTacToeBoard";this._r=_;
S.appendChild(_);a=[];for(e=0;e<3;e++){b=[];for(d=0;d<3;
d++){c=document.createElement("div");g=document.createElement("div");
c.appendChild(g);g.row=e;g.column=d;g.game=this;g.onclick=this._H;
_.appendChild(c);b.push(c);}a.push(b);}this._s=a;this._w=[];this._y=[];
f=document.createElement("div");f.className="ticTacToeScore";
i=document.createElement("span");i.className="ticTacToePlayer1";
i.appendChild(document.createTextNode("x: "));this._y.push(i);
g=document.createElement("span");g.className="ticTacToeScore1";
i.appendChild(g);this._w.push(g);f.appendChild(i);
f.appendChild(document.createTextNode(" "));
i=document.createElement("span");i.className="ticTacToePlayer2";
i.appendChild(document.createTextNode("o: "));this._y.push(i);
g=document.createElement("span");g.className="ticTacToeScore2";
i.appendChild(g);this._w.push(g);f.appendChild(i);_.appendChild(f);
this._x=[];f=document.createElement("div");f.className="ticTacToePlayers";
for(h=0;h<3;h++){g=document.createElement("span");
g.className="ticTacToePlayers"+h;
if(h==this._m)g.className+=" ticTacToePlayersActive";g._m=h;g.game=this;
g.appendChild(document.createTextNode(" "+h));
g.title="Restarts the game with "+h+" human players";g.onclick=this._F;
f.appendChild(g);this._x.push(g);}_.appendChild(f);this._z=[];
f=document.createElement("div");f.className="ticTacToeIQs";for(h=0;h<3;
h++){g=document.createElement("div");g.className="ticTacToeIQ"+h;
if(h<=this._B)g.className+=" ticTacToeIQActive";g._B=h;g.game=this;
g.title="Sets difficulty level to "+TicTacToe._l[h];g.onclick=this._G;
f.appendChild(g);this._z.push(g);}_.appendChild(f);this._D();};
TicTacToe._T._D=function(){var g,h;for(h=0;h<2;h++){g=this._w[h];
if(g.firstChild)g.removeChild(g.firstChild);
g.appendChild(document.createTextNode(this._q[h]));}};
TicTacToe._T._E=function(){var g,h;for(h=0;h<2;h++){g=this._y[h];
g.className="ticTacToePlayer"+(h+1);
if(h==this._n-1)g.className+=" ticTacToePlayerActive";}};
TicTacToe._T.init=function(U){var d,e,c;if(U&&U!=this._A)return;
this._t=[[0,0,0],[0,0,0],[0,0,0]];for(e=0;e<3;e++){for(d=0;d<3;
d++){c=this._s[e][d];c.className="ticTacToeField";
}}this._r.className="ticTacToeBoard";this._o=0;this._u=true;this._E();
if(this._n>this._m)this.ai();};
TicTacToe._T._F=function(){this.game.setPlayers(this._m);};
TicTacToe._T._G=function(){this.game.setDifficulty(this._B);};
TicTacToe._T.setDifficulty=function(V){var h,g;this._B=V;for(h=0;h<3;
h++){g=this._z[h];g.className="ticTacToeIQ"+h;
if(h<=V)g.className+=" ticTacToeIQActive";}};
TicTacToe._T.reset=function(){this._A++;this._q=[0,0];this._p=0;this._n=1;
this._D();this.init();};TicTacToe._T.setPlayers=function(W){var h,g;
this._m=W;for(h=0;h<3;h++){g=this._x[h];g.className="ticTacToePlayers"+h;
if(h==this._m)g.className+=" ticTacToePlayersActive";}this.reset();};
TicTacToe._T._H=function(){if(this.game._m){if(this.game._u){if(this.game._n<=this.game._m)this.game._I(this.column,this.row);
}else{this.game.init();}}else{this.game.setPlayers(1);}};
TicTacToe._T._I=function(d,e){var u;
if(!this._t[e][d]){this._t[e][d]=this._n;
this._s[e][d].className+=" ticTacToeField"+this._n;this._o++;this._O();
this._J();this._E();}};TicTacToe._T._J=function(){this._n=3-this._n;
if(this._u&&this._n>this._m){setTimeout("TicTacToe.instances["+this._k+"].ai("+this._A+")",this._v);
}};TicTacToe._T._K=function(Z,__){var v,w;w=this._t;if(Z<3){v=w[Z];
}else{Z-=3;if(Z<3){v=[w[0][Z],w[1][Z],w[2][Z]];}else{Z=(Z-3)*2;
v=[w[0][Z],w[1][1],w[2][2-Z]];}}return __?this._L(v):v;};
TicTacToe._T._L=function(v){return[v[0]?3-v[0]:0,v[1]?3-v[1]:0,v[2]?3-v[2]:0];
};TicTacToe._T._M=function(v,x){if(v[0]==x&&v[1]==x&&!v[2])return 2;
if(v[0]==x&&!v[1]&&v[2]==x)return 1;if(!v[0]&&v[1]==x&&v[2]==x)return 0;
return-1;};TicTacToe._T._N=function(v){var x;x=v[0];
if(x==v[1]&&x==v[2])return x;return 0;};
TicTacToe._T._O=function(){var h,v,A;for(h=0;h<8;h++){v=this._K(h);
A=this._N(v);if(A){this._Q(h);return this._R(A);}}if(this._o==9)this._R(0);
};TicTacToe._T._P=function(Z,O){var d,e;if(Z<3){e=Z;d=O;}else if(Z<6){e=O;
d=Z-3;}else if(Z==6){e=O;d=O;}else{e=O;d=2-O;}return{"x":d,"y":e};};
TicTacToe._T._Q=function(Z){var h,c;for(h=0;h<3;
h++){if(Z<3)c=this._s[Z][h];else if(Z<6)c=this._s[h][Z-3];
else if(Z==6)c=this._s[h][h];else c=this._s[h][2-h];
c.className+=" ticTacToeFieldWin";}};
TicTacToe._T._R=function(x){if(x)this._S(x);this._p++;
this._r.className+=" ticTacToeBoardEnd";this._u=false;
if(!this._m){setTimeout("TicTacToe.instances["+this._k+"].init("+this._A+")",2*this._v);
}};TicTacToe._T._S=function(x){this._q[x-1]++;this._D();};
TicTacToe._T.ai=function(U){var F,d,e,I,J,K,L,h,v,O,w,Q;
if(U&&U!=this._A)return;w=this._t;Q=3-this._n;F=[[3,2,3],[2,3,2],[3,2,3]];
if(this._B==2&&this._o==1)F[1][1]=4;for(h=0;h<8;h++){v=this._K(h);
O=this._M(v,Q);if(O>=0){L=this._P(h,O);F[L.y][L.x]=6;}O=this._M(v,this._n);
if(O>=0){L=this._P(h,O);F[L.y][L.x]=7;
}}if(this._B>0&&this._o==3){if(this._B==2||Math.random()*100>50){if((w[0][0]==Q&&w[2][2]==Q)||(w[2][0]==Q&&w[0][2]==Q)){F[0][1]=5;
F[2][1]=5;F[1][0]=5;F[1][2]=5;
}}if(this._B==2||Math.random()*100>50){if(w[0][1]==Q&&w[1][2]==Q)F[2][0]=1;
if(w[1][2]==Q&&w[2][1]==Q)F[0][0]=1;if(w[1][0]==Q&&w[2][1]==Q)F[0][2]=1;
if(w[0][1]==Q&&w[1][0]==Q)F[2][2]=1;
}if(this._B==2||Math.random()*100>75){var R=0;if(w[0][1]==Q)R++;
if(w[1][0]==Q)R++;if(w[1][2]==Q)R++;if(w[2][1]==Q)R++;
if(R==1){if(w[0][1]==Q){F[0][0]=5;F[0][2]=5;}else if(w[1][0]==Q){F[0][0]=5;
F[2][0]=5;}else if(w[1][2]==Q){F[0][2]=5;F[2][2]=5;
}else if(w[2][1]==Q){F[2][0]=5;F[2][2]=5;}}}}for(e=0;e<3;e++)for(d=0;d<3;
d++)if(w[e][d])F[e][d]=0;I=0;K=[];for(e=0;e<3;e++){for(d=0;d<3;
d++){J=F[e][d];if(J>I){I=J;K=[{"x":d,"y":e}];
}else if(J==I){K.push({"x":d,"y":e});
}}}L=K[parseInt(Math.random()*K.length)];this._I(L.x,L.y);};