evaluating strength of a poker hand

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bnashenas1984
    Contributor
    • Sep 2007
    • 257

    evaluating strength of a poker hand

    Hi every one
    I'm trying to make a little poker game but I don't know how to evaluate the strength of a 7 card hand..

    It's not that hard with 5 cards. Actually I found some program to do that with 5 cards but the problem is that there is 5 flop cards and 2 cards that the player has in hand.

    I don't even know how to start that .... (identify cards by numbers? 1 to 52) or (identify them by both numbers and suits)

    Any suggestion will be helpful

    Thanks
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by bnashenas1984
    Hi every one
    I'm trying to make a little poker game but I don't know how to evaluate the strength of a 7 card hand..

    It's not that hard with 5 cards. Actually I found some program to do that with 5 cards but the problem is that there is 5 flop cards and 2 cards that the player has in hand.

    I don't even know how to start that .... (identify cards by numbers? 1 to 52) or (identify them by both numbers and suits)

    Any suggestion will be helpful

    Thanks
    no, just think the hand is an array (7,2)

    where the first dimension is the card, and the second dimension will be (1 the suit, 2 the number)
    so if you want someone to have a poker of A's and other 3 cards, it'll look like:

    arr(1,1) = 1 , arr(1,2) = 1
    arr(2,1) = 2 , arr(2,2) = 1
    arr(3,1) = 3 , arr(3,2) = 1
    arr(4,1) = 4 , arr(4,2) = 1
    arr(5,1) = any suit , arr(5,2) = any number
    arr(6,1) = any suit , arr(6,2) = any number
    arr(7,1) = any suit , arr(7,2) = any number

    i think the algorithm to check them wont be short, but it seems doable using some FOR to sum and some SELECT CASE
    give it a try.

    HTH

    Comment

    • bnashenas1984
      Contributor
      • Sep 2007
      • 257

      #3
      Thanks for the reply
      I realy need to get this program working as soon as possible.
      I'll try to do it like you said.

      Thanks again

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        Originally posted by bnashenas1984
        Thanks for the reply
        I realy need to get this program working as soon as possible.
        I'll try to do it like you said.

        Thanks again
        Good luck with that.

        If you have any doubts while coding, we'll be glad to help you.

        Comment

        • bnashenas1984
          Contributor
          • Sep 2007
          • 257

          #5
          Hi again kadghar and thanks for the help
          It's working now.. I haven't tested the speed yet but it's working fine. the only hand I'm having problem with is full house..
          I'm working on it. I hope I can fix it.

          Thanks again

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            One method which would require a lot of initial setup but could work very fast is to define a seven-dimensional array. Assign a number to every one of the 52 cards. Then every element in the array would represent a possible hand, and would hold the score for that hand.

            Hahaha... just realised, the downside is that I think there are a bit over a trillion (1,000,000,000, 000) possible hands. This might cause some minor difficulties related to (A) array size and (B) time required to populate it.

            Well, I still think the idea was valid.

            Hm... no wonder we don't see much multi-dimensional array usage.

            Comment

            • daniel aristidou
              Contributor
              • Aug 2007
              • 494

              #7
              Originally posted by Killer42
              Hahaha... just realised, the downside is that I think there are a bit over a trillion (1,000,000,000, 000) possible hands. This might cause some minor
              Hehe Minor.......... ..... only a few huh?

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by daniel aristidou
                Hehe Minor.......... ..... only a few huh?
                Sure. It's just a matter of throwing some huge quantities of money into RAM and processor power, and everything will be fine.

                Plus, I suppose you might have to hire the population of India to enter the data.

                Comment

                • bnashenas1984
                  Contributor
                  • Sep 2007
                  • 257

                  #9
                  Thanks for posts
                  Actually my program is working fine now.. But there is a pronlem
                  I put cards information in an array like "deck(x,y)"
                  the first dimension shows the number of card and the other one shows the suit.
                  then i check for the strength
                  But I'm having problem with the speed. It's now 15,000 hands per second which is not enough for me
                  I think I need a faster way to sort the array.. The way I sort it takes long time. Does any of you know how to sort a 2 dimensional array in a fast way?

                  Thanks

                  Comment

                  • kadghar
                    Recognized Expert Top Contributor
                    • Apr 2007
                    • 1302

                    #10
                    Originally posted by bnashenas1984
                    Thanks for posts
                    Actually my program is working fine now.. But there is a pronlem
                    I put cards information in an array like "deck(x,y)"
                    the first dimension shows the number of card and the other one shows the suit.
                    then i check for the strength
                    But I'm having problem with the speed. It's now 15,000 hands per second which is not enough for me
                    I think I need a faster way to sort the array.. The way I sort it takes long time. Does any of you know how to sort a 2 dimensional array in a fast way?

                    Thanks
                    do you mean sort, like with bubble sort, sorting the first dimension first and then the seccond?,

                    why dont you use something like (to sort a one dimension array):

                    arr1(1 to n) 'where n is the number of hands, and the array values are from 1 to 230, where 1 is a pair of two, and 230 a royal straight flush

                    so lets say your first hand was a pair of 5, so arr1(1) = 4, and your seccond hand was a royal straight flush, so arr1(2) = 230.

                    Here you can use a bubble sort, or a cocktail sort or any Sorting Algorithm. I'd say that once you have arr1, you will be able to sort more than 15,000 hands in a seccond.
                    Now, i bet the method that will fill this array, measuring the strength of the hands, is quite slower. May be you want to work with that.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by bnashenas1984
                      But I'm having problem with the speed. It's now 15,000 hands per second which is not enough for me
                      Those have to be some almighty fast players you're dealing with...

                      Comment

                      • bnashenas1984
                        Contributor
                        • Sep 2007
                        • 257

                        #12
                        Actually I'm working on a poker calculator. I just got an idea for making a calculator other than normal calculators that people use.
                        But in order to do this I need to have a REALLY FAST hand evaluator.
                        I give you an example:
                        When the dealer shows the first 3 cards on the flop and the person you are playing with has 2 cards ( X and X ) then you need some program like what you see below

                        Code:
                        For a = 1 To 47
                            For b = a To 47
                                For c = b To 47
                                    For d = c To 47
                                        
                                        If a <> b And a <> c And a <> d Then
                                            If b <> c And b <> d And c <> d Then
                                                
                                                  '** and here you can get possible hands**
                                                  print a,b,c,d
                        
                                            End If
                                        End If
                                        
                                    Next d
                                Next c
                            Next b
                        Next a


                        178365 possible hands
                        now you know why I need a faster evaluator.
                        what I did was that I put cards in an array like this:
                        myarray( 13,4 )

                        the first dimension shows number of each card and the other dimension shows the suit...
                        then I sort the array by the first dimension to evaluate it faster.
                        The best record I could reach was 15,500 hands per second and it means it takes 11 seconds to calculate

                        Thanks for posts
                        Last edited by bnashenas1984; Feb 2 '08, 02:32 AM. Reason: CODE tag

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Could you show us the code, or is it too "sensitive" ?

                          I love optimising code to squeeze out a few more processor cycles. (Note I didn't say I'm any good at it, just that I love it.)

                          Oh, and if we're talking about performance optimisation I think we need to know what version of VB it is. Or did you tell us that already?

                          Comment

                          • bnashenas1984
                            Contributor
                            • Sep 2007
                            • 257

                            #14
                            First of all I have to say that I may be a professional poker player but I'm NOT a professional programmer. So don't blame me if my program doesn't look like what it should.
                            And I'm using Visual basic 6.0
                            This program is to check 7 card hands


                            First we put cards in an array like this
                            Code:
                            Dim cards (7,2)
                            First dimension shows the number of card which can be between 1 to 13 and the second dimension shows the suits ( between 1 to 4 )

                            then what we should know about hands to be evaluated faster is that when we find the stronger possibility then we can skip checking for weaker ones
                            Here's an exampl:

                            When we find a (Full house) then we don't need to check for ( Flush, Straight, 3 of a kind, 2 pair, 1 pair ) because the hand will be counted as a Full house

                            So as you can see in my program after each part of checking there is a line ( Goto 1 )

                            By the way. in my system we don't need to check for ( Royal flush AND Straight flush ) because the possiblity to get them is (1 time in 72000 hands) for Straight flush and ( 1 time in 650000 hands) for Royal flush

                            Here is the code to check the hand :

                            Code:
                                'sorting the array
                                While Sorted = 0
                                    Sorted = 1
                                    For i = 1 To 6
                                       If Val(cards(i, 1)) > Val(cards(i + 1, 1)) Then
                                            temp = cards(i, 1)
                                            cards(i, 1) = cards(i + 1, 1)
                                            cards(i + 1, 1) = temp
                                            
                                            temp = cards(i, 2)
                                            cards(i, 2) = cards(i + 1, 2)
                                            cards(i + 1, 2) = temp
                                            Sorted = 0
                                       End If
                                    Next i
                                Wend
                                'end of sorting the array
                                
                                
                                
                                
                                'check for four of a kind
                                For i = 1 To 4
                                    If cards(i, 1) = cards(i + 1, 1) And cards(i, 1) = cards(i + 2, 1) And cards(i, 1) = cards(i + 3, 1) Then
                                        result = "Strength : Four of a kind"
                                        GoTo 1
                                    End If
                                Next i
                                'end of check for four of a kind
                                
                                
                                'check for full house
                                For i = 1 To 7
                                    fullhousecards(i, 1) = cards(i, 1)
                                Next i
                                For i = 1 To 5
                                    If fullhousecards(i, 1) = fullhousecards(i + 1, 1) And fullhousecards(i, 1) = fullhousecards(i + 2, 1) Then
                                        threeofakind = 1
                                        fullhousecards(i, 1) = 100
                                        fullhousecards(i + 1, 1) = 101
                                        Exit For
                                    End If
                                Next i
                                If threeofakind = 1 Then
                                    For i = 1 To 6
                                        If fullhousecards(i, 1) = fullhousecards(i + 1, 1) Then result = "Strength : Full House": GoTo 1
                                     Next i
                                End If
                                'end of check for full house
                                
                                
                                'check for flush
                                flush1 = 0
                                flush2 = 0
                                flush3 = 0
                                flush4 = 0
                                For i = 1 To 7
                                    If cards(i, 2) = 1 Then flush1 = flush1 + 1
                                    If cards(i, 2) = 2 Then flush2 = flush2 + 1
                                    If cards(i, 2) = 3 Then flush3 = flush3 + 1
                                    If cards(i, 2) = 4 Then flush4 = flush4 + 1
                                    
                                    If flush1 > 4 Or flush2 > 4 Or flush3 > 4 Or flush4 > 4 Then
                                        result = "Strength : Flush"
                                        GoTo 1
                                    End If
                                Next i
                                'end of check for flush
                                
                                
                                'check for Straight
                                straightcheck = 0
                                For i = 1 To 7
                                
                                    If Val(cards(i, 1)) = Val(cards(i + 1, 1)) - 1 Then straightcheck = straightcheck + 1
                                    If Val(cards(i, 1)) < Val(cards(i + 1, 1)) - 1 Then straightcheck = 0
                                    If straightcheck = 3 And Val(cards(i, 1)) = 13 And Val(cards(1, 1)) = 1 Then straightcheck = 4
                                    If straightcheck = 4 Then result = "Strength : Straight": GoTo 1
                                Next i
                                
                                'end of check for Straight
                                
                                
                                
                                'check for three of a kind
                                If threeofakind = 1 Then
                                    result = "Strength : Three of a kind"
                                    GoTo 1
                                End If
                                'end of check for three of a kind
                                
                                
                                'check for one / two pair
                                paircount = 0
                                For i = 1 To 6
                                    If cards(i, 1) = cards(i + 1, 1) Then paircount = paircount + 1
                                    
                                Next i
                                If paircount = 1 Then
                                    result = "Strength : 1 Pair"
                                    GoTo 1
                                End If
                                If paircount > 1 Then
                                    result = "Strength : 2 Pair"
                                    GoTo 1
                                End If
                                'end of check for one / two pair
                             
                            1
                            print result

                            As I said I'm not a professional programmer and any suggetions to make this program faster will be helpful ...

                            Thanks / B...
                            Last edited by bnashenas1984; Feb 2 '08, 03:20 PM. Reason: wrong typing

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #15
                              I'll try to review this code at lunch time today (in a few hours). But in the meantime, I'll say this. For maximum speed in VB6, try to define all variables as Long, wherever possible. Assuming you're using a 32-bit processor (by far the most common type at present), the 32-bit "long integer" is the native data type. When you use other numeric types such as Byte or Integer, they have to be converted back and forth for each calculation. So even though they may save a tiny amount of RAM, they're not worth it if you need top performance.

                              If you define them as nothing in particular (by not specifying a type), they take the default type. Unless you have specified something like "DefLng A-Z", this will most likely be Variant. Which is the most "expensive" data type in terms of memory usage and (I think) conversion required.

                              Note, from a quick glance at the code I think we can come up with a few improvements.

                              Comment

                              Working...