Sunday, September 11, 2011

JAVASCRIPT TIC TAC TOE








<center>
<SCRIPT name = "JavaScript">
function variables(){
    t = 1;
    change = 1;   
    empty1 = -1;
    empty2 = -1;
    empty3 = -1;
    empty4 = -1;
    empty5 = -1;
    empty6 = -1;
    empty7 = -1;
    empty8 = -1;
    empty9 = -1;
    return;
    whogoesnow = "Tour joueur 1"
}

function win(){
  if   (empty1==empty2 && empty2==empty3 && empty3==0 ||
empty4==empty5 && empty5==empty6 && empty6==0 ||   
        empty7==empty8 && empty8==empty9 && empty9==0 ||
        empty1==empty4 && empty4==empty7 && empty7==0 ||
        empty2==empty5 && empty5==empty8 && empty8==0 ||
        empty3==empty6 && empty6==empty9 && empty9==0 ||
        empty1==empty5 && empty5==empty9 && empty9==0 ||
        empty3==empty5 && empty5==empty7 && empty7==0 ){
        alert ("Joueur 1: VOUS GAGNEZ !!!");         
        history.go(0) 
  }
  if   (empty1==empty2 && empty2==empty3 && empty3==1 ||
empty4==empty5 && empty5==empty6 && empty6==1 ||   
        empty7==empty8 && empty8==empty9 && empty9==1 ||
        empty1==empty4 && empty4==empty7 && empty7==1 ||
        empty2==empty5 && empty5==empty8 && empty8==1 ||
        empty3==empty6 && empty6==empty9 && empty9==1 ||
        empty1==empty5 && empty5==empty9 && empty9==1 ||
        empty3==empty5 && empty5==empty7 && empty7==1 ){
        alert ("Joueur 2: VOUS GAGNEZ !!!");
    history.go(0)
  }
}

function turnchange(t){
  if (change == 1)  {
    if (t == 0){
        t = 1;
        whogoesnow = "Tour : Joueur " + 1
    }
    else {
        t = 0;
    whogoesnow = "Tour : Joueur " + 2
    }
   
  }
  else {
    t = t;
  }
  change = 1 
  return (t);
}

function changing(clicked){
if (clicked == 1){           
   spot = empty1 ;}
if (clicked == 2){
   spot = empty2 ;}
if (clicked == 3){
   spot = empty3 ;}
if (clicked == 4){
   spot = empty4 ;}
if (clicked == 5){
   spot = empty5 ;}
if (clicked == 6){
   spot = empty6 ;}
if (clicked == 7){
   spot = empty7 ;}
if (clicked == 8){
   spot = empty8 ;}
if (clicked == 9){
   spot = empty9 ;}

if (spot == -1){
   if (t == 0){
      xo = "  O  " ;                        
      spot = 0            
   }
   else {
      xo = "  X  "  ;             
      spot = 1        
   }                             
   if (clicked == 1){           
      empty1 = spot;}
   if (clicked == 2){
      empty2 = spot ;}
   if (clicked == 3){
      empty3 = spot ;}
   if (clicked == 4){
      empty4 = spot ;}
   if (clicked == 5){
      empty5 = spot ;}
   if (clicked == 6){
      empty6 = spot ;}
   if (clicked == 7){
      empty7 = spot ;}
   if (clicked == 8){
      empty8 = spot ;}
   if (clicked == 9){
      empty9 = spot ;}
}
else {
   if (spot == 0){
       xo = "  O  ";}
   if (spot == 1){
       xo = "  X  ";}
   change = 0
}
return (xo) ;
}

function change_b1(form){   
    clicked = 1;   
    changing(clicked) ;    
    t = turnchange(t) ; 
    form.b1.value = xo;
    form.whoseturn.value = whogoesnow       
    win();
   return;
}
function change_b2(form){   
    clicked = 2;
    changing(clicked) ;  
    form.b2.value = xo;   
    t = turnchange(t) ;
    form.whoseturn.value = whogoesnow     
    win();
    return;
}

function change_b3(form){   
    clicked = 3;
    changing(clicked) ;
    form.b3.value = xo;        
    t = turnchange(t) ;   
    form.whoseturn.value = whogoesnow
    win();
    return;  
}

function change_b4(form){   
    clicked = 4;
    changing(clicked) ;
    form.b4.value = xo;   
    t = turnchange(t) ;   
    form.whoseturn.value = whogoesnow    
    win();
    return;
}

function change_b5(form){   
    clicked = 5;
    changing(clicked) ;
    form.b5.value = xo;   
    t = turnchange(t) ;
    form.whoseturn.value = whogoesnow    
    win();    
    return;
}

function change_b6(form){   
    clicked = 6;
    changing(clicked) ;
    form.b6.value = xo;   
    t = turnchange(t) ;
    form.whoseturn.value = whogoesnow    
    win();
    return;
}

function change_b7(form){   
    clicked = 7;
    changing(clicked) ;
    form.b7.value = xo;   
    t = turnchange(t) ;
    form.whoseturn.value = whogoesnow   
    win();
    return;
}

function change_b8(form){   
    clicked = 8;
    changing(clicked) ;   
    form.b8.value = xo;   
    t = turnchange(t) ;
    form.whoseturn.value = whogoesnow
    win();   
    return;
}

function change_b9(form){   
    clicked = 9;
    changing(clicked) ;
    form.b9.value = xo;   
    t = turnchange(t) ;
    form.whoseturn.value = whogoesnow 
    win();
    return;
}
</SCRIPT>


<SCRIPT name = "JavaScript">
variables();
</SCRIPT>

<FORM>
<input type = "button" name = "whoseturn"
 value = "Tour: Joueur  1">   
<br><br><br><br>
<table>
<tr><td><input type = "button" name = "b1" value = "      "         onClick =
change_b1(this.form)> 
    <td><input type = "button" name = "b2" value = "      "         onClick = change_b2(this.form)>
    <td><input type = "button" name = "b3" value = "      "         onClick =
change_b3(this.form)>
<tr><td><input type = "button" name = "b4" value = "      "         onClick =
change_b4(this.form)>
    <td><input type = "button" name = "b5" value = "      "         onClick = change_b5(this.form)>
    <td><input type = "button" name = "b6" value = "      "         onClick =
change_b6(this.form)>
<tr><td><input type = "button" name = "b7" value = "      "         onClick =
change_b7(this.form)>
    <td><input type = "button" name = "b8" value = "      "         onClick = change_b8(this.form)>  
    <td><input type = "button" name = "b9" value = "      "
onClick = change_b9(this.form)>
</table>
<br><br>

<INPUT TYPE="button" VALUE="Nouvelle Partie" onClick="history.go(0)">
</center>

________
Webpage Source URL:
http://www.javascriptkit.com/script/script2/tic.shtml

No comments:

Post a Comment