首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接4游戏结束条件

连接4游戏结束条件
EN

Stack Overflow用户
提问于 2017-10-11 05:16:14
回答 1查看 77关注 0票数 0

我正在用javascript构建一个connect 4 web应用程序。我目前正在尝试创建一个‘结束游戏’检查,如果整个网格被填满,没有赢家,游戏将结束。

代码语言:javascript
复制
function endGame(yTarget, xTarget) {
  let topRowOpen = true;

  // must have all yTarget index to have class for game to end

  for (let i = 0; i < yTarget + 1; i++) {
    let box = $(`#g-${i}-${0}`);
    console.log(box)
    if (box.hasClass("selectedP0Box") || box.hasClass("selectedP1Box")) {
      topRowOpen = false;
    }
  }
  return !topRowOpen;
}

我似乎不能使用for循环来解决这个问题(我需要考虑到不同大小的游戏板)。

EN

回答 1

Stack Overflow用户

发布于 2017-10-11 08:05:24

这是你正在寻找的更多东西:

代码语言:javascript
复制
var moveAvailable = function() {
    var n = 0;
    while (true) {
        var topCell = $('g-' + (n++) + '-0');
        if (!topCell.length) break;

        // It's important to use &&, not || here:
        if (!topCell.hasClass('selectedP0Box')
            && !topCell.hasClass('selectedP1Box')) return true;
    }

    // All top cells were checked; none were empty
    return false;
}

(注意:我的jquery相当生疏,我相信你可以让这段代码看起来更好)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46676175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档