| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- using System.Linq;
- public static class Combinations
- {
- public static void Sample()
- {
- List<string[]> sourceList = new List<string[]>(3);
- sourceList.Add(new string[] { "black", "green" });
- sourceList.Add(new string[] { "red", "yellow"});
- List<string[]> resultList = GetCombinations(sourceList);
- foreach (string[] item in resultList)
- {
- Debug.Log(string.Join(",", item));
- }
- }
- public static List<T[]> GetCombinations<T>(List<T[]> sourceList)
- {
- List<T[]> resultList = new List<T[]>();
- Stack<T> stack = new Stack<T>();
- GetCombinationsCore(stack, resultList, sourceList);
- return resultList;
- }
- private static void GetCombinationsCore<T>(Stack<T> stack, List<T[]> resultList, List<T[]> sourceList)
- {
- int dimension = stack.Count;
- if (sourceList.Count <= dimension)
- {
- T[] array = stack.ToArray();
- Array.Reverse(array);
- resultList.Add(array);
- return;
- }
- else
- {
- foreach (T item in sourceList[dimension])
- {
- stack.Push(item);
- GetCombinationsCore(stack, resultList, sourceList);
- stack.Pop();
- }
- }
- }
- //GetDifferenetColorCardCount
- public static int GetDifferenetColorCardCount(List<CardSource> cardSources)
- {
- List<CardColor[]> cardColors = new List<CardColor[]>();
- foreach (CardSource cardSource in cardSources)
- {
- cardColors.Add(cardSource.CardColors.ToArray());
- }
- List<CardColor[]> colorCombinations = Combinations.GetCombinations(cardColors);
- int maxColorCount = 0;
- //赤~白に対応するカード1枚を各色毎に格納する配列
- CardSource[] cardsCorrespondingToColor = new CardSource[System.Enum.GetValues(typeof(CardColor)).Length - 1];
- for (int i = 0; i < cardsCorrespondingToColor.Length; i++)
- {
- cardsCorrespondingToColor[i] = null;
- }
- foreach (CardColor[] cardColorArray in colorCombinations)
- {
- if (cardColorArray.Length == cardSources.Count)
- {
- for (int i = 0; i < cardColorArray.Length; i++)
- {
- CardSource cardSource = cardSources[i];
- /*bool skip = false;
- for (int j = 0; j < cardsCorrespondingToColor.Length; j++)
- {
- if (cardsCorrespondingToColor[j] != null)
- {
- //既に同じ組み合わせの色のカードが配列に格納されている場合
- if (Enumerable.SequenceEqual(cardSource.CardColors.OrderBy(e => e), cardsCorrespondingToColor[j].CardColors.OrderBy(e => e)))
- {
- UnityEngine.Debug.Log($"SKIPPING: {cardSource.BaseENGCardNameFromEntity}");
- skip = true;
- break;
- }
- }
- }
- if (skip)
- {
- continue;
- }*/
- CardColor cardColor = cardColorArray[i];
- int colorIndex = (int)cardColor;
- if (0 <= colorIndex && colorIndex <= cardsCorrespondingToColor.Length - 1)
- {
- if (cardsCorrespondingToColor[colorIndex] == null)
- {
- cardsCorrespondingToColor[colorIndex] = cardSource;
- }
- }
- }
- }
- }
- int colorCount = cardsCorrespondingToColor.ToList().Count((cardSource) => cardSource != null);
- UnityEngine.Debug.Log($"COUNTS: {colorCount} >= {maxColorCount}");
- if (colorCount >= maxColorCount)
- {
- maxColorCount = colorCount;
- }
- return maxColorCount;
- }
- }
|