A famous casino is suddenly faced with a sharp decline of their revenues. They decide to offer Texas hold'em also online. Can you help them by writing an algorithm that can rank poker hands?
Create a poker hand that has a method to compare itself to another poker hand:
PokerHand.prototype.compareWith = function(hand){...};
A poker hand has a constructor that accepts a string containing 5 cards:
var hand = new PokerHand("KS 2H 5C JD TD");
The characteristics of the string of cards are:
- Each card consists of two characters, where
- The first character is the value of the card: 2, 3, 4, 5, 6, 7, 8, 9, T(en), J(ack), Q(ueen), K(ing), A(ce)
- The second character represents the suit: S(pades), H(earts), D(iamonds), C(lubs)
- A space is used as card separator between cards
The result of your poker hand compare can be one of these 3 options:
var Result =
{
"win": 1,
"loss": 2,
"tie": 3
}
Notes
- Apply the Texas Hold'em rules for ranking the cards.
- Low aces are NOT valid in this challenge.
- There is no ranking for the suits.
Examples
("2H 3H 4H 5H 6H", "AS AD AC AH JD") => WIN "Straight flush wins of 4 of a kind"
("2H 3H 4H 5H 6H", "KS AS TS QS JS") => LOSS "Highest straight flush wins"
Tests
("2H 3H 5H 6H 7H", "2S 3H 4H 5S 6C")
("2S 3H 4H 5S 6C", "3D 4C 5H 6H 2S")
("2S AH 4H 5S KC", "AH AC 5H 6H 7S")
Good luck!
This challenge comes from FrankK on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (6)
Sad this is a bit too difficult for me to do with Befunge, even Funge++, but I did think it could work quite well if done functionally.
This C# solution seems to work, have not tested beyond the initial to test cases
Edit: Did not add HighCard rules....
I think I also missed flush with high card... hrmm
Created a python version
link to gist if embed isn't working
use it like:
Real world Javascript map/reduce, solving the Poker Hand problem
Mike Talbot ・ Jun 16 ・ 6 min read
I once implemented with Node.js. It was a little bit complicated as I remember. I think there is no quick and easy solution.