424,991 Members | 1,888 Online
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
24 Replies

 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

 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

 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

 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 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 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 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 : Expand|Select|Wrap|Line Numbers     '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... 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

 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

 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

 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