I already assigned 36 teams into an array of 144 match-ups, each facing off against eight opponents. I want to divide the matchups into 8 rounds, so it has 18 matches per round, but the constraint is that each team can only compete once per round. It’s basically the new UEFA Champion League Swiss system format. I have tried a while loop to keep retrying and shuffling the matchup but after 100 trials it still doesn’t work. Some rounds have fewer or more than 18 matches. Do you think it is possible without changing the matchups? Here’s my code [jsFiddle]:
const matchups = [
[
"26",
"10"
],
[
"26",
"23"
],
[
"26",
"7"
],
[
"28",
"26"
],
[
"21",
"26"
],
[
"32",
"26"
],
[
"36",
"26"
],
[
"26",
"22"
],
[
"43",
"25"
],
[
"25",
"54"
],
[
"25",
"11"
],
[
"37",
"25"
],
[
"16",
"25"
],
[
"27",
"25"
],
[
"25",
"20"
],
[
"25",
"2"
],
[
"14",
"36"
],
[
"37",
"36"
],
[
"36",
"43"
],
[
"36",
"15"
],
[
"11",
"36"
],
[
"29",
"36"
],
[
"36",
"41"
],
[
"23",
"3"
],
[
"23",
"24"
],
[
"23",
"43"
],
[
"27",
"23"
],
[
"10",
"23"
],
[
"29",
"23"
],
[
"23",
"37"
],
[
"54",
"13"
],
[
"48",
"54"
],
[
"54",
"40"
],
[
"54",
"41"
],
[
"54",
"18"
],
[
"34",
"54"
],
[
"12",
"54"
],
[
"2",
"29"
],
[
"2",
"12"
],
[
"24",
"2"
],
[
"2",
"13"
],
[
"7",
"2"
],
[
"20",
"2"
],
[
"2",
"21"
],
[
"7",
"43"
],
[
"1",
"43"
],
[
"43",
"15"
],
[
"43",
"41"
],
[
"43",
"28"
],
[
"48",
"37"
],
[
"7",
"37"
],
[
"22",
"37"
],
[
"37",
"11"
],
[
"37",
"24"
],
[
"1",
"27"
],
[
"11",
"1"
],
[
"1",
"12"
],
[
"14",
"1"
],
[
"1",
"21"
],
[
"9",
"1"
],
[
"29",
"1"
],
[
"35",
"16"
],
[
"35",
"20"
],
[
"10",
"35"
],
[
"28",
"35"
],
[
"35",
"19"
],
[
"11",
"35"
],
[
"22",
"35"
],
[
"35",
"48"
],
[
"15",
"33"
],
[
"17",
"15"
],
[
"15",
"40"
],
[
"15",
"10"
],
[
"12",
"15"
],
[
"15",
"9"
],
[
"33",
"11"
],
[
"9",
"11"
],
[
"11",
"34"
],
[
"27",
"19"
],
[
"48",
"27"
],
[
"34",
"27"
],
[
"13",
"27"
],
[
"27",
"33"
],
[
"9",
"48"
],
[
"9",
"17"
],
[
"28",
"9"
],
[
"20",
"9"
],
[
"14",
"9"
],
[
"17",
"14"
],
[
"28",
"17"
],
[
"17",
"48"
],
[
"17",
"20"
],
[
"16",
"17"
],
[
"22",
"17"
],
[
"19",
"3"
],
[
"19",
"40"
],
[
"13",
"19"
],
[
"19",
"22"
],
[
"19",
"29"
],
[
"33",
"19"
],
[
"32",
"21"
],
[
"10",
"32"
],
[
"18",
"32"
],
[
"32",
"28"
],
[
"16",
"32"
],
[
"32",
"3"
],
[
"20",
"32"
],
[
"22",
"28"
],
[
"13",
"22"
],
[
"10",
"22"
],
[
"41",
"14"
],
[
"41",
"7"
],
[
"41",
"29"
],
[
"41",
"34"
],
[
"16",
"41"
],
[
"3",
"16"
],
[
"3",
"28"
],
[
"3",
"10"
],
[
"3",
"7"
],
[
"24",
"3"
],
[
"18",
"16"
],
[
"13",
"16"
],
[
"18",
"7"
],
[
"7",
"14"
],
[
"33",
"29"
],
[
"29",
"34"
],
[
"21",
"13"
],
[
"21",
"33"
],
[
"21",
"12"
],
[
"24",
"21"
],
[
"20",
"48"
],
[
"48",
"10"
],
[
"40",
"14"
],
[
"24",
"40"
],
[
"40",
"12"
],
[
"40",
"18"
],
[
"40",
"34"
],
[
"12",
"18"
],
[
"12",
"20"
],
[
"34",
"13"
],
[
"34",
"18"
],
[
"14",
"24"
],
[
"18",
"33"
],
[
"33",
"24"
]
]
const totalRounds = 8
const matchesEachRound = 18;
let rounds: TeamId[][][] = [];
let trial = 0;
while (trial++ < 100) {
rounds = Array(totalRounds)
.fill([])
.map(() => []);
for (const matchup of _.shuffle(matchups)) {
let findTargetRound = rounds.find((round) => {
return (
(round.length < matchesEachRound &&
// both teams not complete in this round
_.intersection(round.flat(), matchup).length === 0)
);
});
if (findTargetRound) {
findTargetRound.push(matchup);
}
}
if (rounds.flat().length === matchups.length) {
break;
}
}
console.log(rounds)