Sign in to follow this  
Brash Benson

Poker game help

Recommended Posts

Hi all, I'm currently making a simple console-based Poker game in Java, and I'm having trouble when it comes time to check hands to determine the winner. Anybody have any suggestions how I should go about checking for 2 of a kind, 3 of a kind, 2 pair, flush, etc? I have some basic ideas, but they involve running tons of different checks and hundreds of lines of code (i.e. looping through the hand to see how many of each suit/value is there, etc). Am I on the right track?

Share this post


Link to post
Share on other sites
Well, I am not a java nor poker expert, but I'd imagine it'd go exactly how you feared it go, but you can cut some corners.


You can write a class, or an array (of card objects), or vector(java has it?) of the cards a player has. Then you can use a sorting method to get them in the right order. Once you do you can find if the first term is equal to second term, if not, 2nd term and 3rd term, etc etc. If the 1st and 2nd are alike, check to see if 3rd is the same as them, and so on.

That will be for checking for pairs, 3 of a kind, 4 of a kind. I dont know the other card combos, but the special ones will probably be the "whole bunch of testing and hundreds line of code", but not quite as bad as you think.

A straight is 4 or 5? in a increasing value line.. or something like that, right? Well you can check if the terms are 1 value ahead of the previous. I think something like this is a royal flush, only more conditions. So you can use the same function for a royal flush or whatever, and then just add the extra checks to it.

Im no expert in poker nor java, so my methods might be wrong.

Share this post


Link to post
Share on other sites
No, you pretty much have the right idea. I'm no Poker expert either, but I know the basics and that sounds about right. I just wish there was an easier way to do it than "if card 1 is different than card 2, 3, 4, and 5, check the next card against all others, etc". What a pain :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Brash Benson
No, you pretty much have the right idea. I'm no Poker expert either, but I know the basics and that sounds about right. I just wish there was an easier way to do it than "if card 1 is different than card 2, 3, 4, and 5, check the next card against all others, etc". What a pain :(


Some pseudo-code

straights:
val = card.value();
int straightCount = 1;
while(in_array(++val,otherCardsValues))
{
straightCount++;
}
val = card.value();
while(in_array(--val,otherCardsValues))
{
straightCount--;
}


And this in a loop so you can do this for cards 1->4. I can't think of anything better, but it doesn't look like a real pain to me, I've written worse.
Checking on equal would be quite more easy i'd guess.

edit: Ow, I used pseudocode for the in_array because i'm not sure java support such a function (I don't program in java often), it isn't hard to implement one yourself however.
edit nr.2: this code only lets you see there is a straight, not which cards create it.
edit nr.3: as i was to stupid to think of ordering cards in advance myself (look post beneath this one), the pseudo code i posted is easy to edit to create a (shorter) algorithm for straight-checking.

[Edited by - kire on September 6, 2006 2:09:16 PM]

Share this post


Link to post
Share on other sites
You're making the problem a lot harder than it really is. If you sort the cards in advance, you're looking at a few dozen lines of code. Poker is actually a pretty straight forward game in this regard...Cribbage, for instance, has three, four, and five card runs that have to be considered.

One important tip is to not check everything at once. That'll just complicate things. Have at least three separate functions...onee that checks for flushes, one that checks for straights, and one that checks for pairs and so forth. Maybe even a fourth just for full houses. Yeah, you could do all this in a single iteration with a bunch of flags, but that'll just make debugging that much harder.

Wilds complicate things, of course. As do games like Texas Hold'em, where you can choose which cards to build your hand out of. But if all you want to do is score a hand, then it's not hard at all.

CM

Share this post


Link to post
Share on other sites
What you said sounds about right. There are a few tricks though you can go to simplify the logic.

This isn't exactly the best way to do things, but should give you some ideas.

Object Card:
value (number)
suit (enumeration)

OK, lets go over poker

High Card
Pair
Two Pair
Three of a Kind
Straight
Flush
Full House
Four of a Kind
Straight Flush
Royal Flush

Value Array (1-13)
For each card in your hand, increase the array by 1.
Two = 1, Ace = 13

First thing. Sort the cards by value.

Before going over pairs, full house, etc, lets check straights & flushes, as those are the easiest things to check.

A straight: Card[1].value == Card[2].value - 1 == Card[3].value - 2 == Card[4].value - 3 == Card[5].value - 4
A flush: Card[1].suit == Card[2].suit == Card[3].suit == Card[4].suit == Card[5].suit

If you have a straight & a flush, you have a straight flush.
If you have a straight flush, check Card 5, if it == 13, you have a royal flush.

If you don't have a straight nor a flush, start checking for pairs.

Starting from Array[13], go down to 1. (Highest cards always beat lowest)
Check if the value > 0.

Structure Pairs:
Pair Group1:
Value
Number
Pair Group2:
Value
Number

Group 1 & 2 might not necessarily exist, so they have an initial value of NULL. The first value in the array which contains a value >= 2, assign the value of the card to Group1.value and the array value to Group1.number (number of cards). Keep moving down until you find another value >= 2. Fill that in as group 2. Break out of the loop now that you have two pair.

If Group 1 & 2 exist
Full House = (Group1.Number + Group2.Number) == 5
else you have two pair

If Group 1 only exists, check Group1.Number. If it = 3, you have 3 of a kind, else you have a pair.

If neither Group 1 nor Group 2 exist, you go into high card.

And you want a player object
Player:
Object Cards[5]
Object Hand (I'll let you figure this out, shouldn't be too difficult).

Now, this only covers a simple version of classic poker. If you want to go into different poker games, such as Hold'em, the above will have to be slightly modified, although this should help you get a basic understanding.

PS As Conner McCloud stated, it's really not too difficult. I doubt the main program would honestly be longer than this post.

Share this post


Link to post
Share on other sites
Keep in mind that many versions of Poker allow so-called "Ace low straights"
In other words you can use an ace like this 'A-K-Q-J-10' or like this '5-4-3-2-A'
The latter example is often called a "wheel"

Also don't forget about more complicated comparisons. Obviously three-of-a-kind beats a pair but how do you handle situations where both players have a flush? The answer is that the player with the high card wins. If the highest card in each player's hand is the same you go to the next highest card etc... If both players have a Full House you look at who has the highest Three-of-a-kind in his hand(ie KKK22 beats QQQAA). If they both have the same three-of-a-kind(which can happen in games with wildcards or community cards) you next look at who has the highest pair.

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