Combinations.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.Linq;
  6. public static class Combinations
  7. {
  8. public static void Sample()
  9. {
  10. List<string[]> sourceList = new List<string[]>(3);
  11. sourceList.Add(new string[] { "black", "green" });
  12. sourceList.Add(new string[] { "red", "yellow"});
  13. List<string[]> resultList = GetCombinations(sourceList);
  14. foreach (string[] item in resultList)
  15. {
  16. Debug.Log(string.Join(",", item));
  17. }
  18. }
  19. public static List<T[]> GetCombinations<T>(List<T[]> sourceList)
  20. {
  21. List<T[]> resultList = new List<T[]>();
  22. Stack<T> stack = new Stack<T>();
  23. GetCombinationsCore(stack, resultList, sourceList);
  24. return resultList;
  25. }
  26. private static void GetCombinationsCore<T>(Stack<T> stack, List<T[]> resultList, List<T[]> sourceList)
  27. {
  28. int dimension = stack.Count;
  29. if (sourceList.Count <= dimension)
  30. {
  31. T[] array = stack.ToArray();
  32. Array.Reverse(array);
  33. resultList.Add(array);
  34. return;
  35. }
  36. else
  37. {
  38. foreach (T item in sourceList[dimension])
  39. {
  40. stack.Push(item);
  41. GetCombinationsCore(stack, resultList, sourceList);
  42. stack.Pop();
  43. }
  44. }
  45. }
  46. //GetDifferenetColorCardCount
  47. public static int GetDifferenetColorCardCount(List<CardSource> cardSources)
  48. {
  49. List<CardColor[]> cardColors = new List<CardColor[]>();
  50. foreach (CardSource cardSource in cardSources)
  51. {
  52. cardColors.Add(cardSource.CardColors.ToArray());
  53. }
  54. List<CardColor[]> colorCombinations = Combinations.GetCombinations(cardColors);
  55. int maxColorCount = 0;
  56. //赤~白に対応するカード1枚を各色毎に格納する配列
  57. CardSource[] cardsCorrespondingToColor = new CardSource[System.Enum.GetValues(typeof(CardColor)).Length - 1];
  58. for (int i = 0; i < cardsCorrespondingToColor.Length; i++)
  59. {
  60. cardsCorrespondingToColor[i] = null;
  61. }
  62. foreach (CardColor[] cardColorArray in colorCombinations)
  63. {
  64. if (cardColorArray.Length == cardSources.Count)
  65. {
  66. for (int i = 0; i < cardColorArray.Length; i++)
  67. {
  68. CardSource cardSource = cardSources[i];
  69. /*bool skip = false;
  70. for (int j = 0; j < cardsCorrespondingToColor.Length; j++)
  71. {
  72. if (cardsCorrespondingToColor[j] != null)
  73. {
  74. //既に同じ組み合わせの色のカードが配列に格納されている場合
  75. if (Enumerable.SequenceEqual(cardSource.CardColors.OrderBy(e => e), cardsCorrespondingToColor[j].CardColors.OrderBy(e => e)))
  76. {
  77. UnityEngine.Debug.Log($"SKIPPING: {cardSource.BaseENGCardNameFromEntity}");
  78. skip = true;
  79. break;
  80. }
  81. }
  82. }
  83. if (skip)
  84. {
  85. continue;
  86. }*/
  87. CardColor cardColor = cardColorArray[i];
  88. int colorIndex = (int)cardColor;
  89. if (0 <= colorIndex && colorIndex <= cardsCorrespondingToColor.Length - 1)
  90. {
  91. if (cardsCorrespondingToColor[colorIndex] == null)
  92. {
  93. cardsCorrespondingToColor[colorIndex] = cardSource;
  94. }
  95. }
  96. }
  97. }
  98. }
  99. int colorCount = cardsCorrespondingToColor.ToList().Count((cardSource) => cardSource != null);
  100. UnityEngine.Debug.Log($"COUNTS: {colorCount} >= {maxColorCount}");
  101. if (colorCount >= maxColorCount)
  102. {
  103. maxColorCount = colorCount;
  104. }
  105. return maxColorCount;
  106. }
  107. }