private static bool ReplaceSetWithNewCards(Deck deck, Table table, IList<Card> set) { Card[] nextCards = deck.GetNext(3); if (!nextCards.Any()) return false; // Not enough cards left in deck // Loop through each card in the set looking for it's position on the table // And replace card with new card from the deck. for (int i = 0; i < set.Count; i++) { for (int j = 0; j < table.Cards.Count(); j++) { if (table.Cards[j] != set[i]) continue; Console.WriteLine(String.Format("Replace Card {0}, {1} with {2}", i, table.Cards[j], nextCards[i])); //TODO use list. Don't replace in array. //TODO look for more than one set //TODO if no sets, add 3 more cards //TODO try combinations of two and loop through remaining cards table.Cards[j] = nextCards[i]; break; } } return true; }
public void Play() { var deck = new ShuffledDeck(); var table = new Table {Cards = deck.GetNext(12)}; Hands = 1; while (true) { IList<Card> set = FindSet(table); if (set == null) break; // Game over, no sets found Console.WriteLine(String.Format("Match found: {{{0} {1} {2}}}", set[0], set[1], set[2])); if (!ReplaceSetWithNewCards(deck, table, set)) break; // Game over, no more cards Hands++; } Console.WriteLine(String.Format("Game Over: {0} hands played.", Hands)); Console.WriteLine(Hands < 24 ? "No more matches." : "No more cards."); Console.WriteLine("What's left on the Table?"); Console.WriteLine(String.Join(",", table.Cards.Take(4))); Console.WriteLine(String.Join(",", table.Cards.Skip(4).Take(4))); Console.WriteLine(String.Join(",", table.Cards.Skip(8).Take(4))); Console.WriteLine(); }
public void FindMatchingCombinationsAndReplaceCards() { var deck = new Deck(); var table = new Table { Cards = deck.GetNext(12) }; var combinations = new Combinations<Card>(table.Cards, 3); var sets = combinations.Where(IsMatch); var cards = sets.FirstOrDefault(); Console.WriteLine(String.Format("Match found: {{{0} {1} {2}}}", cards[0], cards[1], cards[2])); ReplaceSetWithNewCards(deck, table, cards); }
private static IList<Card> FindSet(Table table) { // Get all 3 card combinations and sets there within // http://www.codeproject.com/KB/recipes/Combinatorics.aspx var combinations = new Combinations<Card>(table.Cards, 3); IEnumerable<IList<Card>> sets = combinations.Where(IsMatch); // Return the first set found, return null if no sets return sets.FirstOrDefault(); }
public void GetSetCombinations() { var deck = new Deck(); var table = new Table { Cards = deck.GetNext(12) }; Combinations<Card> combinations = new Combinations<Card>(table.Cards, 3); foreach (IList<Card> c in combinations) { Console.WriteLine(String.Format("{{{0} {1} {2}}}", c[0], c[1], c[2])); } }
public void CanReplaceCardsInSet() { var deck = new Deck(); var table = new Table {Cards = deck.GetNext(12)}; Card[] nextCards = deck.GetNext(3); table.Cards[0] = nextCards[0]; Assert.AreEqual(table.Cards[0], deck.Cards[12]); table.Cards[1] = nextCards[1]; Assert.AreEqual(table.Cards[1], deck.Cards[13]); table.Cards[2] = nextCards[2]; Assert.AreEqual(table.Cards[2], deck.Cards[14]); }
public void FindFirstMatchingCombinations() { var deck = new Deck(); var table = new Table {Cards = deck.GetNext(12)}; var combinations = new Combinations<Card>(table.Cards, 3); foreach (IList<Card> c in combinations) { if (IsMatch(c)) { Console.WriteLine(String.Format("{{{0} {1} {2}}}", c[0], c[1], c[2])); break; } } }
public void CanDealFirst12CardsOntoTable() { var deck = new Deck(); var table = new Table {Cards = deck.GetNext(12)}; Assert.AreEqual(table.Cards.Count(), 12); }
private static IList<Card> FindSet(Table table) { var combinations = new Combinations<Card>(table.Cards, 3); var sets = combinations.Where(IsMatch); return sets.FirstOrDefault(); }
private static bool ReplaceSetWithNewCards(Deck deck, Table table, IList<Card> set) { Card[] nextCards = deck.GetNext(3); if (!nextCards.Any()) return false; for (int i = 0; i < set.Count; i++) { String before = String.Join<Card>(",", table.Cards); for (int j = 0; j < table.Cards.Count(); j++) { if (table.Cards[j] != set[i]) continue; Console.WriteLine(String.Format("Replace Card {0}, {1} with {2}", i, table.Cards[j], nextCards[i])); table.Cards[j] = nextCards[i]; break; } String after = String.Join<Card>(",", table.Cards); Assert.AreNotEqual(before, after); } return true; }