By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

evaluating strength of a poker hand

100+
P: 258
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
Jan 29 '08 #1
Share this Question
Share on Google+
24 Replies


kadghar
Expert 100+
P: 1,295
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
Jan 29 '08 #2

100+
P: 258
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
Jan 29 '08 #3

kadghar
Expert 100+
P: 1,295
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.
Jan 29 '08 #4

100+
P: 258
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
Jan 30 '08 #5

Expert 5K+
P: 8,434
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.
Jan 30 '08 #6

daniel aristidou
100+
P: 491
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?
Jan 31 '08 #7

Expert 5K+
P: 8,434
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.
Jan 31 '08 #8

100+
P: 258
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
Jan 31 '08 #9

kadghar
Expert 100+
P: 1,295
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.
Jan 31 '08 #10

Expert 5K+
P: 8,434
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...
Feb 1 '08 #11

100+
P: 258
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

Expand|Select|Wrap|Line Numbers
  1. For a = 1 To 47
  2.     For b = a To 47
  3.         For c = b To 47
  4.             For d = c To 47
  5.  
  6.                 If a <> b And a <> c And a <> d Then
  7.                     If b <> c And b <> d And c <> d Then
  8.  
  9.                           '** and here you can get possible hands**
  10.                           print a,b,c,d
  11.  
  12.                     End If
  13.                 End If
  14.  
  15.             Next d
  16.         Next c
  17.     Next b
  18. Next a
  19.  


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
Feb 2 '08 #12

Expert 5K+
P: 8,434
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?
Feb 2 '08 #13

100+
P: 258
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
Expand|Select|Wrap|Line Numbers
  1. Dim cards (7,2)
  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 :

Expand|Select|Wrap|Line Numbers
  1.     'sorting the array
  2.     While Sorted = 0
  3.         Sorted = 1
  4.         For i = 1 To 6
  5.            If Val(cards(i, 1)) > Val(cards(i + 1, 1)) Then
  6.                 temp = cards(i, 1)
  7.                 cards(i, 1) = cards(i + 1, 1)
  8.                 cards(i + 1, 1) = temp
  9.  
  10.                 temp = cards(i, 2)
  11.                 cards(i, 2) = cards(i + 1, 2)
  12.                 cards(i + 1, 2) = temp
  13.                 Sorted = 0
  14.            End If
  15.         Next i
  16.     Wend
  17.     'end of sorting the array
  18.  
  19.  
  20.  
  21.  
  22.     'check for four of a kind
  23.     For i = 1 To 4
  24.         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
  25.             result = "Strength : Four of a kind"
  26.             GoTo 1
  27.         End If
  28.     Next i
  29.     'end of check for four of a kind
  30.  
  31.  
  32.     'check for full house
  33.     For i = 1 To 7
  34.         fullhousecards(i, 1) = cards(i, 1)
  35.     Next i
  36.     For i = 1 To 5
  37.         If fullhousecards(i, 1) = fullhousecards(i + 1, 1) And fullhousecards(i, 1) = fullhousecards(i + 2, 1) Then
  38.             threeofakind = 1
  39.             fullhousecards(i, 1) = 100
  40.             fullhousecards(i + 1, 1) = 101
  41.             Exit For
  42.         End If
  43.     Next i
  44.     If threeofakind = 1 Then
  45.         For i = 1 To 6
  46.             If fullhousecards(i, 1) = fullhousecards(i + 1, 1) Then result = "Strength : Full House": GoTo 1
  47.          Next i
  48.     End If
  49.     'end of check for full house
  50.  
  51.  
  52.     'check for flush
  53.     flush1 = 0
  54.     flush2 = 0
  55.     flush3 = 0
  56.     flush4 = 0
  57.     For i = 1 To 7
  58.         If cards(i, 2) = 1 Then flush1 = flush1 + 1
  59.         If cards(i, 2) = 2 Then flush2 = flush2 + 1
  60.         If cards(i, 2) = 3 Then flush3 = flush3 + 1
  61.         If cards(i, 2) = 4 Then flush4 = flush4 + 1
  62.  
  63.         If flush1 > 4 Or flush2 > 4 Or flush3 > 4 Or flush4 > 4 Then
  64.             result = "Strength : Flush"
  65.             GoTo 1
  66.         End If
  67.     Next i
  68.     'end of check for flush
  69.  
  70.  
  71.     'check for Straight
  72.     straightcheck = 0
  73.     For i = 1 To 7
  74.  
  75.         If Val(cards(i, 1)) = Val(cards(i + 1, 1)) - 1 Then straightcheck = straightcheck + 1
  76.         If Val(cards(i, 1)) < Val(cards(i + 1, 1)) - 1 Then straightcheck = 0
  77.         If straightcheck = 3 And Val(cards(i, 1)) = 13 And Val(cards(1, 1)) = 1 Then straightcheck = 4
  78.         If straightcheck = 4 Then result = "Strength : Straight": GoTo 1
  79.     Next i
  80.  
  81.     'end of check for Straight
  82.  
  83.  
  84.  
  85.     'check for three of a kind
  86.     If threeofakind = 1 Then
  87.         result = "Strength : Three of a kind"
  88.         GoTo 1
  89.     End If
  90.     'end of check for three of a kind
  91.  
  92.  
  93.     'check for one / two pair
  94.     paircount = 0
  95.     For i = 1 To 6
  96.         If cards(i, 1) = cards(i + 1, 1) Then paircount = paircount + 1
  97.  
  98.     Next i
  99.     If paircount = 1 Then
  100.         result = "Strength : 1 Pair"
  101.         GoTo 1
  102.     End If
  103.     If paircount > 1 Then
  104.         result = "Strength : 2 Pair"
  105.         GoTo 1
  106.     End If
  107.     'end of check for one / two pair
  108.  
  109. 1
  110. print result
  111.  

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

