Combinations.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace MISTBowl_Brackets
  5. {
  6. public static class Combinations
  7. {
  8. public static List<List<T>> GetAllCombinations<T>(IEnumerable<T> input, int groupsize)
  9. {
  10. try
  11. {
  12. var output = new List<List<T>>();
  13. var indices = Enumerable.Range(0, groupsize).ToList();
  14. while (indices[0] < input.Count() - groupsize + 1)
  15. {
  16. output.Add(indices.ConvertAll(a => input.ElementAt(a)));
  17. indices[groupsize - 1]++;
  18. for (int i = groupsize - 1; i >= 0; i--)
  19. {
  20. if (indices[i] >= input.Count() - groupsize + i + 1)
  21. {
  22. indices[i] = i > 0 ? ++indices[i - 1] + 1 : indices[i] + 1;
  23. for (int j = i + 1; j < groupsize; j++)
  24. indices[j] = indices[i] + j - i;
  25. }
  26. }
  27. }
  28. return output;
  29. }
  30. catch (Exception e)
  31. {
  32. Traceback.Write("At List<List<T>> GetAllCombinations<T>(IEnumerable<T>, int) (class Combinations):");
  33. throw e;
  34. }
  35. }
  36. public static List<List<Team>> GenerateBestMatches(List<Team> teams, int teamsPerMatch, int matchCount, List<Team> origTeamList)
  37. {
  38. if (teamsPerMatch * matchCount > teams.LongCount())
  39. throw new ArgumentOutOfRangeException();
  40. var combinations = GetAllCombinations(teams, teamsPerMatch).OrderBy(a => a.SameRegionMatchCount()).ThenBy(a => a.RepeatMatchCount()).ThenBy(a => a.RepeatRegionMatchCount(origTeamList)).ThenBy(a => TickBasedRandom.GetInteger(int.MinValue, int.MaxValue - 1)).ToList();
  41. var matches = new List<List<Team>>();
  42. while (matches.Count < matchCount)
  43. {
  44. if (!combinations[0].Any(a => matches.Any(b => b.Any(c => c.teamId == a.teamId))))
  45. matches.Add(combinations[0]);
  46. combinations.RemoveAt(0);
  47. }
  48. return matches;
  49. }
  50. }
  51. }