1

I've tried to look through several sources online and they all deal with 5 card and 7 card hands. Also, I'm not really looking for the code, I'll try to do that on my own (although perhaps if you're willing, I'm looking to implement this in either Python or JavaScript). I just want someone to explain to me the steps involved in finding such a hand (using pseudocode).

Basically, what I'm asking is: How can I find the highest poker hand from a 9 card hand between 4 players?

Should I just assign all the highest poker hand numbers in ranking and then parsing through each player's hand and see if their hand contains that number? That seems a little tedious and I'm not sure that's the right way to do it.

Also, I noticed other hand evaluators are set for optimization and some count the number of bits per card played which confused me.

EDIT: Here's the game I'm working on:

It's a game with a 6x6 grid in which four players pick up cards where wherever their player piece lands on, they pick up that card. Cards are from a standard 52 card deck but the cards are face up and only 36 randomly selected cards from the deck are used.

Eventually, toward the end of the game, the player could at most contain 9 cards in their hands.

The way to win the game is to contain the highest poker hand amongst the four players around you.

So, essentially, if you have a royal flush and everyone else has a pair or a straight, they lose.

In another game where the highest hand is a straight and the rest have a three of a kind or a two pair, then the person with the straight wins.

So, highest poker hand is the hand highest relative to other player's hand. It is the hand that has the highest ranking cards among 4 players.

poker hand is just regular hand that may or may not be the highest in rank.

Kaylie
  • 13
  • 5
  • 1
    Define `poker hand` and `highest poker hand`. – Kabie May 15 '13 at 09:34
  • @Kabie, I noticed that I might have been unclear. See edit above. Thank you! – Kaylie May 15 '13 at 09:42
  • So one hand only use 5 cards most, right? – Kabie May 15 '13 at 09:48
  • @Kabie, it depends on what you mean by uses. At the end of the game, only 5 cards (which form a possible poker hand, high or not) are evaluated in your hand out of at most 9 cards. – Kaylie May 15 '13 at 09:50
  • Essentially, I have to parse through a 9 card hand to see if it contains a 5 card poker hand. If the hand contains a 5 card poker hand, then isolate it for evaluation and comparison among the other 4 players. – Kaylie May 15 '13 at 09:51

1 Answers1

1

There are only 126 possible 5-cards combinations for a 9-cards hand. So you can just iterate over them to find the highest. itertools.combinations(9_cards, 5) can generate all of them.

To find the highest, a trivial implement could be define a function which gives a hand a score. Then use this function as key: max(all_5_cards_hands, key=hand_score)

You can use a tuple to represent the score. Leading by hand ranking and followed by card rankings.

An example:

STRAIGHT_FLUSH = 9
...
TWO_PAIR = 2
ONE_PAIR = 1
HIGH_CARD = 0

hand_score('A7532')  # I omit suits here
# => (HIGH_CARD, 14,7,5,3,2)
hand_score('KK333')
# => (FULL_HOUSE, 3, 13)
hand_score('33444')
# => (FULL_HOUSE, 4, 3)
hand_score('AKQJ0')
# => (STRAIGHT, 14)

# Tuples can be easily compared:
(HIGH_CARD, 14,7,5,3,2) < (STRAIGHT, 14)
# => True
Kabie
  • 10,489
  • 1
  • 38
  • 45
  • Hmm I see. I'll try it out and see if I get anywhere. Thank you so much @kabie. Btw, what if the total cards in the hand were less than 9 cards. For example, in the game, someone can move to a space that has no card and doesn't receive a card for that turn. So, that player might have 8 cards instead of 9 cards. And another player might have more than 10 cards at the end then. So, I just thought, let's make it general, what if a player can have x amount of cards and within that hand there is a possibility of 5 cards? – Kaylie May 15 '13 at 13:19
  • one more thing, how do I reference back to the player who has the highest poker hand? – Kaylie May 15 '13 at 17:28
  • @Kaylie: `9_cards` is the players hand here. `C(14,5)` is 2002. So it's still computable. So you can just replace `9_cards` with one's hand. To reference the player. The ordinary way could be just assign the `highest_hand` as a attribute of `Player` (I assume you use a class to represent the player?). Or make it a class method. – Kabie May 16 '13 at 02:22
  • Yeah I have a class to represent the player. Terribly stupid stupid question but how do I assign highest_hand as an attribute of a Player. Can you give me an example? – Kaylie May 17 '13 at 12:43
  • @Kaylie: Say you have `player1 = Player()`, then `player1.highest_hand=some_hand` will do. – Kabie May 18 '13 at 02:54