我的JavaScript代码中存在数学问题。我需要将给定数量的球员随机分成两队,以便每次都有 - 如果球员想要再次上场 - 球队会再次形成,他们应该是不同的,直到形成所有组合。
假设我有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;
...然后在循环到表单团队时检查团队的总值是否等于最后一个值。
有人可以在数学/ JavaScript上更有才能请帮助我解决这个编码问题。谢谢!
答案 0 :(得分:3)
使用值是一个好主意,但如果你让它们成为位掩码,则更容易检查哪些匹配的玩家。 例如
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>");
答案 1 :(得分:1)
对于偶数情况,只需选择你没有随机使用的数字并组建团队。
对于奇怪的情况,随机选择一个数字坐下。然后剩下的数字就像一个偶数的情况。