【发布时间】:2016-03-01 04:55:25
【问题描述】:
我应该首先说我对此非常陌生(不仅仅是堆栈溢出,还有任何一般的编码)。我现在正在参加一个非常基础的入门课程,我们的一项任务是在 HTML 框中创建一个井字游戏。
我专门搜索了这个问题的答案,但是我发现的任何东西对我来说都非常难以理解(注意:编写这段代码是我迄今为止所做过的最复杂的编码,所以这就是我的水平在)。
我了解为游戏创建空间(表格)并将按钮嵌入不同单元格以供玩家选择的动态。但是,为了获得额外奖励,他们为我们提供了让代码决定谁是获胜者的选择。任何关于从哪里开始的见解将不胜感激。
我什至不确定从哪里开始,但我想我需要编写另一个 javascript 代码才能添加到游戏中。到目前为止,这是我所拥有的(为了尽量减少这篇文章的长度,我只包含了一行):
function RowOneBoxThreeYButton() {
var x = document.getElementById("Initial_Choice_Row_One_Box_Three");
x.innerHTML = "Y";
x.style.color = "white";
}
function RowOneBoxThreeXButton() {
var x = document.getElementById("Initial_Choice_Row_One_Box_Three");
x.innerHTML = "X";
x.style.color = "white";
}
function RowOneBoxTwoYButton() {
var x = document.getElementById("Initial_Choice_Row_One_Box_Two");
x.innerHTML = "Y";
x.style.color = "white";
}
function RowOneBoxTwoXButton() {
var x = document.getElementById("Initial_Choice_Row_One_Box_Two");
x.innerHTML = "X";
x.style.color = "white";
}
function RowOneBoxOneYButton() {
var x = document.getElementById("Initial_Choice_Row_One_Box_One");
x.innerHTML = "Y";
x.style.color = "white";
}
function RowOneBoxOneXButton() {
var x = document.getElementById("Initial_Choice_Row_One_Box_One");
x.innerHTML = "X";
x.style.color = "white";
}
<html>
<body>
<table style="width:100%; background-color:black" border="2">
<tr>
<td>
<p id="Initial_Choice_Row_One_Box_One" style="color:white">Click a Button to Choose "X" or "Y"</p>
<button onclick="RowOneBoxOneXButton()">Choose X</button>
<button onclick="RowOneBoxOneYButton()">Choose Y</button>
</td>
<td>
<p id="Initial_Choice_Row_One_Box_Two" style="color:white">Click a Button to Choose "X" or "Y"</p>
<button onclick="RowOneBoxTwoXButton()">Choose X</button>
<button onclick="RowOneBoxTwoYButton()">Choose Y</button>
</td>
<td>
<p id="Initial_Choice_Row_One_Box_Three" style="color:white">Click a Button to Choose "X" or "Y"</p>
<button onclick="RowOneBoxThreeXButton()">Choose X</button>
<button onclick="RowOneBoxThreeYButton()">Choose Y</button>
</td>
</tr>
</body>
</html>
非常感谢大家!对不起,如果我的格式错误/我没有足够努力或正确地搜索这个答案。很高兴在各个方面都得到改进(包括在这里格式化我的帖子!)。
【问题讨论】:
-
嗨,欢迎来到 Stack Overflow。看起来您的问题相当广泛,可能不适合当前格式的网站。如果您还没有,请考虑查看 Tour stackoverflow.com/tour 和/或帮助中心。 stackoverflow.com/help
标签: javascript html tic-tac-toe