Thanks / B...
Feb 2 '08 #14

Expert 5K+
P: 8,434
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.
Feb 3 '08 #15

Expert 5K+
P: 8,434
Some thoughts on the above code...
  • If the cards() array holds numbers, it should be of numeric format (preferably Long as mentioned above). Using the Val() function all the time adds heaps of processing overhead.
  • With 7 cards, couldn't you have 3 pair? (As you can see, I'm not a poker player)
  • Don't a straight or a flush (or a straight flush) outrank four of a kind?
  • You might save a little time by using a faster sort method. It looks as though you've coded a bubble sort. This is about the most commonly-written technique, because it's so simple and intuitive. But it's also one of the slowest available. Depending on the circumstances, of course - sorting is a complex subject. You might want to have a look at the Wikipedia article on sorting algorithms
  • For reasons of readability, I'd recommending you stick with either single-line or multi-line IF...END IF syntax, don't mix them. For example, line see 46 and lines 63-66. I believe the latter is much easier to follow.
  • Since you've already checked for three or four of a kind, in the pair-checking you might save a bit of time by skipping ahead one card after you detect a pair.
  • Is the sequence of the checks correct? Since you're stopping as soon as your detect something, presumably you need to check in reverse order of strength so that you always detect the strongest hand present. As I said I'm not a poker player (or even much of a fan) but it doesn't look to me as though this is what happens here.
  • I think the full-house (and three of a kind) check could be improved quite simply. Do the initial checking for three of a kind using the original array. If found, then if necessary copy the cards to your second array and continue checking. Given that the majority of hands (I assume) won't contain three of a kind, this would usually skip a lot of the overhead.
  • If you're really looking to squeeze out every last processor cycle, there are further tweaks which may sound silly. For example, define a Const One as Long = 1 and use that everywhere in place of the literal value 1. Similarly for other commonly-used numbers. Believe it or not, it can be slightly faster to access a constant like this than a literal.
  • You can't quote 15,000 hands a second then talk about once in 72,000 hands as though it's a rare thing. That's an average of about once every 4-5 seconds. Basically, in programming you usually need to take into account what is possible, not just what is likely.
  • Even if you stick with the bubble sort, I think we can tweak it slightly for performance. There's no need to loop through the entire array each time 'round the While loop. Depending on which direction your values "bubble", you can start or stop a bit further along the array each time to reduce overhead.
  • I think there are still areas where we can rewrite these checks to reduce processing - possibly by combining some of the checks. A bit will depend on the sequence of strengths I mentioned above, though.
  • Some checks such as for pairs you might be able to incorporate into other places, such as the sort. Since you're already looping through the whole array (and the last time through the sort loop, they are in sequence) you might be able to skip one entire loop in the majority of cases, at the expense of some unnecessary checking in cases where there's a stronger hand. (In other words, if you hit a flush, you no longer care that you earlier found a pair. But this usually won't happen). The idea is that you cut down on the work which is almost always done, at the expense of some extra overhead in the rare cases.
Feb 4 '08 #16

100+
P: 258
thanks Killer42 for helping.. I really appreciate
As I said I'm just a beginer in programming.. I'll read your post and do like you say... I hope my program gets faster

I'll post the answer about what happens

Thanks again
B...
Feb 4 '08 #17

100+
P: 258
Hi Killer42
I'v read your post .. It really helped. After some changes I could reach 22.800 h/s ( with 3.06 Intel processor ) which is 7.300 hands more than the previous program.
The order I check the hands should be as it is.So I skip checking weaker possibilities as soon as I find one. For example if I find a (Two pair) then I don't need to check for (one pair). Like you said it increases the speed of evaluating.
And in my system there is no need to check for (Royal flush) and (Straight flush ) because the probablity to get them is 1 in more than 100,000 hands
Here is the order of strengths in poker with probablity of getting each one of them

Royal flush (Best) (1 in 650,000 hands)
Straight flush (1 in 72,000 hands)
Four of a kind (1 in 4,200 hands)
Full house (1 in 700 hands)
Flush (1 in 510 hands)
Straight (1 in 250 hands)
Three of a kind (1 in 48 hands)
Two pair (1 in 21 hands)
One pair (1 in 2.4 hands)

And if none of them happens then the person with highest card wins. But we can forget that one because people who use a poker calculator don't risk their money with having a high card

(Quote: With 7 cards, couldn't you have 3 pair? ) The answer to this question is ,Yes you can get 3 pair but only 2 of them will be counted. The probablity of getting 3 pair might be less than (Three of a kind) So it must be stronger but not in poker rules

After reading your post I realized that there must be many ways to increase the speed , even with changing whole checking parts.
So I'll work more on your opinions. It seems that there are many facts in programming that I don't know about

Thanks again for posting

B...
Feb 4 '08 #18

daniel aristidou
100+
P: 491
Just a question ...what are you developing this game for...and more importantly who?.....by not checking for a royal flush......you might be viewed by the players as..kinda cheating at you own game....Please note this is not an accusation...just pointing out.. if i was betting using this game and got a royal flush i would gamble alot on it... so by not checking.....someone could lose alot
Feb 4 '08 #19

100+
P: 258
Just a question ...what are you developing this game for...and more importantly who?.....by not checking for a royal flush......you might be viewed by the players as..kinda cheating at you own game....Please note this is not an accusation...just pointing out.. if i was betting using this game and got a royal flush i would gamble alot on it... so by not checking.....someone could lose alot
Hi daniel and thank you for posting
As I said in my previous posts I'm not making a poker game..
You know that there is many poker calculators to download. Those programs are to help a player to see his/her chance to win. Some people call it "Cheating" but I don't think it is.. Because it just shows the probablity of winning and it's never at 100%.
what these programs do is that they check thousands of different possibilities to find out if the person is more a loser or a winner.
You have 2 cards in your hand and there is 5 cards that all of player can see so there is 45 cards left.
This program checks (45 * 45) differert possibilities to find out strength of your hand comparing with other players
If you use it after 4th card of flop then it will be (45 * 45 * 45)
or after 3rd card of flop will be (45 * 45 * 45 * 45)

Do you think it's cheating?

Thanks / B...
Feb 4 '08 #20

daniel aristidou
100+
P: 491
well at maths yes.... in the game no....
well actually kind of...i don't know
I believe its up to the opponent to decide whether they accept an opponent using a possibility calculator...Sorry for the confusion
Feb 4 '08 #21

Expert 5K+
P: 8,434
...find out if the person is more a loser or a winner.
Oh, I get it. Like on, say, World Poker Tour where they show the percentage chance each player has of winning the pot.

Cool !
Feb 5 '08 #22

Expert 5K+
P: 8,434
One other question. When you achieved 22,800 hands per second, was this running in the IDE (Integrated Development Environment) or compiled? Because you'll find that you get the best performance out of your program once it's compiled, to native code, with all optimisations turned on under the "Compile" properties tab.

(Not sure how familiar you are with VB, so I'll point out that compiling it won't make any difference to what happens if you run it again from the editor. You'll have to go to Windows Explorer and run the Exe file that was created - that's the "real" compiled code.)
Feb 5 '08 #23

100+
P: 258
Ok .. I found a way to make this programs 9 times faster..
I changed the whole system
In my new program there is no need to sort cards for checking. I just put them in an array like "dim cards(13)" without suits

By the way .. I don't know why but when I run the exe file it's 2 times faster than running from the visual basic compiler..

And I have another question :P .. Actually I posted this question and got no answer.
Any suggestion will be helpful

You may have seen some programs that read values from external windows...
For example when you open a poker calculator while you are playing poker the calculator reads your cards from the poker program.. (Those are two different programs)
Does anyone have any idea how it can be possible?

Thanks
Feb 5 '08 #24

Expert 5K+
P: 8,434
Excellent! I knew there had to more efficient ways to do the job, but haven't had time to look into it in depth.

As for picking up info from the poker program, well... that's another story entirely. Communication between programs is a big subject area. There are tons of techniques which may be in use. The simplest would be if the poker program exposes its workings through an automation interface.

If the poker program in question isn't designed to share information with other programs, then you may have a very difficult task.

I'd suggest you try some searches on this sort of topic. Or even read your VB documentation. You might want to include search terms such as...
  • API
  • automation
  • OLE
  • ActiveX
  • COM
Feb 5 '08 #25

Post your reply

Sign in to post your reply or Sign up for a free account.