0

好的,CodeReview的某个随机家伙让我把这个传到这里,就这样吧。

我有一个基于这个答案的组合生成代码:StackOverflow answer

基本思想是我有一组球员(总共 6 到 14 名),我需要组建 2 支球队,每支球队有 3 名球员,而其余球员正在为那场比赛休息。该代码生成所有可能的匹配项。当我需要随机选择下一场比赛设置时,问题就来了,这样球员们只会休息,如果可能的话,只有一场比赛。此外,每支球队都应该尽可能地随机化,而不是像迄今为止的任何一场比赛一样让相同的球员作为一个球队参加比赛。

为了澄清这个问题,这是一个很大的禁忌,如果我从数组中选择下一个匹配项,就会发生这种情况:

        Team 1 | Team 2 | Resting
Game 1: A B C    D E F    G H I J
Game 2: A B D    C E F    G H I J
Game 3: A B E    C D F    G H I J

...虽然这看起来不错:

        Team 1 | Team 2 | Resting
Game 1: A B C    D E F    G H I J
Game 2: C G H    F I J    A B D E
Game 3: A D I    B E J    C F G H

我已经修改了代码,现在我有了:

  • 带位掩码的玩家
  • 带位掩码的团队
  • 将数组与 2 个团队和 1 个休息组匹配(每个都带有位掩码)

问题
鉴于这些比赛组成部分,即球员、球队和休息,都有一个掩码值,我应该如何计算下一场比赛?使用按位运算符,如何?将每个休息的球员推入一个数组,并排除那些在下一场比赛中休息的球员。我尝试了一些多阵列的东西,这完全是混乱的,结果并不好。所以,我需要一些帮助。这是我第一次处理位掩码。

代码

players: [],
teams: [],
matches: [],
Team: function () {
    this.list           = [];
    this.mask           = 0;
    this.count          = 0;
    this.full           = false;
    this.Add            = function (i) {
        this.list.push(GAME.players[i]);
        this.mask       |= GAME.players[i].mask;
        this.full       = ++this.count === 3;
    }
},

Resting: function () {
    this.list           = [];
    this.mask           = 0;
    this.count          = 0;
    this.full           = false;
    this.Add            = function (i) {
        this.list.push(GAME.players[i]);
        this.mask       |= GAME.players[i].mask;
        this.full       = ++this.count === (GAME.players.length - 2*3);
    }
},

addPlayers: function () {
    var indexes         = [],
        p,
        playersPerTeam  = 3,
        l               = playersPerTeam - 1;

    for (var p = 0; p < playersPerTeam; p += 1) {
        indexes[p] = p;
    }

    function addteam() {
        var team = new GAME.Team();

        for (var p = 0; p < playersPerTeam; p++) {
            team.Add(indexes[p]);
        }

        GAME.teams.push(team);
    }

    function addplayer(start, depth) {
        var target = GAME.players.length - playersPerTeam + depth + 1;

        for (var i = start; i < target; i++) {
            indexes[depth] = i;
            if (depth == l) {
                addteam();
            } else {
                addplayer(i + 1, depth + 1);
            }
        }
    }

    addplayer(0, 0);
}

// The code below runs during game initializing,
// before any round has started
var playerCount         = GAME.addedPlayers.length,
    i;

for (i = 0; i < playerCount; i += 1) {
    GAME.players[i] = {
        index: i,
        mask: 1 << i,
        name: GAME.addedPlayers[i].name
    };
}

GAME.addPlayers();

// The matches creation
for (var i = 0; i < GAME.teams.length; i++) {
    for (var j = i + 1; j < GAME.teams.length; j++) {
        var t1      = GAME.teams[i],
            t2      = GAME.teams[j],
            r1      = (GAME.players.length > 2*3) ? new GAME.Resting() : false;

        if ((t1.mask & t2.mask) === 0) { //this is where the masks come in
            if (r1) {
                var resting = t1.mask ^ t2.mask;

                for (var k = 0; k < GAME.players.length; k++) {
                    if ((resting & 1) === 0) {
                        r1.Add(k);
                    }
                    resting >>>= 1;
                }
            }

            GAME.matches.push({
                Team1: t1,
                Team2: t2,
                Resting: r1
            });
        }
    }
}

// The code below start a new round
// This should start a new randomly selected game described in my question
// and not like I'm doing it now
var gamesPlayed         = GAME.gamesPlayed,
    match               = GAME.matches[gamesPlayed];
4

0 回答 0