[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: 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, StringSplitOptions.RemoveEmptyEntries); // 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, let me know.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.