如何查看井字游戏获胜者

发布于 2024-10-31 04:39:02 字数 1238 浏览 1 评论 0原文

我想知道我的代码有什么问题。我使用网站正文中的表格和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热血少△年 2024-11-07 04:39:02

你的问题是 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.

本王不退位尔等都是臣 2024-11-07 04:39:02

您应该包含更多的 imageClicked 函数。我猜它会开始:

function imageClicked(element, row, col) {

并且在您发布的代码中:

same = false;

大概您之前已经声明了相同的内容以将其保留在本地。

var r = 0;

如果在循环中这样做,则无需在此处初始化 r(即为其赋值)。

tbl = document.getElementById("matrix");

for(r = 0; r < tbl.rows.length; r++) {
    var _tempmg = tbl.rows[r].cells[col].childNodes[0].src;

    if (!_tempmg) break;

在您的 html 中,您尚未为图像 src 属性分配值,因此我认为此时您将根据点击它的人分配一个零或十字。分配了适当的图像后,我将执行下一行。

    if (_img.src != _tempmg) break;
    same = true;

在 for(...) 表达式中添加一个条件可能会更清楚,例如:

for(r = 0; r < tbl.rows.length && !same; r++) {

现在您可以使用:而不是break

    if (_img.src == _tempmg) same = true;

}

if (r == tbl.rows.length && same) {
    alert(_img.src + "won");
    return;
}

same = false;

我需要查看更多 imageClicked 函数才能理解它不起作用的原因。

You should include a bit more of the imageClicked function. I'll guess that it starts:

function imageClicked(element, row, col) {

and in your posted code:

same = false;

Presumably you've declared same earlier to keep it local.

var r = 0;

There is no need to initialise r here (i.e. assign it a value) if you do so in the loop.

tbl = document.getElementById("matrix");

for(r = 0; r < tbl.rows.length; r++) {
    var _tempmg = tbl.rows[r].cells[col].childNodes[0].src;

    if (!_tempmg) break;

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.

    if (_img.src != _tempmg) break;
    same = true;

It might be clearer to add a condition to the for(...) expression like:

for(r = 0; r < tbl.rows.length && !same; r++) {

Now instead of break you can use:

    if (_img.src == _tempmg) same = true;

.

}

if (r == tbl.rows.length && same) {
    alert(_img.src + "won");
    return;
}

same = false;

I need to see more of the imageClicked function to understand why it isn't working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文