Sign in to follow this  
tsvedin

Programming Poker (BASIC)

Recommended Posts

Let me start by asking you to excuse my poor english.. : ) I'm planning on programming my first Texas Hold 'Em game.. I have long wondered how you rank hands.. How do I know if a client have a Full House for example? Is there a simple way to do this using a BASIC language? I'm not working on a commercial product so the code doesn't need to be fast.. just fast enough to do the job :-)

Share this post


Link to post
Share on other sites
Unfortunately, you will have to be more specific in order to get a satisfactory answer. Your post doesn't really tell us much. However, I'm going to assume that you are using the client/server model for your game (you should) and that the server is authoritative over all clients (it should be). If that is the case, the server will generate the hand of each player, and then pass this information to the corresponding player's client. Under no circumstances shall the client be allowed to generate hands or anything else that modifies the outcome of the game. Violating this rule will inevitably lead to cheating.

In essence, what you do when a round has begun is this:
1. You deal a hand to each player by generating the appropriate cards on the server.
2. You send each player his/her hand over the network. Do not send it to anyone else.
3. The player's client receives this information and displays the hand on the player's screen.
4. The player may choose (unless s/he has put in blinds) whether or not to join the game. This decision is sent to the server.
5. Based on this information, the server either removes the client from the current round or lets it stay there.

Of course, this needs to be expanded upon before you can have a complete poker game, but the point I'm trying to make is that the client should merely act as a dumb bridge between the player and the server; never should it be allowed to view or modify information that could enable cheating (such as seeing other players' hands, generating its own hand, etc).

P.S: Good to see another fellow swede around here. :)

Share this post


Link to post
Share on other sites
Actually, his question was quite specific: he wants to know how to rank poker hands in the Texas Hold'em version of the game.

I don't think this is a particularly hard problem, but it takes some work. In order to detect full houses, for instance, first make sure that you don't have something better. Then you can loop through your 7 cards and keep accumulators that count how many cards of each kind you have. If the maximum counter is not 3, you don't have a full house. If it's 3, pick the highest kind for which the count is 3 and check if there is another kind for which the count is at least 2 (pick the highest kind if there are multiple). If everything checked so far, you have a full house.

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
Actually, his question was quite specific: he wants to know how to rank poker hands in the Texas Hold'em version of the game.

I don't think this is a particularly hard problem, but it takes some work. In order to detect full houses, for instance, first make sure that you don't have something better. Then you can loop through your 7 cards and keep accumulators that count how many cards of each kind you have. If the maximum counter is not 3, you don't have a full house. If it's 3, pick the highest kind for which the count is 3 and check if there is another kind for which the count is at least 2 (pick the highest kind if there are multiple). If everything checked so far, you have a full house.


Ok, I think I got it.. But it will be one hell of a loop to calculate this I guess... :-) I will try to code this idea when I come home tonight and see if I understood you right. Thanks :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by alvaro
Actually, his question was quite specific: he wants to know how to rank poker hands in the Texas Hold'em version of the game.


I was referring to the lack of information as to how his current implementation (if any) worked. I was hoping to be able to deduce his network/security savvy-ness from this information. To clarify: the information I sought for was not related to his problem, but rather to his (planned or current) implementation of the core game. I'm sorry for not being clear enough.

Share this post


Link to post
Share on other sites
You might find this article useful for getting it to go at a decent speed. Although the code there is in C the explanation of how it all works is very clear so you should be able to convert it to your favourite language.

http://www.suffecool.net/poker/evaluator.html

Share this post


Link to post
Share on other sites
I've recently done this and, if I remember correctly, this is how I went about it (I'm at work and don't have the code for reference, but will check back later anyway)...

I began by sorting the cards by value so that straights are easily detected and same-value sets are easily found. Next I generated a list of same-value runs found in the sorted copy of the hand so if the hand contained A 2 5 K 5 the sorted hand would be A 2 5 5 K and the same-value runs list would be 1 1 2 1 (where each number represents the same-value run length, 2 representing the two fives). Finally I had a function to check for a flush - all five cards having the same suit. Then it was simply a case of checking for each possible hand in order from best to worst.

Royal Flush -> Must be a flush and and ace high straight (A 10 J K Q if aces are sorted as low)
Straight Flush -> Must be a flush and five consecutively valued cards (don't forget ace can be high OR low)
Four of a Kind -> Two same-value runs of length 4 and 1
Full House -> Two same-value runs of length 3 and 2
Flush -> Simply check for a flush.
Straight -> Five consecutively valued cards (ace can be high or low)
Three of a kind -> Three same-value runs of length 3, 1, 1
Two Pairs -> Three same-value runs of length 2, 2, 1
One Pair -> Four same-value runs of length 2, 1, 1, 1

I never got around to improving the system to work with 7 cards and choose the best 5 (as required for Texas Hold 'Em), but hopefully this will give you a strong starting point!

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam_42
You might find this article useful for getting it to go at a decent speed. Although the code there is in C the explanation of how it all works is very clear so you should be able to convert it to your favourite language.

http://www.suffecool.net/poker/evaluator.html


Wow! That code will be easy to translate to BASIC !! :) Thanks sooo much! :D I never got the time yesterday to sit down and code but I will make some time tonight! Screw girlfriend and tv... Protean IDE.. I'm all yours tonight.. lol

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this