【发布时间】:2016-11-15 23:33:49
【问题描述】:
<!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;
}
这是我用于井字游戏的 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();
}
这是我的井字游戏 JS。问题是我不知道如何通知玩家他们是否赢了。到目前为止,在董事会上,两个人可以轮流进行,仅此而已。如何在 Javascript 中添加代码以显示“X”或“O”已获胜? 另外,我想知道到目前为止我的代码是否正确。所有建议将不胜感激!
【问题讨论】:
-
很高兴您已经包含了一些代码,但是在发布到这里之前,做一些研究并自己尝试一下确实是个好主意。如果您尝试并认为您已经接近,我们可以帮助您朝着正确的方向前进。现在,你基本上已经给了我们一个基础,并且你要求我们建造房子——获胜的逻辑是迄今为止这个练习中最复杂/最困难的部分。
标签: javascript