Display team names for a round robin tournament in C#

[round robin tournament]

The example Generate a schedule for a round robin tournament in C# explains how you can generate a schedule for a round robin tournament. This example simply adds the ability to display the schedule using the team names instead of the team numbers.

When you enter the team names and click Go, the following code executes.

// Schedule the round robin.
private void btnGo_Click(object sender, EventArgs e)
    // Get the teams.
    string all_teams = txtTeams.Text;
    char[] separators = { '\r', '\n' };
    string[] team_names = all_teams.Split(separators,

    // Get the schedule.
    int num_teams = team_names.Length;
    int[,] results = GenerateRoundRobin(num_teams);

    // Display the result.
    string txt = "";
    for (int round = 0; round <= results.GetUpperBound(1); round++)
        txt += "Round " + round + ":\r\n";
        for (int team = 0; team < num_teams; team++)
            if (results[team, round] == BYE)
                txt += "    " + team_names[team] + " (bye)\r\n";
            else if (team < results[team, round])
                txt += "    " + team_names[team] + " v " +
                    team_names[results[team, round]] + "\r\n";

    txtResults.Text = txt;

The program first gets the text from the txtTeams text box. It then splits that text at carriage return and new line characters, removing any blank entries.

The program uses the length of the team_names array as the number of teams and passes it into the GenerateRoundRobin method described in the previous post.

The results array is a two-dimensional array holding team indices in its first dimension and round numbers in the second. For example, results[1, 2] is the opponent that team 1 plays in round 2 of the round robin tournament.

The code uses results.GetUpperBound(1) to get the number of rounds and loops through the rounds. For each round, the code loops through the teams and displays the matches for that round in the round robin tournament.

To avoid displaying each match twice (for example, A v B and B v A), the code only displays matches where the first team number is less than the second.

When it displays the matches, the code uses the team_names array to display the team names instead of their indices. The code that does that is highlighted in blue in the preceding code. (This is the main difference between this example and the previous one.)

You can probably find a way to use a LINQ query to display the team names, but it would probably be unnecessarily complicated and confusing. If you do find such a query, post it in a comment below.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, combinatorics, mathematics and tagged , , , , , , , , , , , . Bookmark the permalink.