I'm developing a poker game which works out what hand you have made given the cards dealt out. I'm currently stuck on the logic for the straight (5 cards in an incremental sequence e.g. 3,4,5,6,7). The Card objects I'm using have a int property which represents its value (ace is 14 by default).
Here's what I have so far:
private static bool CheckForStraight()
{
List<Card> tmpCards = new List<Card>(_cards); //local copy of the cards
if (tmpCards.Count < 5) //need atleast 5 cards to make a straight
return false;
tmpCards = tmpCards.DistinctBy(v => v.CardValue).ToList(); //remove duplicate values
tmpCards = tmpCards.OrderBy(x => x.CardValue).ToList(); //order list
int count = 0;
if (tmpCards.Zip(tmpCards.Skip(1), (a, b) => (a.CardValue + 1) == b.CardValue).Any(x => x))
{
count++;
}
else
{
count = 0;
}
return false;
}
When this code is run, the linq expression will check if any of the card values in the list are in sequence... so if the list contains cards with values 2,3,5,8,11 it would +1 to count as 2 and 3 are sequential. So count is always either going to be 0 or 1 which is no use to me. Ideally I'd want count to be 5 if there are five cards in sequence. Then if count was equal to 5, I could go on and find the cards that make the straight.
(Would be even better if a linq expression could determine which 5 cards were in sequence, not sure if that is possible in one expression mind).
My question: How can my current linq expression be modified to get the result I need? Or can I be pointed in the right direction if I'm going about this the wrong way, thanks in advance.