Tic-Tac-Toe
1: Copy and paste the following script in the HEAD section of your page:
Select all...
<script language="javascript"> var image1=new Image(); image1.src="ex.gif"; var image2=new Image(); image2.src="oh.gif"; var tmp, done, iswon; var pcwins=0, playerwins=0, draws=0,content; var playerstarts=true; var moves=new Array(); var choices=[11,12,13,21,22,23,31,32,33]; var ways=new Array(); ways[1]=[0,11,12,13]; ways[2]=[0,21,22,23]; ways[3]=[0,31,32,33]; ways[4]=[0,11,21,31]; ways[5]=[0,12,22,32]; ways[6]=[0,13,23,33]; ways[7]=[0,11,22,33]; ways[8]=[0,13,22,31]; var w3c=(document.getElementById)?true:false; var ns4=(document.layers)?true:false; var ie4=(document.all && !w3c)?true:false; var ie5=(document.all && w3c)?true:false; var ns6=(w3c && navigator.appName.indexOf("Netscape")>=0)?true:false; function init(){ oktoplay=true; iswon=false; done=0; writetext(4); moves[11]=0; moves[12]=0; moves[13]=0; moves[21]=0; moves[22]=0; moves[23]=0; moves[31]=0; moves[32]=0; moves[33]=0; for(i=0;i<=8;i++){ document.images['rc'+choices[i]].src="nothing.gif"; document.images['rc'+choices[i]].alt=""; } if(!playerstarts)pcturn(); } function writetext(num){ switch(num){ case 1: content='
This game is a draw
'; break; case 2: content='
The computer won this round.
'; break; case 3: content='
You won this round.
'; break; case 4: content='
Computer Wins:
Player Wins:
Draws:
'; content+='
'+pcwins+'
'+playerwins+'
'+draws+'
'; break; } if(ns4){ document.scores.document.open(); document.scores.document.write('
'+content+'
'); document.scores.document.close(); } else if(ie4)document.all.scores.innerHTML=content; else document.getElementById('scores').innerHTML=content; if(num<4)setTimeout('init(4)',1000); } function setbutton(cellnum){ if(moves[cellnum]==0){ document.images['rc'+cellnum].src="ex.gif"; document.images['rc'+cellnum].alt=" X "; moves[cellnum]=1; done++; findwinner(true); }else alert('You cannot move here!'); } function pcstrategy(istowin){ var str=(istowin)? 2 : 1; for(n=1;n<=8;n++){ if((moves[ways[n][1]]==str) && (moves[ways[n][2]]==str) && (moves[ways[n][3]]==0))tmp=ways[n][3]; if((moves[ways[n][1]]==str) && (moves[ways[n][3]]==str) && (moves[ways[n][2]]==0))tmp=ways[n][2]; if((moves[ways[n][2]]==str) && (moves[ways[n][3]]==str) && (moves[ways[n][1]]==0))tmp=ways[n][1]; }} function findwinner(isplayer){ me=(isplayer)? 1 : 2; for(n=1;n<=8;n++){ if( (moves[ways[n][1]]==me) && (moves[ways[n][2]]==me) && (moves[ways[n][3]]==me) ){ iswon=true; break; }} if(iswon){ if(isplayer){ playerwins++; playerstarts=true; writetext(3); }else{ pcwins++; playerstarts=false; writetext(2); }}else{ if(done>8){ draws++; playerstarts=!playerstarts; writetext(1); }else if(isplayer) pcturn(); }} function pcturn(){ tmp='00'; pcstrategy(true); if(tmp=='00')pcstrategy(false); if(tmp=='00'){ do{ tmp=choices[Math.floor(Math.random()*9)]; }while(moves[tmp]!=0); } moves[tmp]=2; document.images['rc'+tmp].src="oh.gif"; document.images['rc'+tmp].alt=" O "; done++; findwinner(false); } window.onload=init; window.onresize=function(){ if(ns4)setTimeout('history.go(0)',400); } </script>
2: Copy and paste the following HTML in the BODY section of your page:
Select all...