如何查看井字游戏获胜者
我想知道我的代码有什么问题。我使用网站正文中的表格和 JavaScript 制作了一个井字棋游戏。以下是我的代码的相关部分:
INSIDE BODY:
<body>
</table>
<table id="matrix" border="1">
<tr>
<td class="cellCSS">
<img alt="Click Here" class="imageCSS" onclick="imageClicked(this,0,0)"/>
</td>
<td class="cellCSS">
<img alt="Click Here" class="imageCSS" onclick="imageClicked(this,0,1)"/>
</td>
<td class="cellCSS">
<img alt="Click Here" class="imageCSS" onclick="imageClicked(this,0,2)"/>
</td>
</tr>
...
</table>
</body>
这是函数 imageClicked 的相关部分:
...
same = false;
var r = 0;
tbl = document.getElementById("matrix");
//alert(tbl.rows.length);
for(r = 0; r < tbl.rows.length; r++) {
// alert('Bob');
var _tempmg = tbl.rows[r].cells[col].childNodes[0].src;
alert(_tempmg);
if (!_tempmg) break;
if (_img.src != _tempmg) break;
same = true;
}
if (r == tbl.rows.length && same) {
alert(_img.src + "won");
return;
}
same = false;
...
I want to know what is wrong with my code. I made a tic-tac-toe game using a table in the body of the website and javascript. Here are the relevant parts of my code:
INSIDE BODY:
<body>
</table>
<table id="matrix" border="1">
<tr>
<td class="cellCSS">
<img alt="Click Here" class="imageCSS" onclick="imageClicked(this,0,0)"/>
</td>
<td class="cellCSS">
<img alt="Click Here" class="imageCSS" onclick="imageClicked(this,0,1)"/>
</td>
<td class="cellCSS">
<img alt="Click Here" class="imageCSS" onclick="imageClicked(this,0,2)"/>
</td>
</tr>
...
</table>
</body>
And this is the relevant part of the function imageClicked:
...
same = false;
var r = 0;
tbl = document.getElementById("matrix");
//alert(tbl.rows.length);
for(r = 0; r < tbl.rows.length; r++) {
// alert('Bob');
var _tempmg = tbl.rows[r].cells[col].childNodes[0].src;
alert(_tempmg);
if (!_tempmg) break;
if (_img.src != _tempmg) break;
same = true;
}
if (r == tbl.rows.length && same) {
alert(_img.src + "won");
return;
}
same = false;
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题是 r == tbl.rows.length 总是 false,因为 for 循环在 r < 时迭代。表.行.长度。
Your problem is that r == tbl.rows.length is always false, because the for loop iterates while r < tbl.rows.length.
您应该包含更多的 imageClicked 函数。我猜它会开始:
并且在您发布的代码中:
大概您之前已经声明了相同的内容以将其保留在本地。
如果在循环中这样做,则无需在此处初始化 r(即为其赋值)。
在您的 html 中,您尚未为图像 src 属性分配值,因此我认为此时您将根据点击它的人分配一个零或十字。分配了适当的图像后,我将执行下一行。
在 for(...) 表达式中添加一个条件可能会更清楚,例如:
现在您可以使用:而不是break
。
我需要查看更多 imageClicked 函数才能理解它不起作用的原因。
You should include a bit more of the imageClicked function. I'll guess that it starts:
and in your posted code:
Presumably you've declared same earlier to keep it local.
There is no need to initialise r here (i.e. assign it a value) if you do so in the loop.
In your html, you have not assigned a value to the image src attribute, so I would have thought that at this point you'd assign a naught or cross depending on who had clicked on it. Having assigned an appropriate image, I would execute the next line.
It might be clearer to add a condition to the for(...) expression like:
Now instead of break you can use:
.
I need to see more of the imageClicked function to understand why it isn't working.