Update/Bounty
In two different poker games the best 5-card hand is formed by picking the cards out of a 7 cards - 7-Card Stud and Texas Hold'em.
This site has a poker tag and many question that 'hit this up' from one angle or another. But it would be nice to have a 'go to link' with algorithms that calculate many of these questions in 'one swoop'.
It boils down to simply being able to calculate, given two players, who has the best hand. The algorithm must be able to form the 5-Card hand of each player and them be able to compare the two holdings. It is possible for the two hands to be tied - no winner.
Since starting this I've come up with something that will do the job, and I will definitely post it after putting in the finishing touches. But as a novice in Python, it would be instructive for me (as well as others) to see several solutions.
It appears after my web search and looking at the initial response to my question, that putting this together is not a 'walk in the park'. However, I can assure any skeptics, that the solution doesn't need to perform any I/O. In fact, as a novice, my code contains no import statements at all. By looking at the problem in the right way, the coding is indeed not that laborious of an endeavor and it almost 'writes itself'. But it took me many hours of picking the problem apart to arrive at that point.
Also, if any expert looks down at my stringly typed programming (see comment), they can put this on any footing they want, provided the requested functionality is provided.
I'm fairly confident that there is some open source code available (github?) that has the algorithm 'buried inside' a system that tackles much more. It would be acceptable for an answer to 'pull' from this resource, provided they give credit to the source.
My original question follows in the next section.
The 52 cards take the form
Deck = [2c,2d,2h,2s,3c,3d,3h,3s,...,Tc,Td,Th,Ts,Jc,Jd,Jh,Js,...,Ac,Ad,Ah,As]
There are several questions on this site that might appear to be duplicates, but here we want to ask a very specific and new question.
Let
HC = 0 # no pair
HC = 1 # one pair
HC = 2 # two pair
HC = 3 # trips
HC = 4 # straight
HC = 5 # flush
HC = 6 # full house
HC = 7 # four of a kind
HC = 8 # straight flush
Let C7Cards be a Python list of 7 cards taken from the Deck.
Explicitly define the coding/algorithm for a handClassifier(C7Cards) function with
return (best_5_cards, HC) # best_5_cards are (sorted) list of 5 cards from C7Cards
The sort on best_5_cards is the 'natural one' that allows one too easily (with HC) write the code to determine if one poker hand is better than another one.
Example:
C7Cards = ['2c','Ah','3d', '5s','4h','5d', '3h']
hand, hc = handClassifier(C7Cards)
#result: hand = ['Ah', '2c', '3d', '4h', '5d'] and hc = 4
We do not want to see answers that use external lookup tables - the answers will be self-contained solutions with all the coding tested and ready to run.
I am working on my own solution and plan to post it as an answer by March 1, 2019. But if somebody preempts this with an answer that makes my work look 'silly', I won't bother with the self-answer post, and will upvote /accept that answer.
from Python Code/Algorithms Ordering "5 Out of 7" Card Poker Hands
No comments:
Post a Comment