<!DOCTYPE html>
<html>
<head>
<link href="tictactoe.css" type="text/css" rel="stylesheet" >
<meta charset= "utf-8">
<title>Tic Tac Toe</title>
</head>
<body>
<h1>Tic Tac Toe</h1>
<form>
Current Player <p id="currentplayer">X</p>
<table id= "game" border="1">
<tr>
<td type = "button" onclick = "set('box1');" id = "box1" ></button></td>
<td type = "button" onclick = "set('box2');" id = "box2" ></button></td>
<td type = "button" onclick = "set('box3');" id = "box3" ></button></td>
</tr>
<tr>
<td type = "button" onclick = "set('box4');" id = "box4" ></button></td>
<td type = "button" onclick = "set('box5');" id = "box5" ></button></td>
<td type = "button" onclick = "set('box6');" id = "box6" ></button></td>
</tr>
<tr>
<td type = "button" onclick = "set('box7');" id = "box7" ></button></td>
<td type = "button" onclick = "set('box8');" id = "box8" ></button></td>
<td type = "button" onclick = "set('box9');" id = "box9" ></button></td>
</tr>
</table>
<button id = "restart">New Game </button>
</form>
<script src= "tictactoe.js" type = "text/Javascript"></script>
</body>
</html>
這是我的HTML代碼。如何判斷玩家是否在井字遊戲中獲勝或失敗
h1 {
font-family: "Courier New";
}
body {
text-align: center;
background-color: #FFB6C1;
}
table {
height: 300px;
width: 300px;
margin: auto;
}
button {
height: 100px;
width: 100px;
}
#box1 {
height: 100px;
width: 100px;
}
#box2 {
height: 100px;
width: 100px;
}
#box3 {
height: 100px;
width: 100px;
}
#box4 {
height: 100px;
width: 100px;
}
#box5 {
height: 100px;
width: 100px;
}
#box6 {
height: 100px;
width: 100px;
}
#box7 {
height: 100px;
width: 100px;
}
#box8 {
height: 100px;
width: 100px;
}
#box9 {
height: 100px;
width: 100px;
}
這是我的tic tac toe遊戲的CSS。
function changeplayer()
{
var player = document.getElementById("currentplayer").innerHTML;
if(player == "X")
{
document.getElementById("currentplayer").innerHTML = "O";
}
else{
document.getElementById("currentplayer").innerHTML = "X";
}
}
function set(idvalue)
{
var buttonclicked = document.getElementById(idvalue);
if(buttonclicked.innerHTML == "" || buttonclicked.innerHTML == null)
{
var player = document.getElementById("currentplayer").innerHTML;
buttonclicked.innerHTML = player;
}
changeplayer();
}
這是我的tic tac toe遊戲的JS。事情是我不知道如何通知玩家,如果他們贏了或不。到目前爲止,雙方都可以輪流參加這一切。如何在Javascript中添加代碼以顯示「X」或「O」贏了? 另外,我想知道我的代碼看起來是否正確。所有建議將不勝感激!
這很棒,你已經包含了一些代碼,但是在發佈之前,這是一個好主意,可以自己做一些研究並嘗試。如果你嘗試並認爲你很接近,我們可以幫助你朝正確的方向發展。現在,你基本上已經給了我們一個基礎,並且你要求我們建造房子 - 勝利邏輯是這個練習中最複雜/最困難的部分。 – Santi