Last active
October 3, 2018 20:51
-
-
Save artyil/44540e8bc892bfe0fb898fbef087f423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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