Combinations.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.Linq;
  6. using System.Xml;
  7. public static class Combinations
  8. {
  9. public static void Sample()
  10. {
  11. List<string[]> sourceList = new List<string[]>(4);
  12. sourceList.Add(new string[] { "red", "yellow" });
  13. sourceList.Add(new string[] { "purple", "red"});
  14. sourceList.Add(new string[] { "purple", "red" });
  15. sourceList.Add(new string[] { "purple", "red" });
  16. List<string[]> resultList = GetCombinations(sourceList);
  17. HighestValue(resultList);
  18. }
  19. public static void NameSample()
  20. {
  21. List<string[]> sourceList = new List<string[]>(4);
  22. sourceList.Add(new string[] { "Takuya Kanbara & Koji Minamoto", "Takuya Kanbara", "Koji Minamoto" });
  23. sourceList.Add(new string[] { "Takuya Kanbara"});
  24. sourceList.Add(new string[] { "Takuya Kanbara & Koji Minamoto", "Takuya Kanbara", "Koji Minamoto" });
  25. sourceList.Add(new string[] { "Koji Minamoto" });
  26. List<string[]> resultList = GetCombinations(sourceList);
  27. HighestValue(resultList);
  28. }
  29. public static List<T[]> GetCombinations<T>(List<T[]> sourceList)
  30. {
  31. List<T[]> resultList = new List<T[]>();
  32. Stack<T> stack = new Stack<T>();
  33. GetCombinationsCore(stack, resultList, sourceList);
  34. return resultList;
  35. }
  36. private static void GetCombinationsCore<T>(Stack<T> stack, List<T[]> resultList, List<T[]> sourceList)
  37. {
  38. int dimension = stack.Count;
  39. if (sourceList.Count <= dimension)
  40. {
  41. T[] array = stack.ToArray();
  42. Array.Reverse(array);
  43. resultList.Add(array);
  44. return;
  45. }
  46. else
  47. {
  48. foreach (T item in sourceList[dimension])
  49. {
  50. stack.Push(item);
  51. GetCombinationsCore(stack, resultList, sourceList);
  52. stack.Pop();
  53. }
  54. }
  55. }
  56. static int HighestValue(List<string[]> resultList)
  57. {
  58. int highestCount = 0;
  59. foreach (string[] item in resultList)
  60. {
  61. Debug.Log(string.Join(",", item));
  62. int count = item.Distinct().ToArray().Length;
  63. if (count > highestCount)
  64. {
  65. highestCount = count;
  66. Debug.Log($"New Highest Count: {highestCount}");
  67. }
  68. }
  69. return highestCount;
  70. }
  71. //GetUniqueNameCardCount
  72. public static int GetUniqueNameCardCount(List<CardSource> cardSources)
  73. {
  74. List<string[]> cardNames = new List<string[]>();
  75. foreach (CardSource cardSource in cardSources)
  76. cardNames.Add(cardSource.CardNames.ToArray());
  77. return HighestValue(GetCombinations(cardNames));
  78. }
  79. //GetUniqueColorCardCount
  80. public static int GetUniqueColorCardCount(List<CardSource> cardSources)
  81. {
  82. List<string[]> cardColors = new List<string[]>();
  83. foreach (CardSource cardSource in cardSources)
  84. cardColors.Add(cardSource.CardColors.Map(x => x.ToString()).ToArray());
  85. return HighestValue(GetCombinations(cardColors));
  86. }
  87. //GetDifferenetColorCardCount
  88. public static int GetDifferenetColorCardCount(List<CardSource> cardSources, bool allowSkip = false)
  89. {
  90. List<CardColor[]> cardColors = new List<CardColor[]>();
  91. foreach (CardSource cardSource in cardSources)
  92. {
  93. cardColors.Add(cardSource.CardColors.ToArray());
  94. }
  95. List<CardColor[]> colorCombinations = Combinations.GetCombinations(cardColors);
  96. int maxColorCount = 0;
  97. //赤~白に対応するカード1枚を各色毎に格納する配列
  98. CardSource[] cardsCorrespondingToColor = new CardSource[System.Enum.GetValues(typeof(CardColor)).Length - 1];
  99. for (int i = 0; i < cardsCorrespondingToColor.Length; i++)
  100. {
  101. cardsCorrespondingToColor[i] = null;
  102. }
  103. foreach (CardColor[] cardColorArray in colorCombinations)
  104. {
  105. if (cardColorArray.Length == cardSources.Count)
  106. {
  107. for (int i = 0; i < cardColorArray.Length; i++)
  108. {
  109. CardSource cardSource = cardSources[i];
  110. if (allowSkip)
  111. {
  112. bool skip = false;
  113. for (int j = 0; j < cardsCorrespondingToColor.Length; j++)
  114. {
  115. if (cardsCorrespondingToColor[j] != null)
  116. {
  117. //既に同じ組み合わせの色のカードが配列に格納されている場合
  118. if (Enumerable.SequenceEqual(cardSource.CardColors.OrderBy(e => e), cardsCorrespondingToColor[j].CardColors.OrderBy(e => e)))
  119. {
  120. UnityEngine.Debug.Log($"SKIPPING: {cardSource.BaseENGCardNameFromEntity}");
  121. skip = true;
  122. break;
  123. }
  124. }
  125. }
  126. if (skip)
  127. {
  128. continue;
  129. }
  130. }
  131. CardColor cardColor = cardColorArray[i];
  132. int colorIndex = (int)cardColor;
  133. if (0 <= colorIndex && colorIndex <= cardsCorrespondingToColor.Length - 1)
  134. {
  135. if (cardsCorrespondingToColor[colorIndex] == null)
  136. {
  137. cardsCorrespondingToColor[colorIndex] = cardSource;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. int colorCount = cardsCorrespondingToColor.ToList().Count((cardSource) => cardSource != null);
  144. if (colorCount >= maxColorCount)
  145. {
  146. maxColorCount = colorCount;
  147. }
  148. return maxColorCount;
  149. }
  150. }