Poker-AI.org
http://poker-ai.org/phpbb/

I have a bug detecting a straight but I can't find it. Help?
http://poker-ai.org/phpbb/viewtopic.php?f=22&t=2812
Page 1 of 1

Author:  Watts [ Thu Sep 25, 2014 11:50 pm ]
Post subject:  I have a bug detecting a straight but I can't find it. Help?

I've built a poker simulator, and every time I run few million hands, straights are consistently under detected by around 9/10th's of a full percentage point. That doesn't sound like much, but every other hand hits to an accuracy of about 1/1000th's of a percent from where it would be expected. So I can only assume there is a bug in my straight detection.

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;
        }

Author:  proud2bBot [ Fri Sep 26, 2014 12:12 am ]
Post subject:  Re: I have a bug detecting a straight but I can't find it. H

Just get a hand evaluator, generate all random hands and boards, call your function with the generated hand/board and the evaluator and check if both say that you have a straight. If there is a difference, you have a specific hand/board and can just debug step by step through your code to see what's going on.

Author:  Watts [ Fri Sep 26, 2014 1:47 am ]
Post subject:  Re: I have a bug detecting a straight but I can't find it. H

That's a good idea. Can you recommend one that I can can use with automation?

Author:  spears [ Fri Sep 26, 2014 6:14 am ]
Post subject:  Re: I have a bug detecting a straight but I can't find it. H

Maybe you can use http://poker-ai.org/archive/www.pokerai ... ?f=3&t=353 or http://www.codeproject.com/Articles/122 ... d-Analysis

Author:  algonoob [ Sat Sep 27, 2014 9:50 pm ]
Post subject:  Re: I have a bug detecting a straight but I can't find it. H

see if it works for this case: {2,2,3,4,4,5,6}
a way to solve it is remove duplicates before processing

Author:  shalako [ Mon Sep 29, 2014 4:57 pm ]
Post subject:  Re: I have a bug detecting a straight but I can't find it. H

Another way to do this quickly is use Timmys evaluator that is available in the archives somewhere. It has a hand state function in which you can just pass the board and your hand and it will tell you if you have a str8 or not. I have used this on all my bots and it works pretty well.

Author:  Watts [ Mon Sep 29, 2014 5:02 pm ]
Post subject:  Re: I have a bug detecting a straight but I can't find it. H

algonoob wrote:
see if it works for this case: {2,2,3,4,4,5,6}
a way to solve it is remove duplicates before processing



Wow, thank you! That appears to be the problem! It's detecting that pattern as a two pair. Thank you for the suggestion, and great catch!! :D

Author:  Watts [ Mon Sep 29, 2014 5:03 pm ]
Post subject:  Re: I have a bug detecting a straight but I can't find it. H

shalako wrote:
Another way to do this quickly is use Timmys evaluator that is available in the archives somewhere. It has a hand state function in which you can just pass the board and your hand and it will tell you if you have a str8 or not. I have used this on all my bots and it works pretty well.



I like writing everything myself, but I think it would be good to incorporate something like this as a way to test the accuracy of it. I'm definitely going to check it out, thank you.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/