I am developing a sports torunament in java based on a round scheduling algorithm. For n teams, I want to generate 2 (n-1) rounds with n / 2 matches. That is, each team must play a match in a round, and every two teams meet twice, once and once at home. I managed to implement the algorithm, except for the home / remote part. I can create rounds, but I can’t “change” teams in the second half of the rounds so that they play both at home and at home.
Here is what I still have:
import java.util.Arrays;
import java.util.Scanner;
public class sports {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many teams should the fixture table have?");
int teams;
teams = input.nextInt();
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);
if (match == 0) {
away = teams - 1;
}
rounds[round][match] = ("team " + (home + 1) + " plays against team " + (away + 1));
}
}
for (int i = 0; i < rounds.length; i++) {
System.out.println("Round " + (i + 1));
System.out.println(Arrays.asList(rounds[i]));
System.out.println();
}
}
}
Do not mind the even / odd number of teams, at the moment I am only interested in the number of teams. Any help is appreciated, thanks :)
source
share