Question : Poker Hand Evaluation in VB.NET

I'm writing a game of Texas Hold'Em in VB.NET and C#, and need to build some logic on calculating the resulting poker hand from a player's two cards and the cards on the table (totalling 7 in Texas Hold'Em).

For example:
Players cards: 7H, JS
Cards on table: TD, 2H, 8H, 7C, 9S

In this case the player has a Jack high straight - 7H, 8H, 9S, TD, JS. The algorithm also has to discern that even though the player also has a pair (7H, 7C) the real hand is the straight.

I tried writing my own logic, but it ended up being 250+ lines of code by the time it could detect a royal flush. I've seen some code around that is meant to do it, but most of it is either in C/C++ or for other versions of poker.

I can write my own logic to discern a winner from a list of hands (i.e. that 3 of a kind beats two pair, and that an ace high straight beats a ten high straight). However, what I'm looking for is a function (or class) written in C# or VB.NET to return a poker hand (e.g. "Two Pair - 3s and Jacks" or "Four Aces") from a set of cards.

I've attached the VB.NET code for my structures and enums, but I can easily convert from another result format.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
Private Enum CardSuits As Integer
        Spade = 4
        Club = 3
        Diamond = 2
        Heart = 1
    End Enum
 
    Private Enum CardValues As Integer
        AceLow = 1
        Two = 2
        Three = 3
        Four = 4
        Five = 5
        Six = 6
        Seven = 7
        Eight = 8
        Nine = 9
        Ten = 10
        Jack = 11
        Queen = 12
        King = 13
        Ace = 14
    End Enum
 
    Private Enum HandTypes As Integer
        HighCard = 0
        Pair = 1
        TwoPair = 2
        ThreeOfKind = 3
        Straight = 4
        Flush = 5
        FullHouse = 6
        FourOfKind = 7
        StraightFlush = 8
    End Enum
 
    Private Structure Card
        Public Suit As CardSuits
        Public Value As CardValues
 
        Public Overrides Function ToString() As String
            Dim ret As String = ""
            If Value <= CardValues.Ten Then
                ret = Value
            Else
                Select Case Value
                    Case CardValues.Jack
                        ret = "Jack"
                    Case CardValues.Queen
                        ret = "Queen"
                    Case CardValues.King
                        ret = "King"
                    Case CardValues.Ace
                        ret = "Ace"
                    Case Else
                        ret = "Error"
                End Select
            End If
            Select Case Suit
                Case CardSuits.Club
                    ret &= " of Clubs"
                Case CardSuits.Diamond
                    ret &= " of Diamonds"
                Case CardSuits.Heart
                    ret &= " of Hearts"
                Case CardSuits.Spade
                    ret &= " of Spades"
                Case Else
                    ret &= " of Error"
            End Select
            Return ret
        End Function
    End Structure

Answer : Poker Hand Evaluation in VB.NET

I know how to compare the hands, you seem to have misunderstood.

The issue is that I've had to re-write the order of things. First I have to look for flushes. If there's a flush, check if a straight can be made out of thel cards that match the suit that the flush is in. If so, then we have a straight flush.

All cards being checked for a straight must always be sorted into order, so that the highest possible straight can be determined. For example:

1) Qh, Js, 10d, 8d, Ks, 2s, 9c is a straight of 9, 10, J, Q, K. However, it also has a straight of 8, 9, 10, J, Q. In order to properly evaluate the hand we re-order the input to Ks, Qh, Js, 10d, 9c, 8d, 2c. It is then trivial to check that there is a sequence beginning with Ks that decrements 5 times. This means that the false Jack high straight is not returned.

2) 6h, 7h, 6d, 9c, 8d, 5h, 8s is a straight of 5, 6, 7, 8, 9. The issue here is that there is a duplicate 6 and a duplicate 8. So, when in order it would be viewed as 9c, 8s, 8d, 7h, 6d, 6h, 5h. There is no simple decrementing pattern here. So, what I had to do is alter my algorithm to ignore cases where number pairs are equal:
9->8 decrements, 8->8 ignored, 8->7 decrements, 7->6 decrements, 6->6 ignored, 6->5 decrements.
There are 4 decrements linking 9 to 5 without a broken link (i.e. 7->4 does not match b = a-1), therefore there is a 9 high straight here.

I have no proiblem understanding the logic behind it, I was asking if anyone knew where I could find a pre-written algorithm to save me the time and effort.
Random Solutions  
 
programming4us programming4us