3

我的 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 上更有才华,请帮助我解决这个编码问题。谢谢!

4

2 回答 2

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>");
于 2012-06-02T10:45:11.220 回答
1

对于偶数情况,只需选择您没有随机使用的号码并组队。

对于奇数情况,随机选择一个数字坐下。然后剩下的数字就像一个偶数情况。

于 2012-06-02T10:31:06.717 回答