var round=2; // contatore dei round
var v=new Array(10); // array per le pedina
var i; // contatore dei for
var move; // la mossa dei giocatori
// inizializzo il vettore con le pedine ad 1 (1 = vuota, 0 = pedina player1, 4 = pedina player2)
for(i=0; i<9; i++) {
v[i]=1;
}
// funzione per stabilire se una mossa è possibile o meno
function check_move(num) {
var ok=false;
if(v[num]==1) {
ok=true;
}
return ok;
}
// mossa casuale
function random_move() {
do {
move = Math.floor(Math.random()*9);
} while(!check_move(move));
return move;
}
// le mosse di di fesa e attaco del computer (per fare tris o evitare di farselo fare)
function intermediate_move() {
if ((v[1]+v[2]==8 && v[0]==1)||(v[3]+v[6]==8 && v[0]==1)||(v[4]+v[8]==8 && v[0]==1)) return 0;
if ((v[0]+v[2]==8 && v[1]==1)||(v[4]+v[7]==8 && v[1]==1)) return 1;
if ((v[0]+v[1]==8 && v[2]==1)||(v[4]+v[6]==8 && v[2]==1)||(v[5]+v[8]==8 && v[2]==1)) return 2;
if ((v[0]+v[6]==8 && v[3]==1)||(v[4]+v[5]==8 && v[3]==1)) return 3;
if ((v[1]+v[7]==8 && v[4]==1)||(v[3]+v[5]==8 && v[4]==1)||(v[0]+v[8]==8 && v[4]==1)||(v[2]+v[6]==8 && v[4]==1)) return 4;
if ((v[2]+v[8]==8 && v[5]==1)||(v[3]+v[4]==8 && v[5]==1)) return 5;
if ((v[0]+v[3]==8 && v[6]==1)||(v[7]+v[8]==8 && v[6]==1)||(v[2]+v[4]==8 && v[6]==1)) return 6;
if ((v[1]+v[4]==8 && v[7]==1)||(v[6]+v[8]==8 && v[7]==1)) return 7;
if ((v[0]+v[4]==8 && v[8]==1)||(v[2]+v[5]==8 && v[8]==1)||(v[6]+v[7]==8 && v[8]==1)) return 8;
if ((v[1]+v[2]==0 && v[0]==1)||(v[3]+v[6]==0 && v[0]==1)||(v[4]+v[8]==0 && v[0]==1)) return 0;
if ((v[0]+v[2]==0 && v[1]==1)||(v[4]+v[7]==0 && v[1]==1)) return 1;
if ((v[0]+v[1]==0 && v[2]==1)||(v[4]+v[6]==0 && v[2]==1)||(v[5]+v[8]==0 && v[2]==1)) return 2;
if ((v[0]+v[6]==0 && v[3]==1)||(v[4]+v[5]==0 && v[3]==1)) return 3;
if ((v[1]+v[7]==0 && v[4]==1)||(v[3]+v[5]==0 && v[4]==1)||(v[0]+v[8]==0 && v[4]==1)||(v[2]+v[6]==0 && v[4]==1)) return 4;
if ((v[2]+v[8]==0 && v[5]==1)||(v[3]+v[4]==0 && v[5]==1)) return 5;
if ((v[0]+v[3]==0 && v[6]==1)||(v[7]+v[8]==0 && v[6]==1)||(v[2]+v[4]==0 && v[6]==1)) return 6;
if ((v[1]+v[4]==0 && v[7]==1)||(v[6]+v[8]==0 && v[7]==1)) return 7;
if ((v[0]+v[4]==0 && v[8]==1)||(v[2]+v[5]==0 && v[8]==1)||(v[6]+v[7]==0 && v[8]==1)) return 8;
return random_move();
}
// prima mossa del computer
function first_move() {
if(v[4]==1) return 4;
else {
do {
move = Math.floor(Math.random()*5);
} while(move == 2);
move = move * 2;
return move;
}
}
// seconda mossa del computer
function second_move()
{
if (v[4]==4) {
if (v[0]+v[8]==0 || v[2]+v[6]==0) {
move = Math.floor(Math.random()*4);
move *= 2; move++;
return move;
}
if (v[1]==0 && (v[6]==0 || v[8]==0)) {
do { move = Math.floor(Math.random()*6); } while(move==1 || move==4);
return move;
}
if (v[7]==0 && (v[0]==0 || v[2]==0)) {
do { move = Math.floor(Math.random()*6); } while(move==1 || move==4);
move = move + 3;
return move;
}
if (v[3]==0 && (v[2]==0 || v[8]==0)) {
do { move = Math.floor(Math.random()*8); } while(move>=2 && move<=5);
return move;
}
if (v[5]==0 && (v[0]==0 || v[6]==0)) {
do { move = Math.floor(Math.random()*8); } while(move>=2 && move<=5);
move++;
return move;
}
if(v[1]+v[3]==0) return 0;
if(v[1]+v[5]==0) return 2;
if(v[5]+v[7]==0) return 8;
if(v[7]+v[3]==0) return 6;
}
else {
if((v[0]==4 && v[8]==0) || (v[8]==4 && v[0]==0)) {
move = Math.floor(Math.random()*2);
move = move*4; move = move+2;
return move;
}
if((v[2]==4 && v[6]==0) || (v[6]==4 && v[2]==0)) {
move = Math.floor(Math.random()*2);
move = move * 8;
return move;
}
}
return intermediate_move();
}
// procedura che indirizza il computer a fare la mossa
function computer_move()
{
if (round == 3) {
return first_move();
}
if (round == 5) {
return second_move();
}
return intermediate_move();
}
// funzione per disabilitare la griglia di gioco a fone partita
function disabilita() {
for(i=1; i<=9; i++) {
document.getElementById("button"+i).disabled=true;
}
}
// funzione per stabilire se si ha vinto
function win(char) {
var ok=false;
if(v[0]+v[1]+v[2]==12 || v[0]+v[1]+v[2]==0) {
ok=true;
}
if(v[3]+v[4]+v[5]==12 || v[3]+v[4]+v[5]==0) {
ok=true;
}
if(v[6]+v[7]+v[8]==12 || v[6]+v[7]+v[8]==0) {
ok=true;
}
if(v[0]+v[3]+v[6]==12 || v[0]+v[3]+v[6]==0) {
ok=true;
}
if(v[1]+v[4]+v[7]==12 || v[1]+v[4]+v[7]==0) {
ok=true;
}
if(v[2]+v[5]+v[8]==12 || v[2]+v[5]+v[8]==0) {
ok=true;
}
if(v[0]+v[4]+v[8]==12 || v[0]+v[4]+v[8]==0) {
ok=true;
}
if(v[2]+v[4]+v[6]==12 || v[2]+v[4]+v[6]==0) {
ok=true;
}
if(ok) {
testo.value="Giocatore "+char+" ha vinto";
disabilita();
}
else {
if(round==11) {
testo.value="Pareggio";
disabilita();
}
}
}
// funzione che gestisce la partita
function print_move(a) {
var h;
var char;
if(round%2==0) char="X";
else char="O";
if(check_move(a-1)) {
v[a-1]=(round%2)*4;
document.getElementById("button"+a).value=char;
round++;
win(char);
if(round!=11) {
h = computer_move();
v[h]=(round%2)*4;
h++;
document.getElementById("button"+h).value="O";
round++;
win("O");
}
}
}
// funzione che azzera i contatori in caso di rivincita
function ripeti() {
testo.value=" ";
for(i=1; i<10; i++) {
round=2;
v[i-1]=1;
document.getElementById("button"+i).disabled=false;
document.getElementById("button"+i).value="";
}
}