Skip to content

Instantly share code, notes, and snippets.

@artyil
Last active October 3, 2018 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artyil/44540e8bc892bfe0fb898fbef087f423 to your computer and use it in GitHub Desktop.
Save artyil/44540e8bc892bfe0fb898fbef087f423 to your computer and use it in GitHub Desktop.
var _players = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
function generateTournament(players){
players = [...players];
if(players.length % 2 !== 0){
players.push('Bye');
}
// fill the tournament with empty arrays, every array holds the games for the same cycle
const tournament = [...players.slice(1).map(()=>new Array(players.length/2))];
console.log(tournament);
// special algorithm for this purpose
// https://en.wikipedia.org/wiki/Round-robin_tournament#Scheduling_algorithm
for (let i = 0; i < players.length - 1; i++) {
// console.table(players)
for (let j = 0; j < players.length / 2; j++) {
tournament[i][j] = [players[j], players[players.length - 1 - j]].join(',');
}
players.splice(1, 0, players.pop());
}
console.table(tournament)
}
generateTournament(_players);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment