我在我的JavaScript代码的数学问题。 我需要这样每次玩家给定数量分成2队随机 - 如果玩家想再次发挥 - 队再次形成,直至形成所有的组合,他们应该是不同的。
比方说,我有4名球员,因此,所有的组合如下:
[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]
然而,因为球队侧不算数,只有3个不同的组合:
[1,2] vs [3,4]
[1,3] vs [2,4]
[1,4] vs [2,3]
当游戏次数超过组合的数量,应该从头再来......即随机选择三个组合中的一种,随机选择下一个等等...
但是有一个扭曲......和我的数学能力去相当困难,当南方的玩家人数为奇数,且玩家一个需要休息一场比赛。 所以,用5名球员全部对战组合(即球员休息的最后一个数字):
[1,2] vs [3,4] [5]
[1,2] vs [3,5] [4]
[1,2] vs [4,5] [3]
[1,3] vs [2,4] [5]
[1,3] vs [2,5] [4]
[1,3] vs [4,5] [2]
[1,4] vs [2,3] [5]
[1,4] vs [2,5] [3]
[1,4] vs [3,5] [2]
[1,5] vs [2,3] [4]
[1,5] vs [2,4] [3]
[1,5] vs [3,4] [2]
[2,3] vs [4,5] [1]
[2,4] vs [3,5] [1]
[2,5] vs [3,4] [1]
怎么可能在JavaScript中获得这些团队形成的呢?
即走进心灵的一件事是给每个玩家一个独特的价值(10 ^ X),例如:
player1.value = 10;
player2.value = 100;
player3.value = 1000;
player4.value = 10000;
...然后循环,形成团队检查,如果一个球队的总价值等于最后的值时。
可能有人数学/ JavaScriptly更多有才华的,请帮我这个编码问题。 谢谢!
使用值是一个好主意,但如果你让他们位掩码更容易检查哪些球员■找得到匹配。 例如
player1.value = 1
player2.value = 2
player3.value = 4
//(dynamically player n would have value 1 << (n-1) )
通过检查与其他玩家的面膜,它可以,如果他们已经被加上进行检查,因为他们已经有了自己的屏蔽值,他们永远不能与自身匹配。 至于随机因素,我认为最简单的方法是,先创建所有的组合,为你在你的例子一样,并使用这些组合的数组作为选择一个随机匹配的基础。 如果你觉得这样的做法是一个很好的路要走,但必须实现麻烦,我可以尝试,并煮了一些示例代码。
编辑这里的示例代码,希望评论有助于理清自己的意思EDIT2改变团队组合
var playercount = 5;
//add players
var players = new Array();
for (var i = 0; i < playercount; i++) {
players[i] = { Index: i, Mask: 1 << i, Name: "Player" + (i + 1), toString: function () { return this.Name; } };
//about the 1 << i: "<<" is a so called bit wise shift to the left.
//1 << i has the same outcome as 2 to the power of i
}
//the line below would print all players
//for (var pi in players) { var p = players[pi]; document.write(p + " (Mask:" + p.Mask + ")<br>"); } document.writeln("<br>");
//create all possible team combinations
var teams = new Array();
var playersPerTeam = Math.floor(playercount / 2);
function Team(){
this.list = new Array();
this.mask = 0;
this.count = 0;
this.full =false;
this.Add = function (i) {
this.list.push(players[i]);
this.mask |= players[i].Mask;
this.full = ++this.count === playersPerTeam;
}
this.toString = function () {
var res = "", cnt = this.list.length;
for (var i = 0; i < cnt; i++) {
if (i > 0)
res += i == cnt - 1 ? " and " : ",";
res += this.list[i].Name;
}
return res;
}
}
function addplayers() {
var indices = new Array(playersPerTeam);
for (var p = 0; p < playersPerTeam; p++) indices[p] = p;
var l = playersPerTeam - 1;
function addteam() {
var team = new Team();
for (var p = 0; p < playersPerTeam; p++) team.Add(indices[p]);
teams.push(team);
}
function addplayer(start, depth) {
var target = players.length - playersPerTeam + depth + 1;
var team;
for (var i = start; i < target; i++) {
indices[depth] = i;
if (depth == l)
addteam();
else
addplayer(i + 1, depth + 1);
}
}
addplayer(0, 0);
}
addplayers();
//the line below would print the team combinations
//for (var te in teams) { var t = teams[te]; document.write(t + " (mask:" + t.mask + ")<br>"); } document.writeln("<br>");
//create matches
var matches = new Array();
//the matches can be created in the same way as the teams, only the first team cannot have players of the second team
for (var i = 0; i < teams.length; i++) {
for (var j = i + 1; j < teams.length; j++) {
var t1 = teams[i], t2 = teams[j];
if ((t1.mask & t2.mask) === 0) //this is where the masks come in,
matches.push({ Team1: t1, Team2: t2, toString: function () { return this.Team1 + " versus " + this.Team2 } });
}
}
//randomize matches. Instead of picking a random match per turn, we can randomize at the
//start, so we know all the matches in advance.
//this can be done by using a sort on the array with a random index
//NB, this isn't the most random randomness (or whatever you call it LOL). For better shuffling
//there are several alternatives, but perhaps this one is enough
matches.sort(function() { return (parseInt(Math.random() * 100) % 2);});
//the line below prints the matches
for (var mi in matches) { document.write(matches[mi] + "<br>"); } document.writeln("<br>");
对于偶数的情况下,随便挑你没有随意使用的数字,使团队。
对于奇数的情况下,选择一个随机数坐了。 那么剩下的数字是像一个甚至情况。
文章来源: How to divide x number of players into 2 teams randomly multiple times, differently every time?