17
1
Your friend invited you to a high-stakes poker game at the last minute and, as a computer scientist, you've decided to use your skills to gain an edge on the game. Your task will be, given 2 cards
(your hand) and 0, 3, 4 or 5 cards
(the dealt cards), you must decide what the best possible hand you can get will be. If all 7 cards are given as arguments, the answer is pretty clear. If given less, the problem becomes more complex. However, this isn't quite enough to give you the edge you're looking for, you must also calculate the best possible hand from the remaining cards to understand what your opponents could have as well.
Hold'em Refresher
If you don't know about hold'em, each player in the game starts with 2 cards as their 'hand'. Over the course of 3 'turns', additional cards are revealed to be shared between all players. The first turn, 3 cards are revealed. The second, 1 more, and the third time a final card is revealed. The two cards given first represent your hand, while the latter represents 0, 3, 4, or 5 cards given by the successive turns.
Possible Numbers:
[2,3,4,5,6,7,8,9,T(10),J,Q,K,A]
Possible Suits:
[S,C,H,D]
Full Deck:
[2S,3S,4S,5S,6S,7S,8S,9S,TS,JS,QS,KS,AS, # Spades.
2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC,AC, # Clubs.
2H,3H,4H,5H,6H,7H,8H,9H,TH,JH,QH,KH,AH, # Hearts.
2D,3D,4D,5D,6D,7D,8D,9D,TD,JD,QD,KD,AD] # Diamonds.
Hand Rankings:
1:Royal Flush (A-K-Q-J-10, all from the same suit).
2:Straight Flush (Sequential cards, all from the same suit).
3:Four-of-a-Kind (Self explanatory).
4:Full House (3-of-a-kind and a 2-of-a-kind).
5:Flush (All cards are from the same suit).
6:Straight (Sequential Cards, any suits).
7:3-of-a-Kind (Self explanatory).
8:2-Pair (Double 2-of-a-Kind).
9:Pair (2-of-a-Kind).
10:High Card (You have absolutely nothing except a single card).
Lets take an example or two and walk through them:
The simple example:
[AS, AC],[AH,AD,9S,9C,9H]
-> 3
(Four-of-a-Kind), 3
(Four-of-a-Kind)
The best possible hand you'll have in this setup is the four of a kind hand. The best possible hand your opponents could have is also a 4-of-a-Kind, as you cannot have a KQJ10 in their 2-card hands.
[5C,2C],[6C,4C,JH,JD]
-> 2
(Straight Flush), 3
(4-of-a-Kind)
You're at risk for a straight flush, but because you have the 2/5C in your hand, nobody else is due to you holding both middle cards. The best they can hope for is having 2 pocket Jacks and getting a Jack on the flop.
[JS,JC],[]
-> 1
(Royal Flush), 1
(Royal Flush)
No information you can use against them has been given, all you can tell at the moment is that they can only have a royal flush in diamonds/hearts, but it's just as possible that you get the royal flush as they do. In fact, all inputs where the flop has yet to happen should result in a 1-1 answer.
[2C,4S],[3C,7S,9D,AH,JD]
-> 10
(High Card), 7
(3-of-a-Kind)
This is an example of where you're absolutely screwed, and there's no possibility of a straight or a flush given the river. Meaning the best hand here is pocket aces resulting in a 3-of-a-Kind.
I/O Requirements
- Input must be separated between what is in your hand and what is public knowledge; it'll likely be easier this way regardless of implementation.
- Cards can be tuples or strings, up to you.
- The Hand and the Playing Field can be arrays or delimited strings.
- Output must be two indexes from the list of hands I've provided (E.G. [2,1]).
- It may be returned as part of a function, printed to console or output in a suitable manner.
- There must be two distinct values, one for your best hand, another for the best possible.
- 10 may be represented as a
T
or as10
, whichever makes sense for you. - Standard loopholes are disallowed.
Winning Criteria
- This is code-golf, lowest byte-count with a tie-breaker of post date.
It's superfluous, and I removed it. It doesn't really add anything, and most people comment below anyway. – Rɪᴋᴇʀ – 2017-01-31T20:20:59.450
2Shouldn't there only be one of
A
and1
in the possible allowed cards? Also I don't think there is really a compelling reason to require the face abbreviations over numeric values like11
. – FryAmTheEggman – 2017-02-01T01:34:30.6831
Related: http://codegolf.stackexchange.com/q/25056/47022, http://codegolf.stackexchange.com/q/6494/47022 and http://stackoverflow.com/q/3364869/4748017
– Herb Wolfe – 2017-02-01T03:29:16.4579I've never seen a deck with an
A
and a1
. Everything else looks good. – isaacg – 2017-02-01T03:58:37.2501For us non-Poker-players, please explain in the question that the second group of cards are shared between you and your opponent, whereas they have their own two cards that you can't see. A quick glossary defining the terms pocket, flop, and river would be helpful. – DLosc – 2017-02-01T04:08:31.897
1Also helpful: explaining the whole sequence of a round. (Each player starts with two cards known only to them, then three cards are dealt face-up, then a fourth, then a fifth, at which point each player forms a "hand" from any five cards out of the seven visible to them.) It might not be clear to someone why there are seven cards but a hand consists of five. – DLosc – 2017-02-01T04:13:09.250
1In your second example (i.e.
[10H,10C],[AH,9C,JC]
), shouldn't it output2
(Straight Flush),2
(Straight Flush) ? Imagine your opponent has[8C,7C]
in his hand, then the flop might be[6C]
and the river[5C]
, which would give him the following straight flush :[5C,6C,7C,8C,9C]
. – roberto06 – 2017-02-01T08:43:43.797Fixed all of those problems, jeez... Thanks sandbox Obama. – Magic Octopus Urn – 2017-02-01T14:16:10.187
1The player can't have a Straight Flush anymore in your 2nd example. (Even with QC at the river, he will still need 8C or KC.) – Arnauld – 2017-02-02T07:54:14.853
@Arnauld man, I can't even do examples right, this problem will be hard to program haha. – Magic Octopus Urn – 2017-02-02T14:11:50.453
[2C,4S],[3C,7S,9D,AH,JD]
should be (10,6) since the other player can get a straight with an 8 and a 10 – vroomfondel – 2017-06-21T16:11:39.497