//判断第n个窗口是否使空窗口. 
function isBlank(n){ 
if(owns("plyer",n) || owns("computer",n)) return false 
return true 

//判断hidden元素的变量是否是who,既是说第i个窗口是否是属于who 
function owns(who,i){ 
var fr=parent.parent.frames[1] 
var doc=fr.document 
var field=doc.forms[0].elements["f"+i] 
if(field==null || field.value==who) return true 
return false 

//设置第n个窗口的所有者归who 
注:这里和上文的field引用的是一个hidden元素 
function setOwner(who,n){ 
var fr=parent.parent.frames[1] 
var doc=fr.document 
var field=doc.forms[0].elements[n] 
field.value=who 

//判断第 n1,n2,n3,个窗口是否同时归who所有. 
function ticTacToe(who,n1,n2,n3){ 
if(owns(who,n1) && owns(who,n2) && owns(who,n3)){ 
parent.parent.frames[3].focus() 
return true 
  } 
return false 

//判断who所拥有的窗口是否在一条直线上 
function isTicTacToe(who){ 
if(ticTacToe(who,0,1,2)) return true 
if(ticTacToe(who,3,4,5)) return true 
if(ticTacToe(who,6,7,8)) return true 
if(ticTacToe(who,0,3,6)) return true 
if(ticTacToe(who,1,4,7)) return true 
if(ticTacToe(who,2,5,8)) return true 
if(ticTacToe(who,0,4,8)) return true 
if(ticTacToe(who,2,4,6)) return true 
return false 

//判断游戏是否应该结束 
  function gameOver(){ 
var numMoves=0 
for(var i=0;i<9;++i){ 
if(!isBlank(i)) ++numMoves 
    } 
if (numMoves==9) return true 
if(isTicTacToe("player")) return true 
if(isTicTacToe("computer")) return true 
return false 

//为电脑设置可以出牌的窗口 
function ComputerMoves(){ 
if(gameOver()) return -1 
var newMove=Math.round(9*Math.random()) 
while(!isBlank(newMove){ 
newMove=(newMove+1)%9 

//此处值得借鉴,利用while循环判断真假,利用for循环来判断值的比较 
setOwner("computer",newMove) 
return newMove 

//为玩家设置出牌
­function playerMoves(){
if(!gameOver() && isBlank(Cell)){
setOwner("player",Cell)
var move=computerMoves()
location.href="o.htm"
if(move!=-1) parent.frames[move].location.href="x.htm"
 }
}
//显示胜利的信息
function showWin(){
if(isTicTacToe("player")) alert("you is Win!")
}
/显示失败的信息
function showLoser(){
if(isTicTacToe("computer")) alert("you is Loser!")
 }