I am developing a sport torunament in Java based on round robin scheduling algorithm. For n
teams I want to generate 2(n-1)
rounds with n/2
matches. That is that every team must play a match in a round, and every 2 teams meet twice, once away and once home. I managed to implement the algoritm except for the home/away part. I am able to generate the rounds, but can not "swap" the teams in the second half of rounds so they play both away and home.
Here is what I have so far:
public class sports {
public static void main(String[] args) {
//obtain the number of teams from user input
Scanner input = new Scanner(System.in);
System.out.print("How many teams should the fixture table have?");
int teams = input.nextInt();
// Generate the schedule using round robin algorithm.
int totalRounds = (teams - 1) * 2;
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] = ("team " + (home + 1)
+ " plays against team " + (away + 1));
}
}
// Display the rounds
for (int i = 0; i < rounds.length; i++) {
System.out.println("Round " + (i + 1));
System.out.println(Arrays.asList(rounds[i]));
System.out.println();
}
}
}
Don't mind even/odd number of teams, for now I am only interested in even teams number.
In a single round-robin schedule, each participant plays every other participant once. If each participant plays all others twice, this is frequently called a double round-robin.
The formula for calculating the number of games in a double round-robin format is, n(n-1), assuming n is the number of teams in the tournament. E.g., IPL usually has 8 teams, which means n=8, and therefore: Number of league games in IPL = 8(8-1) = 8(7) = 56 matches.
There are two types of round-robin tournaments: Single league tournament: Every single league tournament plays with every other team once in its pool + N(N-1)/2. Double league tournament: Every team plays with every other team twice in its pool N(N-1).
A round robin styled tournament is where teams play one another an equal number of times, accumulating points as they win (or none if they lose). In a round robin tournament, the team with the best record is the winner.
To codify True Soft's answer,
String roundString;
if (round < halfRoundMark) {
roundString = ("team " + (home + 1)
+ " plays against team " + (away + 1));
} else {
roundString = ("team " + (away + 1)
+ " plays against team " + (home + 1));
}
rounds[round][match] = roundString;
where
int halfRoundMark = (totalRounds / 2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With