I am creating a poker system, and currently I am rationalizing my hand calculator.
The following code works:
public enum CARDS { None = 0, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace }; public enum SUITS { None = 0, Diamonds, Clubs, Hearts, Spades }; public class Card { public CARDS Val { get; set; } public SUITS Suit { get; set; } } public class IntIndex { public int Count { get; set; } public int Index { get; set; } } static void Test() { List<Card> cardList = new List<Card>(); cardList.Add(new Card { Suit = SUITS.Diamonds, Val = CARDS.Two }); cardList.Add(new Card { Suit = SUITS.Hearts, Val = CARDS.Four }); cardList.Add(new Card { Suit = SUITS.Clubs, Val = CARDS.Five }); cardList.Add(new Card { Suit = SUITS.Diamonds, Val = CARDS.Six }); cardList.Add(new Card { Suit = SUITS.Spades, Val = CARDS.Six }); cardList.Add(new Card { Suit = SUITS.Hearts, Val = CARDS.Seven }); cardList.Add(new Card { Suit = SUITS.Clubs, Val = CARDS.Eight }); // I have a processor that iterates through the above card list and creates // the following array based on the Card.Val as an index int[] list = new int[] {0,0,0,1,1,2,1,1,0,0,1,0,0,0}; List<IntIndex> indexList = list.Select((item, index) => new IntIndex { Count = item, Index = index }) .Where(c => c.Count > 0).ToList(); List<int> newList = (from i in indexList join j in indexList on i.Index equals j.Index + 1 where j.Count > 0 select i.Index).ToList();
However, I want to make it more compact, as in:
static void Test() { List<Card> cardList = new List<Card>(); cardList.Add(new Card { Suit = SUITS.Diamonds, Val = CARDS.Two }); cardList.Add(new Card { Suit = SUITS.Hearts, Val = CARDS.Four }); cardList.Add(new Card { Suit = SUITS.Clubs, Val = CARDS.Five }); cardList.Add(new Card { Suit = SUITS.Diamonds, Val = CARDS.Six }); cardList.Add(new Card { Suit = SUITS.Spades, Val = CARDS.Six }); cardList.Add(new Card { Suit = SUITS.Hearts, Val = CARDS.Seven }); cardList.Add(new Card { Suit = SUITS.Clubs, Val = CARDS.Eight }); List<Card> newList1 = (from i in cardList join j in cardList on i.Val equals j.Val + 1 select i).ToList();
The problem is that the poles are repeated. A separate as well as a custom comparison function does not work, as this violates the n + 1 join clause.
Another problem is the following list of cards:
List<Card> cardList = new List<Card>(); cardList.Add(new Card { Suit = SUITS.Diamonds, Val = CARDS.Two }); cardList.Add(new Card { Suit = SUITS.Hearts, Val = CARDS.Three }); cardList.Add(new Card { Suit = SUITS.Clubs, Val = CARDS.Five }); cardList.Add(new Card { Suit = SUITS.Diamonds, Val = CARDS.Six }); cardList.Add(new Card { Suit = SUITS.Spades, Val = CARDS.Six }); cardList.Add(new Card { Suit = SUITS.Hearts, Val = CARDS.Seven }); cardList.Add(new Card { Suit = SUITS.Clubs, Val = CARDS.Eight }); cardList.Add(new Card { Suit = SUITS.Diamonds, Val = CARDS.Jack });
I get a return list of 3Hearts, 6Diamond, 7Hearts, 8Hearts, since 2 and 3 are sequential.
What I really want is a list that returns consecutive cards of size 5 or more, or, even better, the top 5 cards of a continuous sequence. Thus, the list above will be empty, since there are no 5 consecutive cards in the input list.