我必須做一個tic tac toe遊戲,我會陷入困境。基本上井字遊戲板的工作原理和我可以分別放置X和O的玩家,但是當我再次點擊單元格時,它會用X或者O代替它。例如,如果一個單元格中有一個X,你再次點擊它出現一個O.我的問題在於,如何檢查單元格中是否有X,然後將其保存並使其無法更改。從那裏我想嘗試贏得的條件,這可能就是如果cell1 & & cell2 & & cell3 ===「X」然後只是說你贏了或什麼。無論如何,任何幫助將非常感激。如何檢查是否有東西有價值
感謝
http://jsfiddle.net/78pu4y7t/11/
var nextTurn = "X";
function clickButton()
{
if(this.id === "t1")
{
\t if(document.getElementById("t1").innerHTML = "");
\t {
\t \t document.getElementById("t1").innerHTML = nextTurn;
\t \t turnSwitch();
\t \t
\t }
}
else if(this.id === "t2")
{
\t if (document.getElementById("t2").innerHTML = "");
\t {
\t \t document.getElementById("t2").innerHTML = nextTurn;
\t \t turnSwitch();
\t \t
\t }
}
else if(this.id === "t3")
{
\t if (document.getElementById("t3").innerHTML = "");
\t {
\t \t document.getElementById("t3").innerHTML = nextTurn;
\t \t turnSwitch();
\t \t
\t }
}
else if(this.id === "t4")
{
\t if (document.getElementById("t4").innerHTML = "");
\t {
\t \t document.getElementById("t4").innerHTML = nextTurn;
\t \t turnSwitch();
\t }
}
else if(this.id === "t5")
{
\t if (document.getElementById("t5").innerHTML = "");
\t {
\t \t document.getElementById("t5").innerHTML = nextTurn;
\t \t turnSwitch();
\t }
}
else if(this.id === "t6")
{
\t if (document.getElementById("t6").innerHTML = "");
\t {
\t \t document.getElementById("t6").innerHTML = nextTurn;
\t \t turnSwitch();
\t }
}
else if(this.id === "t7")
{
\t if (document.getElementById("t7").innerHTML = "");
\t {
\t \t document.getElementById("t7").innerHTML = nextTurn;
\t \t turnSwitch();
\t }
}
else if(this.id === "t8")
{
\t if (document.getElementById("t8").innerHTML = "");
\t {
\t \t document.getElementById("t8").innerHTML = nextTurn;
\t \t turnSwitch();
\t }
}
else if(this.id === "t9")
{
\t if (document.getElementById("t9").innerHTML = "");
\t {
\t \t document.getElementById("t9").innerHTML = nextTurn;
\t \t turnSwitch();
\t }
}
}
document.getElementById("t1").onclick = clickButton;
document.getElementById("t2").onclick = clickButton;
document.getElementById("t3").onclick = clickButton;
document.getElementById("t4").onclick = clickButton;
document.getElementById("t5").onclick = clickButton;
document.getElementById("t6").onclick = clickButton;
document.getElementById("t7").onclick = clickButton;
document.getElementById("t8").onclick = clickButton;
document.getElementById("t9").onclick = clickButton;
function turnSwitch()
{
if(nextTurn === "X")
{
\t nextTurn = "O";
}
else
{
\t nextTurn = "X";
}
\t
}
td{
border: 2px solid green;
height: 100px;
width: 100px;
text-align: center;
}
<table>
\t <tr>
\t \t <td id="t1"></td>
\t \t <td id="t2"></td>
\t \t <td id="t3"></td>
\t </tr>
\t <tr>
\t \t <td id="t4"></td>
\t \t <td id="t5"></td>
\t \t <td id="t6"></td>
\t </tr>
\t <tr>
\t \t <td id="t7"></td>
\t \t <td id="t8"></td>
\t \t <td id="t9"></td>
\t </tr>
</table>
非常感謝! – Glorpy 2014-11-22 23:16:59