Download poker game Free Java Code

But every JavaScript function has a handy method apply which basically says, for this function apply the supplied array as an argument list. The poker library On to the code in question: I seem to have forgotten the same A-5 check for straight flushes somehow in getTypeDetail, suppose that won't matter with the new score computation though. Since our bit field s has a bit set for each rank, a straight would have 5 bits set in sequence. Leave a Reply Cancel reply Your email address will not be published. Unfortunately, JavaScript does not natively support tuples in this way Python, for instance, does.


Your Answer

In the browser with jQuery for some games I wrote. A foray into server side JavaScript via Node. Getting into the MVC flow with Angular. JavaScript is great, and I suspect Steve Yegge was right about it being the next big language. Of these Haskell is top of the list, but other functional languages like OCaml, Erlang and Clojure might get a look-in as well.

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.

This makes finding the maximum easy as we can use Math. But every JavaScript function has a handy method apply which basically says, for this function apply the supplied array as an argument list. The next method we need for Hand is one that looks for straights. It should count the longest number of consecutive ranks in a hand. A rank is an integer from There is no specific ordering on the cards of a Hand object yet, but it would be very handy for this method if we could look at the cards in increasing order of rank.

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.

I was proud of myself… but it was painfully slow. It wasn't just that the Commodore 64 had a whopping 1. Back in those days, I already had a curiosity for bit manipulations. What I didn't realize at the time, was what you could actually accomplish with all those 1s and 0s. I was yet to discover just how succinct one could be in a weakly typed language such as JavaScript. The fact that a language could allow you to freely slide between a float, an integer and back to boolean had the potential to satisfy my mission:.

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.

In contrast, integers use 32 bits in JavaScript. This is great for reading and setting individual bits, but this problem needed full access to 52 bits if it was to succeed.

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.

Here, v gets incremented by 0 because multiplying by o is a multiplication by 0. Some of you may have noticed that on the other side of the multiplication, is a division by 0. Now some languages are able to optimize and not evaluate the rest of the expression if there is a multiplication by 0, but not JavaScript.

Instead, JavaScript evaluates the entire expression. JavaScript nicely coerces this operation result back to a 0 and finally adds 1. This is actually achieved as a series of additions. The reason for this is that JavaScript only allows bit operation on integers.

This means we can only set the lower 32 bits, but this strategy needs to set any of 52 bits. I found this could be accomplished using addition. Since no bit would ever be set twice and the body expression would always yield a power of 2, it was safe to do this. The remaining iterations set i within the range of which are the indexes of the card array cs. Each iteration, our bit offset o gets set to the appropriate offset using Math. This is because JavaScript only allows bit shifting on integers.

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.