蛇通过JavaScript数组

这可能是一个简单的答案,但我是一个最幸福的,这真的打破了我的大脑。 我想通过一个数组通过snaking赋值给variables。

我的代码是用Discord.js写的用于Discord的TTRPG toolbot。 对于这个特殊的function,我希望根据input的玩家数量来调整统计卷数,然后将所有这些卷集中在一起进行分类。 从那里开始,我希望通过sorting的arrays让每个玩家获得一套统计数据,这样每个玩家就可以接近一个平等的游戏场地。

例如,如果input是3个玩家,机器人将滚动3套6个统计数据并将其汇集到一个数组中。 为了简单解释,我们会说我们从1-18所有的数字。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

将被分配到

ABCCBAABCCBAABCCBA

所以最后的变数会

A = [1,6,7,12,13,18]

B = [2,5,8,11,14,17]

C = [3,4,9,10,15,16]

我现在的代码只能通过循环(A,B,C,A,B,C …)来sorting,这不会导致玩家被平均掉。 我尝试了很多不同的方法来获得我需要的结果,但是最终的variables只能被赋值一次,中间的variables会被赋予更多的属性,或者每个玩家variables只赋值一个属性。

我试着在网上search任何帮助,但是用“Javascript”和“Snake”search任何东西只是教你如何制作游戏,所以我真的希望你们能帮助我。 非常感谢,如果我想说的不清楚,我很抱歉,所以我很乐意回答您可能需要帮助的答案。

码:

if (msgContent.startsWith(".dstats ")) { let args = msgContent.split(" ").slice(1); var regex = /^\d+$/; var statIndex = []; var reply; var forward = true; if(regex.test(args) && args <= 10){ for(var i = 0; i < args*6; i++){ statRoll(); statIndex.push(randStat); }; distSort = statIndex.sort(sortNumber); for( j = 0; j < args; j++){ this['player'+j] = []; }; var playIndex = 0; for( f = 0; f < distSort.length; f++){ if(playIndex < args && playIndex >= 0){ this['player'+playIndex].push(distSort[f]); }else { playIndex = 0; this['player'+playIndex].push(distSort[f]); }; playIndex++; }; reply = "Your stats blocks are as follows:\n"; for (k = 0; k < args; k++){ reply += "Player " + (k+1) +": [" + this['player'+k].join(', ') + "]\n"; }; msg.reply(reply); }else( msg.reply("Looks like you inputted an improper number or your number is too high. Check your command and try again!") ); } 

我build议在这种情况下使用JSON,因为这样可以更轻松地处理数据。

无论如何,下面的代码应该做的伎俩: https : //jsbin.com/quvexu/edit?js,console

 // Declare input and output variables arr1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]; arr2 = ['a','b','c','c','b','a','a','b','c','c','b','a','a','b','c','c','b','a']; results = {a:[], b:[], c:[]}; // Do the work arr2.forEach(function(d,i){ results[d].push(arr1[i]); }) // Print the results console.log("Results", results); 

您可以迭代想要的零件的给定长度和零件的数量,然后计算均匀和不均匀零件的计算值。

 var length = 6, lines = 3, result = [], i, j; for (i = 0; i < length; i++) { for (j = 0; j < lines; j++) { result[j] = result[j] || []; result[j].push(i * lines + (i % 2 ? lines - j : j + 1)); } } console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

你可以做如下。 它将处理n玩家,并按照您所描述的顺序为每个玩家处理6个点。

 function dealPoints(c){ var nums = Array(6*c).fill().map((_,i) => i+1); return nums.reduce(function(r,n,i){ var j = i%c; j === 0 && (r.switch = !r.switch); r.switch ? r[j].push(n) : r[cj-1].push(n); return r; }, Array(c).fill().map(_ => [])); } var players = 3, result = dealPoints(players); console.log(result);