Created
March 27, 2014 17:47
-
-
Save joeRinehart/9813679 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package com.boyzoid | |
class ScottsRoundRobiner { | |
static final DEFAULT_TEAM_COUNT = 7 | |
static final DEFAULT_HOLE_COUNT = 9 | |
List makeSchedule( List teams, Integer holeCount ) { | |
List schedule = [] | |
// Checked against http://stackoverflow.com/questions/6648512/scheduling-algorithm-for-a-round-robin-tournament | |
if ( teams.size() % 2 ) { | |
teams << [ id: '0', name: 'BYE'] | |
} | |
def half = teams.size() / 2 | |
def round = 0 | |
while ( round < teams.size() - 1 ) { | |
// Create a random list of holes | |
List holes = (1..holeCount).collect{ it } | |
Collections.shuffle(holes) | |
round++ | |
def pairings = [] | |
for ( int i=0; i<half; i++) { | |
pairings << [ | |
teamOne: teams[i], | |
teamTwo: teams.getAt( teams.size() - ( i + 1 ) ), | |
hole: holes.pop() // grab the last hole from the random list, removing it from the list | |
] | |
} | |
// reorder the teams - inefficent, but who cares? | |
List newTeams = [ teams.first(), teams.last() ] | |
teams.findAll { | |
!newTeams.contains( it ) | |
}.each { | |
newTeams << it | |
} | |
teams = newTeams | |
// add it to the schedule | |
schedule << [ | |
round: round, | |
pairings: pairings | |
] | |
} | |
return schedule | |
} | |
List makeSchedule() { | |
List teams = [] | |
(0..DEFAULT_TEAM_COUNT).each { | |
teams << makeTeam(it) | |
} | |
makeSchedule( teams, DEFAULT_HOLE_COUNT ) | |
} | |
Map makeTeam(id) { | |
return [ | |
id:id + 1, | |
name: "Team " + ( id + 1 ) | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment