public void NextRound(int chunks) { Locked = true; if (CurrentRound != null) { Rounds.Add(CurrentRound.Copy()); } Rounds.ForEach(x => x.Locked = true); foreach (Player player in Players) { player.Chunk = string.Empty; } CurrentRound = new Round(); CurrentRound.ID = Rounds.Count() + 1; foreach (Player player in Players.Where(x => x.Alive && x.ID > 0)) { CurrentRound.Players.Add(player); } List <List <Player> > chunksList = new List <List <Player> >(); for (int i = 0; i < chunks; i++) { chunksList.Add(new List <Player>()); } foreach (var player in CurrentRound.Players.Where(x => !string.IsNullOrEmpty(x.SeedChunk))) { if (int.TryParse(player.SeedChunk, out int chunk) && chunksList.Count() >= chunk) { player.Chunk = chunk.ToString(); chunksList[chunk - 1].Add(player); } } foreach (var player in RandomPermutation(CurrentRound.Players.Where(x => string.IsNullOrEmpty(x.Chunk)))) { List <Player> chunk = chunksList.FirstOrDefault(x => x.Count() == chunksList.Min(y => y.Count())); chunk.Add(player); player.Chunk = (chunksList.IndexOf(chunk) + 1).ToString(); } }