Update 2007-03-21: If all you want to do is generate a fixtures list you can use my Fixtures Generator
I’ve been trying to find out how to generate fixture lists for some time (it is harder than it sounds!) and I finally worked it out thanks to this page about a fixture generating algorithm. My Java implementation of the algorithm is below.
/*
* This code owes an enormous debt to
* http://www.barrychessclub.org.uk/berger2001.htm
*/
import java.util.Arrays;
public class Fixtures2 {
public static void main(String[] args) {
// Find out how many teams we want fixtures for.
int teams = 0;
try {
teams = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.err.println("Please state number of teams as first arg.");
System.exit(0);
}
// If odd number of teams add a "ghost".
boolean ghost = false;
if (teams % 2 == 1) {
teams++;
ghost = true;
}
// Generate the fixtures using the cyclic algorithm.
int totalRounds = teams - 1;
int matchesPerRound = teams / 2;
String[][] rounds = new String[totalRounds][matchesPerRound];
for (int round = 0; round < totalRounds; round++) {
for (int match = 0; match < matchesPerRound; match++) {
int home = (round + match) % (teams - 1);
int away = (teams - 1 - match + round) % (teams - 1);
// Last team stays in the same place while the others
// rotate around it.
if (match == 0) {
away = teams - 1;
}
// Add one so teams are number 1 to teams not 0 to teams - 1
// upon display.
rounds[round][match] = (home + 1) + " v " + (away + 1);
}
}
// Interleave so that home and away games are fairly evenly dispersed.
String[][] interleaved = new String[totalRounds][matchesPerRound];
int evn = 0;
int odd = (teams / 2);
for (int i = 0; i < rounds.length; i++) {
if (i % 2 == 0) {
interleaved[i] = rounds[evn++];
} else {
interleaved[i] = rounds[odd++];
}
}
rounds = interleaved;
// Last team can't be away for every game so flip them
// to home on odd rounds.
for (int round = 0; round < rounds.length; round++) {
if (round % 2 == 1) {
rounds[round][0] = flip(rounds[round][0]);
}
}
// Display the fixtures
for (int i = 0; i < rounds.length; i++) {
System.out.println("Round " + (i + 1));
System.out.println(Arrays.asList(rounds[i]));
System.out.println();
}
System.out.println();
if (ghost) {
System.out.println("Matches against team " + teams + " are byes.");
}
System.out.println("Use mirror image of these rounds for "
+ "return fixtures.");
}
private static String flip(String match) {
String[] components = match.split(" v ");
return components[1] + " v " + components[0];
}
}
Update 2005-06-05: Now allows “complementary teams”, that is – teams that never play at home at the same time as each other. For example, try 10 teams — for each pair from 1 & 6, 2 & 7, 3 & 8, 4 & 9, and 5 & 10, both are never both at home. It was meant to be in the original program but a bug in my code was fouling things up.
Update 2007-03-21: You might also want to see the source code to my PHP fixtures generator
