2012-03-29 54 views
0

我试图做简单的XO游戏,它似乎工作得很好,但当我到达3“Os” 函数调用(),它不会给我你在if语句 松散,它确实给你赢了,我达到3“Xs”。任何人都可以告诉我为什么这个jquery代码不响应正确吗?

<script type="text/javascript" src="jquery.js" ></script> 
<script type="text/javascript" > 



    function call() { 
     var x = Math.floor((Math.random() * 9) + 1); 
     if ($('#td' + x).text() === "x" || $('#td' + x).text() === "o") { 
      call(); 
     } 

     else if (check() === true) { 
      $('div').text("You win "); 
      return false; 
     } 

     else if (checkLoose() === true) { 
     $('p').text("You Loose "); 
      return false; 
     } 

     else 
      $('#td' + x).text("o"); 
     } 



    function win(as, bs, cc) { 
     return ($('#td'+as).text() === "x" && $('#td'+bs).text() === "x" && $('#td'+cc).text() === "x") ; 

    } 

    function check(){ 

     if (win(1, 2, 3) || win(1, 4, 7) || win(4, 5, 6) || win(7, 8, 9) || win(2, 5, 8) || win(3, 6, 9) || win(1, 5, 9) || win(3, 5, 7) === true) 
       return true; 
} 

function checkLoose() { 

    if (lose(1, 2, 3) || lose(1, 4, 7) || lose(4, 5, 6) || lose(7, 8, 9) || lose(2, 5, 8) || lose(3, 6, 9) || lose(1, 5, 9) || lose(3, 5, 7) === true) { 

     return true; 
    } 
} 




    function lose(as, bs, cc) { 
     return ($('#td' + as).text() === "o" && $('#td' + bs).text() === "o" && $('#td' + cc).text() === "o"); 

    } 

    function dsd(sad) { 

     if ($('#td' + sad).text() === "x" || $('#td' + sad).text() === "o") 
      return false; 

     else if (check() === true) 
      return false; 

     else if (checkLoose() === true) { 

      return false; 
     } 
     else { 
      $("#td" + sad).text("x"); 
      call(); 
     }  
     } 

     $(document).ready(function(){ 
     $("td").hover(function() { 
      $(this).css("background-color", "yellow"); 
     }, function() { 
      $(this).css("background-color", "white"); 
     }); 
    }); 

</script> 

</head> 
<body> 




<center> 
<h1 >"  X ,O Game "</h1><br /> 
<div></div> 
<p></p> 
<table> 
    <tr> 
     <td id="td1" onclick="dsd(1);" ></td> 
     <td id="td2" onclick="dsd(2);"></td> 
     <td id="td3" onclick="dsd(3);"></td> 
    </tr> 

    <tr> 
     <td id="td4" onclick="dsd(4);"></td> 
     <td id="td5" onclick="dsd(5);"></td> 
     <td id="td6" onclick="dsd(6);"></td> 
    </tr> 

    <tr> 
     <td id="td7" onclick="dsd(7);"></td> 
     <td id="td8" onclick="dsd(8);"></td> 
     <td id="td9" onclick="dsd(9);"</td> 
    </tr> 
</table> 
</center> 
+0

'checkloose'必须是'checkLoose'大写 – mgraph 2012-03-29 22:52:41

+0

需要发布那些函数做什么和任何关联的HTML。 – j08691 2012-03-29 22:58:43

+0

[“Lose”](http://dictionary.reference.com/browse/lose)不拼写[“Loose”](http://dictionary.reference.com/browse/loose)...查找它们。 – Sparky 2012-03-29 23:28:18

回答

1

您的支票(和checkLoose)只在点击过程中被调用。 您需要检查您在call()函数内添加o的行后的获胜和损失。

$('#td' + x).text("o"); 
/* add calls to check() and checkLoose() here */ 
0

在函数调用,您加入“O”字母的电池(这将导致checkLoose从没有返回true)之前检查赢家/失败者。如果你之前添加它,checkLoose将返回true(并且游戏将会正确更新)

然而,它有一个副作用,即移动它:如果人赢了,计算机仍然会添加一个“o”(然而,游戏会说“你赢了”) - 你可以通过各种方式解决这个问题。我会让你找出那个:)

+1

谢谢,这真的很有帮助。 – 2012-03-30 00:16:06

相关问题