0

This is a problem similar to others which are asked a lot but I can't quite find anything matching what I'm trying to achieve.

In my scenario, a team is made up of 5 players, represented by this class:

class Team
{
    int ID {get; set;}
    List<Player> Players { get; set; } = new List<Player>();
}

And my Player class looks like this:

class Player
{
    string Name { get; set; }
}

From my database I get a List<Player> containing every available player (guaranteed to be unique). I ensure I only get as many as I can get into complete teams (e.g. maximumPlayers = numberPlayers / 5;).

At the moment I can easily loop through the List<Player> and assign each Player one to a Team and create a new Team every 5 players (maximum team size). I put these teams into a List<Team>.

I want to create every combination of List<Team> where the players are in different combinations of teams different, excluding where it's the same players in a different order. The intention is to put them into a List<List<Team>> for later evaluation.

How could I do this?

WSC
  • 903
  • 10
  • 30
  • What have you tried to do that? – Pavel Anikhouski Jul 13 '20 at 21:02
  • Here's a nice solution https://www.w3resource.com/csharp-exercises/recursion/csharp-recursion-exercise-11.php – Andrei Jul 13 '20 at 21:02
  • do you know how many combination you will have and how much time will it take to evaluate them? You should approach your problem in a different way: understand how to build teams (for example trying to have teams which players score sum is quite the same for all the teams) and then build them keeping it in mind. – ddfra Jul 13 '20 at 21:20
  • @ddfra The only guarantee I know is that a team is 5 players. I can guess the number of players won't be very many (< 100). What I've done so far is basically that, I've built my teams from some preliminary criteria and I then iterate over them trying to improve them based on my evaluation criteria. But I want to brute force it in this instance to see what the best possible outcome could be for my sample(s) and then optimize based on that. – WSC Jul 13 '20 at 21:31
  • @PavelAnikhouski Not anything vaguely successful enough to be worth mentioning... I can't quite wrap my head around how I could build my Teams any differently as each loop over `List` would create the same Teams. – WSC Jul 13 '20 at 21:33
  • Well, with 100 players you will only have 100!/5! = 9.034.502.400 possible teams... – ddfra Jul 13 '20 at 21:45
  • [This previous answer](https://stackoverflow.com/a/33336576/2330053) worked well for me in your scenario. – Idle_Mind Jul 13 '20 at 21:50
  • @Idle_Mind Thanks for the link. That does give me combinations of 5 `Players`... Now I need to identify and select the right ones. – WSC Jul 13 '20 at 22:09

0 Answers0