I've verified that the wheel is detected, so I don't think it's that. But who knows. The code is in C#, and I would really appreciate it if someone could look through it and let me know if they see anything wrong, or if there are better ways to do it. Thanks!
The general idea is that I sort the cards in ascending order by value, then I count sequential orders up to 5. Values are from 0 - 12, 12 being an ace. Here's the code:
Code:
static bool isStraight(List<Card> cards, out List<Card> bestHand)
{
bestHand = null;
int straightCount = 1,
lastIndex = -1;
bool isWheel = false;
var sorted = (from x in cards orderby x.Value ascending select x).ToList();
for (int i = 0; i < sorted.Count; i++)
{
Card card = sorted[i];
if (i > 0) // Skip first card because we're starting straight count at 1 already
{
if (card.Value == sorted[i - 1].Value + 1) // Check the previous card to see if we add 1 to it, it matches the current value
{
if (++straightCount >= 5)
{
lastIndex = i;
}
}
else
{
straightCount = 1;
}
}
if (straightCount == 5)
{
break;
}
}
// Check for wheel
if (straightCount < 5)
{
if (sorted.Last().Value == 12)
{
for (int i = 0; i < 4; i++)
{
Card card = sorted[i];
if (i == 0)
{
if (card.Value == 0)
{
if (++straightCount >= 5)
{
lastIndex = i;
}
}
}
else if (card.Value == sorted[i - 1].Value + 1)
{
if (++straightCount >= 5)
{
lastIndex = i;
}
}
else
{
straightCount = 1;
}
}
if (straightCount >= 5) { isWheel = true; }
}
}
bool result = straightCount >= 5;
if (result)
{
bestHand = new List<Card>();
if (isWheel)
{
bestHand.Add(cards.Where(x => x.Value == 12).First()); // add the ace
bestHand.AddRange(sorted.GetRange(lastIndex - 3, 4));
}
else
{
bestHand.AddRange(sorted.GetRange(lastIndex - 4, 5));
}
}
return result;
}
Statistics: Posted by Watts — Thu Sep 25, 2014 11:50 pm
]]>