Skip to content

Instantly share code, notes, and snippets.

@pfirpfel
Created April 27, 2016 13:11
Show Gist options
  • Save pfirpfel/0bbd9d9b7ca53d69d617294326903ada to your computer and use it in GitHub Desktop.
Save pfirpfel/0bbd9d9b7ca53d69d617294326903ada to your computer and use it in GitHub Desktop.
Round Robin Pairings
var players = ['A', 'B', 'C', 'D', 'E', 'F', 'G' ];
var generateRounds = function(players, bye){
// add bye to rotation if number of players is odd
if(players.length % 2 == 1){
players.push(bye);
}
// first player does not rotate
var first = players.shift();
// output
var rounds = [];
// create paired rounds
for(var i = 0; i < players.length; i++){
var currentPlayers = [].concat(first, players);
var currrentRound = [];
while(currentPlayers.length > 0){
currrentRound.push([currentPlayers.shift(), currentPlayers.pop()]);
}
rounds.push(currrentRound);
// rotate players
players.push(players.shift())
}
return rounds;
}
generateRounds(players, 'BYE');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment