I am working through generating a league schedule and I'm stuck on the part where, for any given week, a team should only play once.
So far I have made sure that the correct number of games are played, and that each team plays their conference rivals 4 times, and their cross-conference opponents 2 times. This is the code I have for this:
let easternConfTeams = [a, b, c, d, e, f];
let westernConfTeams = [g, h, i, j, k, l];
const teamPool = [...easternConfTeams, ...westernConfTeams];
let schedule = teamPool.reduce((a, v, i) => {
for (let j = i + 1; j < teamPool.length; j++) {
if (i < 6) {
if (j < 6) {
a.push(`${v} : ${teamPool[j]}`);
a.push(`${v} : ${teamPool[j]}`);
a.push(`${v} : ${teamPool[j]}`);
a.push(`${v} : ${teamPool[j]}`);
} else {
a.push(`${v} : ${teamPool[j]}`);
a.push(`${v} : ${teamPool[j]}`);
}
} else {
if (j < 6) {
a.push(`${v} : ${teamPool[j]}`);
a.push(`${v} : ${teamPool[j]}`);
} else {
a.push(`${v} : ${teamPool[j]}`);
a.push(`${v} : ${teamPool[j]}`);
a.push(`${v} : ${teamPool[j]}`);
a.push(`${v} : ${teamPool[j]}`);
}
}
}
return a;
}, []);
And then I run this through a shuffle function:
shuffle = (schedule) => {
let currentIndex = schedule.length,
temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = schedule[currentIndex];
schedule[currentIndex] = schedule[randomIndex];
schedule[randomIndex] = temporaryValue;
}
return schedule;
};
However, I'm stuck on the final piece, which is to turn this schedule of games into separate weeks. Since there are 12 teams, each week of the season should have 6 games -- and of those six games, no team should appear twice. In other words, each team should play for each week, but only once.
There are 192 games in total, which need to be divided into 32 weeks of 6 games each.
How could I ensure this?