Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
J-Trissone  - tris.txt

tris.txt

Caricato da: Inconcepibile
Scarica il programma completo

  1. var round=2;          // contatore dei round
  2. var v=new Array(10);  // array per le pedina
  3. var i;                // contatore dei for
  4. var move;             // la mossa dei giocatori
  5.  
  6. // inizializzo il vettore con le pedine ad 1 (1 = vuota, 0 = pedina player1, 4 = pedina player2)
  7. for(i=0; i<9; i++) {
  8.   v[i]=1;
  9.   }
  10.  
  11. // funzione per stabilire se una mossa è possibile o meno
  12. function check_move(num) {
  13.   var ok=false;
  14.   if(v[num]==1) {
  15.     ok=true;
  16.   }
  17.   return ok;
  18. }
  19.  
  20. // mossa casuale
  21. function random_move() {
  22.   do {
  23.     move = Math.floor(Math.random()*9);
  24.   } while(!check_move(move));
  25.   return move;
  26. }
  27.  
  28. // le mosse di di fesa e attaco del computer (per fare tris o evitare di farselo fare)
  29. function intermediate_move() {
  30.   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;
  31.   if ((v[0]+v[2]==8 && v[1]==1)||(v[4]+v[7]==8 && v[1]==1)) return 1;
  32.   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;
  33.   if ((v[0]+v[6]==8 && v[3]==1)||(v[4]+v[5]==8 && v[3]==1)) return 3;
  34.   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;
  35.   if ((v[2]+v[8]==8 && v[5]==1)||(v[3]+v[4]==8 && v[5]==1)) return 5;
  36.   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;
  37.   if ((v[1]+v[4]==8 && v[7]==1)||(v[6]+v[8]==8 && v[7]==1)) return 7;
  38.   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;
  39.  
  40.   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;
  41.   if ((v[0]+v[2]==0 && v[1]==1)||(v[4]+v[7]==0 && v[1]==1)) return 1;
  42.   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;
  43.   if ((v[0]+v[6]==0 && v[3]==1)||(v[4]+v[5]==0 && v[3]==1)) return 3;
  44.   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;
  45.   if ((v[2]+v[8]==0 && v[5]==1)||(v[3]+v[4]==0 && v[5]==1)) return 5;
  46.   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;
  47.   if ((v[1]+v[4]==0 && v[7]==1)||(v[6]+v[8]==0 && v[7]==1)) return 7;
  48.   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;
  49.    
  50.   return random_move();
  51. }
  52.  
  53. // prima mossa del computer
  54. function first_move() {
  55.   if(v[4]==1) return 4;
  56.   else {
  57.     do {
  58.       move = Math.floor(Math.random()*5);
  59.     } while(move == 2);
  60.     move = move * 2;
  61.     return move;
  62.   }
  63. }
  64.  
  65. // seconda mossa del computer
  66. function second_move()
  67. {
  68.   if (v[4]==4) {
  69.     if (v[0]+v[8]==0 || v[2]+v[6]==0) {
  70.       move = Math.floor(Math.random()*4);
  71.       move *= 2; move++;
  72.       return move;
  73.     }
  74.     if (v[1]==0 && (v[6]==0 || v[8]==0)) {
  75.       do { move = Math.floor(Math.random()*6); } while(move==1 || move==4);
  76.       return move;
  77.       }
  78.     if (v[7]==0 && (v[0]==0 || v[2]==0)) {
  79.       do { move = Math.floor(Math.random()*6); } while(move==1 || move==4);
  80.       move = move + 3;
  81.       return move;
  82.       }
  83.     if (v[3]==0 && (v[2]==0 || v[8]==0)) {
  84.       do { move = Math.floor(Math.random()*8); } while(move>=2 && move<=5);
  85.       return move;
  86.       }
  87.     if (v[5]==0 && (v[0]==0 || v[6]==0)) {
  88.       do { move = Math.floor(Math.random()*8); } while(move>=2 && move<=5);
  89.       move++;
  90.       return move;
  91.       }
  92.     if(v[1]+v[3]==0) return 0;
  93.     if(v[1]+v[5]==0) return 2;
  94.     if(v[5]+v[7]==0) return 8;
  95.     if(v[7]+v[3]==0) return 6;
  96.     }
  97.     else {
  98.     if((v[0]==4 && v[8]==0) || (v[8]==4 && v[0]==0)) {
  99.       move = Math.floor(Math.random()*2);
  100.       move = move*4; move = move+2;
  101.       return move;
  102.       }
  103.     if((v[2]==4 && v[6]==0) || (v[6]==4 && v[2]==0)) {
  104.       move = Math.floor(Math.random()*2);
  105.       move = move * 8;
  106.       return move;
  107.       }
  108.     }
  109.     return intermediate_move();
  110. }
  111.          
  112. // procedura che indirizza il computer a fare la mossa
  113. function computer_move()
  114. {
  115.   if (round == 3) {
  116.     return first_move();
  117.   }
  118.   if (round == 5) {
  119.     return second_move();
  120.   }
  121.   return intermediate_move();
  122. }
  123.  
  124. // funzione per disabilitare la griglia di gioco a fone partita
  125. function disabilita() {
  126.   for(i=1; i<=9; i++) {
  127.   document.getElementById("button"+i).disabled=true;
  128.   }
  129. }
  130.  
  131. // funzione per stabilire se si ha vinto
  132. function win(char) {
  133.   var ok=false;
  134.   if(v[0]+v[1]+v[2]==12 || v[0]+v[1]+v[2]==0) {
  135.     ok=true;
  136.   }
  137.   if(v[3]+v[4]+v[5]==12 || v[3]+v[4]+v[5]==0) {
  138.     ok=true;
  139.   }
  140.   if(v[6]+v[7]+v[8]==12 || v[6]+v[7]+v[8]==0) {
  141.     ok=true;
  142.   }
  143.   if(v[0]+v[3]+v[6]==12 || v[0]+v[3]+v[6]==0) {
  144.     ok=true;
  145.   }
  146.   if(v[1]+v[4]+v[7]==12 || v[1]+v[4]+v[7]==0) {
  147.     ok=true;
  148.   }
  149.   if(v[2]+v[5]+v[8]==12 || v[2]+v[5]+v[8]==0) {
  150.     ok=true;
  151.   }
  152.   if(v[0]+v[4]+v[8]==12 || v[0]+v[4]+v[8]==0) {
  153.     ok=true;
  154.   }
  155.   if(v[2]+v[4]+v[6]==12 || v[2]+v[4]+v[6]==0) {
  156.     ok=true;
  157.   }
  158.   if(ok) {
  159.     testo.value="Giocatore "+char+" ha vinto";
  160.     disabilita();
  161.   }
  162.   else {
  163.     if(round==11) {
  164.       testo.value="Pareggio";
  165.       disabilita();
  166.     }
  167.   }
  168. }
  169.  
  170. // funzione che gestisce la partita
  171. function print_move(a) {
  172.   var h;
  173.   var char;
  174.   if(round%2==0) char="X";
  175.   else char="O";
  176.   if(check_move(a-1)) {
  177.     v[a-1]=(round%2)*4;
  178.     document.getElementById("button"+a).value=char;
  179.     round++;
  180.     win(char);
  181.     if(round!=11) {
  182.       h = computer_move();
  183.       v[h]=(round%2)*4;
  184.       h++;
  185.       document.getElementById("button"+h).value="O";
  186.       round++;
  187.       win("O");
  188.     }
  189.   }
  190. }
  191.  
  192. // funzione che azzera i contatori in caso di rivincita
  193. function ripeti() {
  194.   testo.value=" ";
  195.   for(i=1; i<10; i++) {
  196.   round=2;
  197.   v[i-1]=1;
  198.   document.getElementById("button"+i).disabled=false;
  199.   document.getElementById("button"+i).value="";
  200.   }
  201. }