I have been making a Texas Hold'Em game as a part of an assessment, and I have been mulling over how to examine the 7 available cards and determine if hands exist.
The only possible method I can think of is to sort the cards numerically, then examine each possible group of 5 cards and check if they match a list of every single possible hand. That would take a long time snd would only be feasible for determining pairs, as the suit is irrelevant.
The cards are each strings, made up of a number/a/j/q/k, and a suit (char)3
(that makes a little spades symbol).
Does anyone have any suggestions, formulas, or links I could use to help create a hand-analysing system?
Don't worry about ranking hands against each other yet, that's a different kettle of fish.
Answer
I think you can find the majority of poker hands by simply making a couple of tables of how many cards in the hand there are of each rank and suit.
In other words, create an array mapping card ranks (numbers and A/J/Q/K) to the count of cards of that rank in your hand. If the player has a pair or three-of-a-kind, there will be an element in this array equal to 2 or 3, etc. They have a full house if there's one element that's 2 and another that's 3, and a straight if there are five consecutive elements equal to 1 in this array.
Likewise you can make a similar array of the count of cards of each suit, and use it to detect flushes.
Once you've detected the presence of a specific hand it's pretty easy to then go back and find the specific cards in the hand, for highlighting them in the UI or whatever you need to do.
In pseudocode:
int countByRank[13] = { 0 }; // Initialize counter to zero for each rank
for (cards in hand)
countByRank[card.rank] += 1; // Increment counter for this card's rank
if (countByRank.find(2))
// There's a pair
else if (countByRank.find(3))
// There's a three-of-a-kind
// etc...
No comments:
Post a Comment