【问题标题】:find 3 equal elements between variable array and set of constant Arrays in JavaScript在 JavaScript 中的变量数组和常量数组集之间找到 3 个相等的元素
【发布时间】:2018-06-29 12:48:39
【问题描述】:

我正在构建一个井字游戏。我目前正在尝试编写函数来检查是否有人赢得了比赛。我有一个包含一组数据的对象,其中包括一个多维数组,其中包含与棋盘上的方块相关的获胜“数字”的所有组合。每次你点击一个方格时,方格的编号都会被推送到每个玩家的单独数组中。

我目前的问题是循环并找出包含玩家当前方格的数组何时等于中奖号码数组之一。

我已经尝试了很多不同的方法,包括嵌套 for 循环的不同变体、使用包含、使用 indexOf,然后尝试控制台记录获胜消息或将随机值推送到单独的数组/变量当达到 3 表示胜利时,但实际上我现在精神崩溃了,希望任何人都可以提供任何建议。

   var game = {

   player: true,
   win: [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,5],[2,5,8],[0,4,8],[2,4,6]], 
   //the above is my array of winning combinations
   p1Points: 0,
   p2Points: 0,
   p1Moves:[], //the place for players "moves" to be stored
   p2Moves:[]
        }

    function currentMove(square){
       var squareNum = square.data('squarenumber');
       square.off();
          if(game.player){
            square.html('x'); 
            game.p1Moves.push(squareNum);
            game.player = false;
           checkWin(game.win, game.p1Moves)
      }

      else{
            square.html('0');
            game.p2Moves.push(squareNum);
            game.player = true;}
            checkWin(game.win, game.p2Moves)
   }

   function checkWin(winArr, movesArr){

   /* this is the function I need help with, have tried multiple things
   it needs to return somthing to signal a player has won. e.g three in a 
   row. */




   }

【问题讨论】:

  • 你可以添加你最好的尝试,只是为了找出你的搜索出了什么问题。
  • 当然很抱歉,我只是想重写我最好的,我是在一个 codepen 中完成的,它不会回溯到足够远的地方让我找到,所以现在就再去一次吧

标签: javascript jquery arrays for-loop


【解决方案1】:

你可以这样做:

function checkWin(winArr, movesArr){
  return winArr.some(function(win) {
    return win.join('') === movesArr.join('')
  })
}

如果找到获胜组合,这将返回 true/false

编辑: 正如@rupps 提到的,不要忘记对其进行排序以保持顺序的一致性。

return win.join('') === movesArr.sort().join('')

【讨论】:

  • 您需要先对数组进行排序,否则移动顺序会破坏您的比较。
  • @rupps 谢谢。更新了答案
  • 什么意思?需要详细说明吗?
  • 是的,我似乎无法让它工作,即使使用 sort()
  • moveArr 数组正在返回,元素之间没有逗号,而 winArr 每个元素之间有逗号
【解决方案2】:

我建议将玩家的移动存储在一个对象中而不是一个数组中,以便之后进行比较。所以你会有:

 if(game.player){
     square.html('x'); 

     // use indexed object
     game.p1Moves[squareNum] = true;

     game.player = false;
     checkWin(game.win, game.p1Moves)
 }

现在,

/**
 * Checks winning combination
 *
 * @param winArr Array of Array[3] of Winning Moves 
 * @param movesObj Object with moves
 */

function checkWin(winArr, movesObj) {

    // iterate through all Winning Combinations
    for (var i = 0, l = winArr.length; i<l; i++) {

        var combArray = winArr[i],    // *each* of the winning combination arrays
            count=0;                  // success counter for every combination

        // check winning move: all squares in player object must be full
        for (var j=0; j<3; j++) 
            if (movesObj[combArray[j]]) count++;

        // current winning move ok if its 3 squares full
        if (count == 3) return true;
    }

    // no winning move found
    return false;

}   

或者在最有效的版本中:

function checkWin(winArr, movesObj) {

    // iterate through all Winning Combinations
    for (var i = 0, count = 0, l = winArr.length; i < l; i++, count = 0) {

        // check winning move: all squares in player object must be full
        for (var j=0; j<3; j++) ( movesObj[winArr[i][j]] && (count++) );

        // current winning move ok if its 3 squares full
        if (count == 3) return true;
    }

    // no winning move found
    return false;

}

【讨论】:

  • 嗨!谢谢,只是为了检查 for (var..) 部分,你有一个 l = win arr .length,我从来没有见过这个,但基本上你将长度分配给变量 l ,对吗?
  • 是的,这是为了提高效率,否则你会在每次迭代时检查 are.length,这会更慢
  • 太棒了,真的很有趣,你的解决方案效果很好,但另一部分我只需要掌握它 var comb = winArr[i], count = 0;所以这将单个 winArr 数组与零计数结合在一起?它是如何工作的,当我 console.log(comb) 我得到一个数组列表>这些是可能的数组留下来赢吗?我对这个过程有一个相对的了解,但任何更多的澄清都会很棒
  • 我已经澄清了一点。 var 中的“逗号”只是一次声明多个变量(更有效)。我为每一个获胜的动作将计数重置为 0。然后我检查当前的获胜移动数组。如果玩家使用了那个方块,我会增加“计数”。如果在循环结束时 count 为 3,则 whr 获胜动作完成。如果不是,那是不完整的。外部循环获取下一个(如果有)。如果不是,则返回 false。所有获胜组合都不完整。
  • 另一个改进点是对两个玩家使用相同的 MoveArray,但一个用“1”填充,另一个用“2”填充。检查程序将接受一个附加参数“player”,并且循环将检查 content == player。如果可行的话,您可以通过这种方式将游戏扩展到无限数量的玩家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多