Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double round robin tournament

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.

like image 753
Alina Gabriela Avatar asked Dec 31 '13 08:12

Alina Gabriela


People also ask

How does a double round-robin work?

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.

How many games are in 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.

What are the two types of round robin tournament?

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).

What is a round robin tournament?

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.


1 Answers

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);
like image 158
aquaraga Avatar answered Oct 09 '22 02:10

aquaraga