Combinations.cs 4.0 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[] { "a", "b" });
  12. sourceList.Add(new string[] { "c", "d", "e" });
  13. sourceList.Add(new string[] { "f", "g", "h", "i" });
  14. List<string[]> resultList = GetCombinations(sourceList);
  15. foreach (string[] item in resultList)
  16. {
  17. Debug.Log(string.Join(",", item));
  18. }
  19. }
  20. public static List<T[]> GetCombinations<T>(List<T[]> sourceList)
  21. {
  22. List<T[]> resultList = new List<T[]>();
  23. Stack<T> stack = new Stack<T>();
  24. GetCombinationsCore(stack, resultList, sourceList);
  25. return resultList;
  26. }
  27. private static void GetCombinationsCore<T>(Stack<T> stack, List<T[]> resultList, List<T[]> sourceList)
  28. {
  29. int dimension = stack.Count;
  30. if (sourceList.Count <= dimension)
  31. {
  32. T[] array = stack.ToArray();
  33. Array.Reverse(array);
  34. resultList.Add(array);
  35. return;
  36. }
  37. else
  38. {
  39. foreach (T item in sourceList[dimension])
  40. {
  41. stack.Push(item);
  42. GetCombinationsCore(stack, resultList, sourceList);
  43. stack.Pop();
  44. }
  45. }
  46. }
  47. //カードリストの内、異なる色を持つカードの枚数
  48. public static int GetDifferenetColorCardCount(List<CardSource> cardSources)
  49. {
  50. List<CardColor[]> cardColors = new List<CardColor[]>();
  51. foreach (CardSource cardSource in cardSources)
  52. {
  53. cardColors.Add(cardSource.CardColors.ToArray());
  54. }
  55. List<CardColor[]> colorCombinations = Combinations.GetCombinations(cardColors);
  56. int maxColorCount = 0;
  57. foreach (CardColor[] cardColorArray in colorCombinations)
  58. {
  59. //赤~白に対応するカード1枚を各色毎に格納する配列
  60. CardSource[] cardsCorrespondingToColor = new CardSource[System.Enum.GetValues(typeof(CardColor)).Length - 1];
  61. for (int i = 0; i < cardsCorrespondingToColor.Length; i++)
  62. {
  63. cardsCorrespondingToColor[i] = null;
  64. }
  65. if (cardColorArray.Length == cardSources.Count)
  66. {
  67. for (int i = 0; i < cardColorArray.Length; i++)
  68. {
  69. CardSource cardSource = cardSources[i];
  70. bool skip = false;
  71. for (int j = 0; j < cardsCorrespondingToColor.Length; j++)
  72. {
  73. if (cardsCorrespondingToColor[j] != null)
  74. {
  75. //既に同じ組み合わせの色のカードが配列に格納されている場合
  76. if (Enumerable.SequenceEqual(cardSource.CardColors.OrderBy(e => e), cardsCorrespondingToColor[j].CardColors.OrderBy(e => e)))
  77. {
  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. int colorCount = cardsCorrespondingToColor.ToList().Count((cardSource) => cardSource != null);
  99. if (colorCount >= maxColorCount)
  100. {
  101. maxColorCount = colorCount;
  102. }
  103. }
  104. return maxColorCount;
  105. }
  106. }