【问题标题】:Javascript/JSON/nodejs: Best way to randomly assign an array of values to an array of players without giving a player their own entered valueJavascript/JSON/nodejs:将一组值随机分配给一组玩家而不给玩家自己输入的值的最佳方法
【发布时间】:2020-04-30 11:34:43
【问题描述】:

所以我有一个数组,其中存储了以下内容

players[{nickname: data.player, id: socket.id, data: playerdata}]

playerdata 本身就是一个数组

playerdata[]

在 playerdata 的第一个值中,每个玩家都输入了一个字符串。 (playerindex是输入字符串的玩家的索引)

players[playerindex].data.push('STRING THE PLAYER ENTERED')

现在我想将每个名称分配给 playerdata 数组的第二个值中的 playerindex:

players[playerindex].data.push(assignedplayerindex)

将每个名称随机分配给玩家索引的最佳方法是检查玩家不会得到自己输入的单词并且每个玩家都被分配了一个单词。

我自己尝试过,遇到了最后一个玩家无法分配单词的情况,调整了它,我觉得我前进的方向没有效率。关于如何做到这一点的任何想法?

【问题讨论】:

    标签: javascript arrays node.js json


    【解决方案1】:

    这就是我最终的做法,但我确信有很多方法可以优化这个脚本:

    //setup an array with all playerindex variables
            var playersnotassigned = [];
            rooms[data.roomindex].players.forEach(function(item,i){
              playersnotassigned.push(i);
            });
    
    
            //assign random name to each person, making sure that no name is send twice and you won't get your own name
            rooms[data.roomindex].players.forEach(function(item,i){
              do
              {
                //set the playerassigned to false;
                var playerassigned = false;
                //check if we are not dealing with the Last 2 players
                if (playersnotassigned.length != 2 || playersnotassigned.length != 1){
                  //create a random indexnumer from 0 to players not assigned-1
                  var randomnumber = Math.floor(Math.random() * playersnotassigned.length);
                  //check if the random index isn't the same as the currentindex
                  if (i!=playersnotassigned[randomnumber]){
                      //assign the data
                      rooms[data.roomindex].players[i].data.push(playersnotassigned[randomnumber]);
                      //remove that player from player assigned listen
                      playersnotassigned = playersnotassigned.filter(function(value){
                        return value!=playersnotassigned[randomnumber]
                      });
                      //set playerassigned to true;
                      playerassigned = true;
                  }
                } else if (playersnotassigned.length == 2)
                {
                  if (i == playersnotassigned[0]){
                    rooms[data.roomindex].players[i].data.push(playersnotassigned[1]);
                    rooms[data.roomindex].players[i+1].data.push(playersnotassigned[0]);
                    playersnotassigned = [];
                    playerassigned = true;
                    console.log(playersnotassigned.length);
    
                  } else if (i == playersnotassigned[1]){
                    rooms[data.roomindex].players[i].data.push(playersnotassigned[0]);
                    rooms[data.roomindex].players[i+1].data.push(playersnotassigned[1]);
                    playersnotassigned = [];
                    playerassigned = true;
                    console.log(playersnotassigned.length);
                  }
    
                } else if (playersnotassigned.length == 0) {
                  playerassigned = true;
                }
    
              }
              while (playerassigned==false)
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-22
      • 2015-12-12
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 2022-10-05
      • 1970-01-01
      相关资源
      最近更新 更多