好的,CodeReview的一些随意的老兄要我把这个传递到这里,就这样吧。
我有一个基于这个答案的组合生成代码:StackOverflow answer
基本的想法是我有一组球员(总共6到14次),我需要组建2支球队,每支球队有3名球员,其余的球员都在休息。代码生成所有可能的匹配。当我需要随机选择下一个比赛设置时,问题就出现了,这样玩家只会休息,如果可能的话,只有1场比赛。此外,每支球队应该尽可能随机,不要像以前任何一场比赛那样拥有同样的球员。
为了澄清这个问题,这是一个很大的禁忌,如果我从阵列中选择下一场比赛就会发生这种情况:
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
我修改了代码,现在我已经:
问题
鉴于每个匹配组件,即玩家,团队和休息,都有掩码值,我该如何计算下一个游戏?使用按位运算符,如何?将每个休息的玩家推入阵列并排除那些在下一场比赛中休息的人。我已经尝试了一些多元化的事情,这是一个完整的混乱,并没有很好地结束。所以,我需要一些帮助。这是我第一次使用位掩码。
代码
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];