/// <summary> /// Enumerate all of the possible choice combinations of card sets on the table for use in determining /// possible plays. /// To this end we can remove all cards from the table that are higher value than that of the thrown /// card as well as eliminate partial sets that sum to a value greater than that of the thrown card. /// </summary> /// <param name="set">The set of cards to select choices from</param> /// <param name="target">The target card to check value against</param> /// <returns> /// Flag indicating if the given set of cards contains a subset of cards whose values sum to the value /// of the target card. /// </returns> public static List <List <Card> > EnumerateAll(List <Card> set, Card target) { Debug.Assert(set != null); set = new List <Card>(set); set.RemoveAll(delegate(Card c) { return((int)c > (int)target); }); List <List <Card> > list = new List <List <Card> >(); if (set.Count > 0) { for (int k = 1; k <= set.Count; ++k) { ChoiceEnumerable <Card> enumerable = new ChoiceEnumerable <Card>(k, set.ToArray <Card>()); foreach (Card[] cards in enumerable) { if ((int)target == cards.Sum <Card>(a => (int)a)) { list.Add(new List <Card>(cards)); } } } } return(list); }
/// <summary> /// Enumerate all of the possible choice combinations of card sets on the table for use in determining /// if a throw is legal or not. A throw is not legal when a trick can be taken using the card. This /// means that if there is a card of equal value on the table the throw is not legal. If there is a /// set of cards on the table that sum to the value of the thrown card then the throw is not legal. /// To this end we can remove all cards from the table that are higher value than that of the thrown /// card as well as eliminate partial sets that sum to a value greater than that of the thrown card. /// Also this method fails fast once a set is identified to meet the criteria then the method returns /// without checking for other possible sets. /// </summary> /// <param name="set">The set of cards to select choices from</param> /// <param name="target">The target card to check value against</param> /// <returns> /// Flag indicating if the given set of cards contains a subset of cards whose values sum to the value /// of the target card. /// </returns> private static bool Enumerate(List <Card> set, Card target) { Debug.Assert(set != null); set = new List <Card>(set); // Remove all Cards of value greater than the target, combinations with those cards are impossible set.RemoveAll(delegate(Card c) { return((int)c > (int)target); }); if (set.Count > 1) { for (int k = 2; k <= set.Count; ++k) { ChoiceEnumerable <Card> enumerable = new ChoiceEnumerable <Card>(k, set.ToArray <Card>()); foreach (Card[] cards in enumerable) { if ((int)target == cards.Sum <Card>(a => (int)a)) { return(true); } } } } return(false); }