Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Generate Game List Based on List of Players in C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
2 Sep 2014CPOL2 min read 18K   4   4
Generate game list based on list of players in C#

Introduction

Several times, I got a request to build an application to support a sportsclub (e.g. table tennis) to keep track of / administer the competition progress. To do this in a correct matter, games must be generated automatically when a player has been added to a competition. Of course, this can be automated and this is explained in this article by using Linq.

Background

The Cartesian product is the mathematical way to solve this but, it generates also product with the same player which is not possible.

e.g. Player A versus Player A

and it also generates duplicates Player A versus Player B and Player B versus Player A which is the same game.

So the generated list must be filtered. The filtering is done by using a unique id per player and only add the last/second reference of a duplicate.

Using the Code

Two classes are used to store player data and the resulting game data:

C++
public class Player
{
  Public String Name {get; private set;}
  Public int ID {get; private set;}
  public Player(String name, int id)
  {
    Name = name;
    ID = id;
  }
}

public class Game
{
  public String PlayerA { get; private set; }
  public String PlayerB { get; private set; }
  public Game(String A, String B)
  {
    PlayerA = A;
    PlayerB = B;
  }
}  

To use these classes, the following code is used in the Form1.cs:

C++
private List<Player> Names = new List<Player>();
private List<Game> Games = new List<Game>();

public Form1()
{
  InitializeComponent();
  Names.Add(new Player("Andre",1));
  Names.Add(new Player("Luke", 2));
  Names.Add(new Player("Des", 3));
  Names.Add(new Player("Patrick", 4));
  dataGridView1.DataSource = Names;
  dataGridView2.DataSource = Games;
}

private void Generate()
{
  Games.Clear();
  //Cartesian product
  var query = Names.SelectMany(x => Names, (x, y) => new { x, y });

  //convert to list of games
  foreach (var q in query)
  {
    // a game between the same players is not added nor
    // a duplicate game with both names exchanged (e.g. a vs b and b vs a)
    if ((q.x.Name != q.y.Name)&& (q.y.ID > q.x.ID))
      Games.Add(new Game(q.x.Name, q.y.Name));
  }
  //ordered
  //Games.OrderBy(x=>x.PlayerA);

  //Alphabetical sorted
  Games.Sort((x, y) => string.Compare(x.PlayerA, y.PlayerA));
}

The form is initialized with 4 players (and shown in dataGridView1). By adding the names, dataGridView1 fires the event "Row Added" which on its turn calls Generate().

The Generate function clears the list, generates the Cartesian product list with 15 results based on 4 players. After obtaining the Cartesian product list, each result is filtered and added as a new game to the game list. Filtering is done when the game has no duplicate players and it's the second result. (Case A vs B and B vs A)

And since the Game list is databound to dataGridView2, the view is automatically updated and shows the games to play.

Points of Interest

From an educational point of view, the Cartesian product is the solution but to do it with Linq was something to find out.

History

  • 2nd September, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Program Manager
Netherlands Netherlands
Engineer with a lot of experience in C-based programming languages and IT in general.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Member 106331813-Sep-14 22:59
Member 106331813-Sep-14 22:59 
GeneralRe: My vote of 2 Pin
Andre van Dun4-Sep-14 3:03
professionalAndre van Dun4-Sep-14 3:03 
Questionexcellent Pin
landos773-Sep-14 7:05
professionallandos773-Sep-14 7:05 
QuestionNice! Pin
Member 87446973-Sep-14 2:46
Member 87446973-Sep-14 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.