Texas Hold ‘Em

March 23, 2010

Texas Hold ‘Em is a variant of poker. After the ante, two cards are dealt face down to each player and a round of betting ensues. Then three community cards (the flop), which may be used by any player, are dealt, followed by another round of betting. Then a single community card (the turn) is dealt, followed by a round of betting, followed by the final community card (the river) with its round of betting. In total, each player has two concealed cards plus five community cards, and the player with the best five-card hand of any of the seven cards available to him is the winner, claiming the pot. Poker hands are ranked, from highest to lowest, according to the following rules:

  1. Straight flush: Five cards in sequence, all of the same suit. The ace can be played either high or low. Between two straight flushes, the highest-ranking card wins; two straight flushes with the same sequence tie.
  2. Four of a kind: Four cards of the same rank, plus one unmatched card. Highest-ranking quads beat lower-ranking quads; if both are the same, the highest-ranking kicker wins, otherwise there is a tie.
  3. Full house: Three matching cards of one rank, and two matching cards of another. The hand with the highest-ranking triplet wins; if both are the same, the highest-ranking pair wins, otherwise there is a tie.
  4. Flush: Five cards of the same suit. Two flushes are compared by the ranks of their cards; the highest-ranking card wins, in the event of a tie the second highest-ranking card wins, and so on until a difference is found. If both flushes have cards of the same rank, they tie.
  5. Straight: Five cards in sequence. The ace may be used either high or low. Two straights are ranked by their highest cards; if their highest cards are equal, they tie.
  6. Three of a kind: Three cards of the same rank, plus two unmatched cards. The highest-ranking triplet wins; if they are the same, the kickers are compared to break the tie.
  7. Two pair: Two cards with matched rank, plus two more cards of matched rank, plus one unmatched card. Two-pair hands are ranked on the highest pair, then the lowest pair, and finally the kicker.
  8. One pair: Two cards with matched rank, plus three more unmatched cards. The highest-ranking pair wins, with the three kickers used to break ties.
  9. High card: Five unmatched cards. Hands are ranked on their highest card, followed by their second-highest card, and so on.

Your task is to write a program that selects the best five-card hand from a list of seven cards. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2