Real world Javascript map/reduce solving the Poker Hand Problem

 Real-world Javascript map/reduce to solve the Poker Hand problem


Maybe you, like me may enjoy trying to solve daily DEV challenges. I usually don't have time to solve more than two lines of fancy functions. Yesterday, however, the challenge was ranking poker hands. It struck me that this is one of those things that "should be" easy!


The end result works well, it's concise and easy to read (it's much shorter than any other suggested solutions).


We can certainly press into service map or reduce to get the information that we need. This is a great example of how you can use these tools in real life to solve multi-step problems.


The challenge

The goal is to rank 2 poker hands and then decide which one will win.


A string of 2 characters separated with a space is used to represent poker hands. For example, 2H represents the 2 of Hearts while TC represents the ten of clubs.


"2C5C3C4C6C" is a straight flush between the clubs.


However, ranking hands have hidden complexities. For example, you can use supplementary cards to solve a draw or use the face value for pairs.


The solution

This is the problem. We need a way to compare hands. When the hand rankings match, then we can determine a winner. If possible, we can also compare supplementary cards.


The challenge requires that there be no better suit than another. We suggest. Now all that's left is to make the result object out of the hand. This is where the fun begins!


Get the details of a Poker hand

In order to solve such problems, you must first determine the core data needed to resolve the problem. Our first task is to rank our hands.


Poker hands may be straights, flushes or combinations of multiple cards of the same face value. It is our job to first assemble the information from the input string. It is important to first decide how you want to organize your input.


Although we need both faces and suits, the only reason that suits matter to us is if they all look the same, there is no need for the suits and the face to be related. This makes parsing easy.


Convert the string into cards

Remove the face and the suit


If we want to be capable of sorting our face cards, they need to be easily compared. So it is not alphabetical. Therefore, we add a third step.


Make the face look like a normal person


The hand has 5 cards. At the end, we need a value to resolve draw. This must be a string. So we will classify our card faces into characters and then put them back in a string. Now, we want A, B, King, Queen, etc.

Here are the cards and faces extracted. Next, we map the faces to A by looking at their positions in the order string. Then, we subtract 77 from this value and turn that string back into a string. 65 is the code for "A" so we can create a string similar to it, starting with "A".


We also separated the suits and the faces, so we can proceed to the next step.


Creating comparable data

Ok, we must now generate additional data to be able to code some code to rank our hand.


Identify a flush

Identify a straight

Identify duplicate faces. This will be used for all types of hand

Identify a flush

It's easy once we have parsed the data, sorted the suits. If the suit entries are identical, we have a flush. why the vreduce should be used in texas holdem

Comments

Popular posts from this blog

Ini adalah tips terbaik untuk meningkatkan peluang Anda memenangkan lotre

Pertanyaan Bagus: Pilihan cepat vs. Pilih nomor lotre Anda sendiri

Tim Newfoundland dan Labrador Muda menikmati pengalaman Brier pertama mereka