Skip to content

Instantly share code, notes, and snippets.

@freiguy1
Created November 6, 2014 21:06
Show Gist options
  • Save freiguy1/3e58f537ba8368df8ca1 to your computer and use it in GitHub Desktop.
Save freiguy1/3e58f537ba8368df8ca1 to your computer and use it in GitHub Desktop.
Some code for generating a round robin tourney.
fn generate_roundrobin(teams: &Vec<IdAndName>) -> Vec<Vec<(&IdAndName, &IdAndName)>> {
let static_team: &IdAndName = &teams[0];
let mut other_teams: Vec<&IdAndName> = teams.iter().skip(1).collect();
let mut result: Vec<Vec<(&IdAndName, &IdAndName)>> = Vec::new();
for _ in range(0u, teams.len() - 1) {
let mut session: Vec<(&IdAndName, &IdAndName)> = Vec::new();
session.push((other_teams[0], static_team));
for i in range(0u, (other_teams.len() - 1) / 2) {
let team_1: &IdAndName = other_teams[i+1];
let team_2: &IdAndName = other_teams[other_teams.len() - 1 - i];
session.push((team_1, team_2));
}
// Rotate other_teams
let temp = other_teams[0];
for i in range(0u, other_teams.len() - 1) {
other_teams[i] = other_teams[i + 1];
}
other_teams[other_teams.len() - 1] = temp;
result.push(session)
}
result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment