Javascript 如何将x个玩家随机分成两个团队多次,每次都不同?

Javascript 如何将x个玩家随机分成两个团队多次,每次都不同?,javascript,math,combinations,Javascript,Math,Combinations,我的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] 当玩的游戏数超过组合数时,应该重新开始。。。i、 随机选择三个

我的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]
当玩的游戏数超过组合数时,应该重新开始。。。i、 随机选择三个组合中的一个,随机选择下一个,依此类推

但有一个转折点。。。当玩家的数量是奇数,其中一个玩家需要休息一场比赛时,我的数学技能就会变得非常糟糕。因此,对于5名球员,所有比赛组合为(最后一个数字为球员休息):

在JavaScript中,如何组建这些团队

想到的一件事是给每个玩家一个唯一的值(10^x),例如:

…然后在循环形成团队时,检查团队的总价值是否等于最后的价值


有谁在数学/Java脚本方面更有天赋,请帮我解决这个编码问题。谢谢

对于偶数情况,只需选择您没有随机使用的数字并组建团队即可


对于奇数的情况,随机选择一个数字,让其退出。然后剩下的数字就像是偶数。

使用值是一个好主意,但是如果你将它们设置为位掩码,则更容易检查匹配的玩家。 e、 g

player1.value=1
player2.value=2
player3.value=4

//(动态地,玩家n的值为1,即“选择您没有随机使用的数字并组成团队”你是指所有玩家的数组索引,对吗?如何不再组成同一个团队?更重要的是,我如何组建这些团队?你能提供一个真实/伪代码示例吗?听起来不错——创建所有组合,然后随机挑选——但对“位掩码”和“很抱歉回复太晚,我出去喝咖啡和吃晚饭了:)我已经把样本添加到了上面的帖子中,真是太棒了!尝试运行此程序,但它仍然有效,尽管我仍然没有获得“mask:players[i].mask | players[j].mask”和“if((t1.mask&t2.mask)==0)”。。。但我会的。然而,我不知道这是否是因为我的英语不好,但这将球员分成两人一组,我只想把所有球员分成两个单独的队。因此,9名球员将分成5对4。代码应该如何更改。啊,对不起,我不明白。我更改了团队创建。有趣的是,与之相关的递归相对来说比掩蔽更难:无论如何,关于掩蔽,&返回一个值,其中位设置在两个掩蔽都有位的位置。所以如果它是0,就没有对应的位,这就是我们要找的。关于比特屏蔽的更多信息:嗨,我在这里创建了一个后续问题:如果你能看一下,谢谢。
[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]
player1.value = 10;
player2.value = 100;
player3.value = 1000;
player4.value = 10000;
player1.value = 1 
player2.value = 2
player3.value = 4
//(dynamically player n would have value 1 << (n-1) )
    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>");