Search code examples
c#algorithmround-robintournament

How do I make a round robin match schedule?


I am building a tournament schedule with round robin style match order. I have an algorithm set up to build the matches, however the match order is not what I am looking for. I am struggling to develop an algorithm that will build the matches in my desired order. See example below with a 6 team bracket. Each vertical line represents a row in the tournament. The far left number represents the base team seed and who they will play in each round.

Note: The only thing that is really important to me is that the 1 and 2 seed play in the last round of the tournament. And preferably that 1 v 6, 2 v 5, 3 v 4 happens in the first round of the tournament. All other matches aren't as important. Thank you in advance for any help you can provide.

1: 6 5 4 3 2

2: 5 4 3 6 1

3: 4 6 2 1 5

4: 3 2 1 5 6

5: 2 1 6 4 3

6: 1 3 5 2 4

Here is my current code:

                int numTeams = teamList.Count;

                int rounds = (numTeams - 1);
                int halfSize = numTeams / 2;
                List<Team> teams = new List<Team>();
                teams.AddRange(teamList); // Copy all the elements.
                teams.RemoveAt(0); // To exclude the first team.

                int teamSize = teams.Count;
                for (int round = 0; round < rounds; round++)
                {
                    int teamIdx = round % teamSize;

                    Team baseTeam1 = teams[teamIdx];
                    Team baseTeam2 = teamList[0];
                    // save each team to a match

                    for (int idx = 1; idx < halfSize; idx++)
                    {
                        int firstTeamIdx = (round + idx) % teamSize;
                        int secondTeamIdx = (round + teamSize - idx) % teamSize;

                        Team subTeam1 = teams[firstTeamIdx];
                        Team subTeam2 = teams[secondTeamIdx];
                        // save each team to a match

                    }

                }

Solution

  • Sometimes asking the question helps figure out the answer. As it turns out, my current algorithm was creating what I wanted, just in opposite order. What I did to fix was create a new varible inside the first for loop called actualRound:

    int actualRound = rounds - round; // this will reverse the round order