Download poker game Free Java Code
On to the code in question: The code provides the services needed to construct, evaluate and compare 5 card poker hands. It could be used to provide a range of poker software: The objects provided include: Client code can use the Deck to deal Hands, then use the Comparer to determine the best hand from a list.
The Hand object has the task of determining what type of five card poker hand it contains. Although a Hand consists of five cards, the library can be used for poker games with community cards, like Texas Holdem, by instancing each five card variation from seven cards as a hand and choosing the best of them via the Comparer.
Poker hands consist of things like straights, flushes, fullhouses and pairs. A dictionary used to name and assign relative values to the complete set of possible hands looks like:. There are three general attributes that different poker hands are made up of: This suggests three core functions are needed to determine a poker hand:. Using an OO approach an appropriate home for these methods is the Hand object. The description of these methods also reveals something about the Card object: The Hand constructor takes an array of Card objects and stores them.
Subsequent methods will operate on the card array to determine attributes of the hand. The first method required for the Hand object is one that will determine the maximum number of cards of the same suit.
The Card object provides a getSuit method, which returns a suit as an integer. If we examine each cards suit and count each value, then determine the highest count, we have the maximum number of cards of the same suit. Here is an implementation:. This method exploits the fact that the suit of a card is an integer from The suit int can be mapped into the counters array.
That would make looking for the longest run of consecutive cards fairly straight forward. It takes the rank of each card and saves it in an array, returning that array after sorting it. JS arrays have a builtin sort method, but by default the sort is performed lexographically in dictionary order.
We are dealing with numbers and want to sort numerically, which is handled by passing a custom comparer function to sort. Often this would be an in-line anonymous function, but as it will be used in a few places it is worth providing a named function. Now that the cards can be examined in order of rank, the max number of connected cards can be calculated as follows:.
This code is examining each card rank and comparing it to the previous card rank. If the current card rank is one more than the previous card rank then we have found a run. As we are interested in the maximum run of cards, a comparison to the current max is made, and updated if this is a new best. If two cards are not consecutive then the run is reset to 1.
After the loop max will be the longest consecutive run of cards, except for a special case. In poker, when making a straight, the ace can be high or low.
That is ten, jack, queen, king, ace is a straight where the ace plays as the highest possible card. But you can also use the ace in the straight ace, two, three, four, five , where it plays as the lowest possible card. Straights cannot be made that use both high and low cards, so queen, king, ace, two, three is not a valid straight, at least under normal poker rules. In this implementation the ace is given its high value So if the player enters 5 cards: Royal Flush It works fine at the moment, but being new to programming I was wondering if you can help me with better logic or any suggestions to improve my code.
High card'; return Result. Assignment of message before returning a value That function PokerHand. To change this, the case winHigh could be added to the result set: Use filter for pairs Instead of map with a conditional: I have updated the answer to explain a possible way of doing that. The result of this classification could look like: Each of the above numbers is less than , so you can write the numbers like this: Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. The code itself is not considered readable code and therefore perhaps not all that advantageous, but the journey while writing it, proved to be most useful. It was my obsession with bit manipulations, my desire to exploit weakly typed variables and a long-time mission to improve an age old poker algorithm that drove me down the path to poker hand analysis Nerdvana.
Although this was the original mission, it only loosely served as forward momentum. There is some real "magic" happening on this page. Sean had collected the best of the best as far as known bit manipulations and optimizations. The four main lines of code are presented here at the top of the rankPokerHand function:. The function takes two arrays. The cs array is an array of five integers that represent the rank of each card with Aces high 14 — as is typical by many algorithms dealing with cards.
The ss array is an array of five integers flag values that represent the suit of each card. At first glance, the code can look intimidating. Before I explain this code line by line, the main concept is that there are two bit fields being used to store the rank and rank counts of the poker hand. The first is the s bit field. The purpose of this bit field is to record a 1 bit for each rank — duplicates are lost.
Only 13 bits are needed as correspond with the card ranks 2 - Ace. There is no need to shift these bits to the 13 LSBs as the card rank values are not important here. It is the proximity to their neighbours that is. The second is the v bit field. The purpose of this bit field is to record the count of each rank.
There is more than one way to represent this information in a bit field, but ultimately what I wanted was one that could be summed for each rank using the modulus operator and an appropriate Mersenne number. Figuring this out is left to the reader, but essentially this has the power of summing each nibble in the bit field in parallel. I needed a scheme that would produce a unique value for all combinations of rank duplicates i.
Each nibble 4 bits is responsible for capturing the count of each rank. If two of a rank is found, the 2 lower bits of the nibble will be set. If three of a rank is found, the 3 lower bits will be set. From the previous examples, the value of the Ace nibble is 3 the two low bits are set. It follows that the value of the Jack nibble is 7 the three low bits are set and the value of the King nibble is 15 0xF.
If this is the case then feel free to read ahead to the Line-by-Line Code Breakdown. This standard defines many details, but most important is the way a number is stored in binary form. This is common practice for any signed number format. Next, 11 bits are dedicated to recording the exponent and the remaining bits 52 are used to record the normalized mantissa. I found a great webpage to help visualize this concept.
When you do, the floating point number is truncated to the lower 32 bits and implicitly converted to an integer. After some experimenting, I figured out that I could use standard mathematical operators to achieve the desired effect. Multiplication, division and modulo division were all supported on floats and could be set-up to mask, read and set bits as required.
We initialize our four local variables. The bit fields are v and s , our loop iterator is i and our bit offset is o. As described earlier, the s bit field is initialized with a bit set for each rank in our poker hand. The next line of code is a loop designed to fill the v bit field with the counts of the ranks in our poker hand:. The first iteration is a wash. The first iteration jumps directly into the body of the loop.
Even though the number is stored as a float, its bits can still be manipulated. The expression in the body nicely bit shifts the desired nibble down to the four LSBs. Here it can easily be masked with 15 0xF. Once the bits in question are isolated, 1 is added to the value. This has the effect of setting the next highest bit in the nibble.
Lastly, this value is multiplied by the offset o to bring it back to the correct position. The first operation is modulo division.