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]

プレイしたゲームの数が組み合わせの数を超えると、最初からやり直す必要があります...つまり、3つの組み合わせのいずれかをランダムに選択し、次の組み合わせをランダムに選択します...

しかし、ひねりがあります...そして、プレーヤーの数が奇数で、プレーヤーの1人が1つのゲームを休まなければならないとき、私の数学のスキルはかなり南に行きます。したがって、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でこれらのチームを形成するにはどうすればよいですか?

頭に浮かんだことの1つは、各プレーヤーに一意の値(10 ^ x)を与えることでした。例:

player1.value = 10;
player2.value = 100;
player3.value = 1000;
player4.value = 10000;

...そして、ループしてチームを形成するときに、チームの合計値が最後の値と等しいかどうかを確認します。

数学的に/JavaScript的にもっと才能のある人がこのコーディングの問題を手伝ってくれませんか。ありがとう!

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 に答える