Poker hand java program




















If we have a tie, the second value will have to determine the winner. How we find the second value will be unique to each type of hand: in a pair, the higher pair wins; in a straight, the higher top card wins, etc. If those values are equal, we move onto the next determining factor, like the highest card besides the pair, or the low pair of a two pair hand.

Some hands will only have two determining factors, like a straight. The first value would be a straight's spot in the rank of the poker hands greater than 3 of a kind, less than a flush , and the second value would be the top card in the straight 7, jack, king, etc. For a high card, we'd have 0, as high card is the lowest ranked hand, and the next 5 values would be the ranks of the cards in the hand in descending order. All righty, now with the dirty work, figuring out the actual value of our poker hand.

Let's start with a pair situation. How would we figure out if there is a pair? If we have two cards of the same rank. How will we implement this? We could cycle through the ranks, seeing if any of the ranks has two cards with its value.

But then, for 3 of a kind, we'd have to do the same thing again. How about we make an array of int s starting at 0 with 13 slots one for each rank , then go through the cards; with each card, we increment the appropriate index of the array.

Let's see a code representation:. All the code from this point on is put in the Hand constructor where our comment was.

For simplicity's sake, we've used card ranks starting at 1 for ace instead of 0 for ace. If we use 0 for ace, then we would be using 9 for 10, which is just confusing. Since our card ranks run , the first index of our array 0 will be empty. Okay, so now, we have our array of card ranks, now we need to find if there are actually any pairs.

We need to know if there is a pair, and if there is, what rank the pair is. So we make an int sameCards to record how many cards are of the same rank, and an int groupRank to hold the rank of the pair. We make an int sameCards because we may have more than two cards of the same value, maybe even 3 or 4 hopefully not 5, unless our processor is a crooked dealer.

We could've just made a bool isPair , but we want to know if there is a 3 or 4 of a kind as well. This will work fine if there's a pair, three of a kind, or four of a kind. But wait a second, let's say we have a full house. There is a pair of kings, so we record 2 as sameCards and 13 as groupRank.

But we keep going through the other ranks, and if there are 3 fives, then we overwrite sameCards with 3 since the number of cards of that rank is more than the current value of sameCards.

Similar situation: we have two pairs, it records the first pair, but not the other one. We can do hands with one group of cards, but not hands with 2. We need a way to keep track of at least two different groups of cards, tracking the number of cards and the rank of each.

Think about it a bit before moving on. Note : this is definitely the most intense logic of the program, so if you don't get this at first, don't worry.

The rest of the code is all a little easier, and you can come back to this part later. My solution: all right, we have to keep track of two ranks of cards and how many cards are of each rank, so we'll have two variables to represent the ranks largeGroupRank , smallGroupRank and two to represent the number of cards that have that rank sameCards , sameCards2.

If ranks[x] is greater than sameCards , we assign the data there; otherwise, if it is greater than sameCards2 , we assign the data there. Now, I'm sure you all saw the nested if , so I guess I might as well tell you what it is for. Say the if wasn't there: we find a pair of 8's, then we find three 5's. But the pair we found earlier is just overwritten and not recorded anywhere, when it should have been stuck into sameCards2. So, the if statement checks if sameCards was previously assigned to something before overwriting it, and if it was, we take care of that.

Sample run: We find 2 queens, so we record that value in sameCards , since 2 is more than the 1 we initialized sameCards with. Then, we find 2 7's, so we record that in sameCards2. We find 3 Jacks, record those in sameCards , then find 2 threes, so we record them in sameCards2. Two 8's, then three 4's.

We write the data from the two 8's into sameCards2 , then put the data from the three 4's into sameCards1. The goal is to get the best poker hand you can. You can click a card to throw it away and replace it with a new card. You can stop any time you wish. Watch the movies above to see two example games. In the first film, the player changes cards until they have a flush - a good result.

In the second film, they are trying for a full-house. But the 9 of diamonds comes up as a duplicate, so they lose. To run the program, you need to download and unpack this archive - it contains three Java classes and pictures of all the 52 cards in a deck.

Displays hands best-first. Goes a little further in also giving the ordered tie-breaker information from the wikipedia page. Programming note: the method used for analyzing hands that contain jokers are limited to a maximum of two jokers.

Create account Log in. Toggle navigation. Page Discussion Edit History. I'm working on modernizing Rosetta Code's infrastructure.

Add a comment. Active Oldest Votes. Ted Hopp Ted Hopp k 48 48 gold badges silver badges bronze badges. Grab a deck of cards. Remove the face cards and aces. Imagine you're playing poker. Give yourself a hand that's a four of a kind. Think about how you, as a person, would determine what you have. I would personally sort my hand, then count the incidences of each card.

If I have 4 of the same value, then I've got a four of a kind and can stop. Okay, now a straight. I'd again sort my hand.



0コメント

  • 1000 / 1000