Poker Example

Poker is a family of card games, where effective betting strategies require knowledge of the probabilities of being dealt various hands.

Problem Description

Here, we use Hava to estimate these probabilities via a simulation approach. In our example, we use a standard card deck of 52 cards, and each hand has five cards. We consider various hand types, for example, full house, straight, two of a kind.

A simulation approach follows these steps:

Hava Preliminaries

To represent this problem in Hava, we begin by defining tokens and lists to represent the card deck:

token JACK, QUEEN, KING, ACE;
FACES = (2, 3, 4, 5, 6, 7, 8, 9, 10, JACK, QUEEN, KING, ACE);
token CLUB, DIAMOND, HEART, SPADE; SUITS = (CLUB, DIAMOND, HEART, SPADE);
Next, we use a Hava structure to define a card in the deck:
struct Card(face, suit);

Dealing a Hand

A hand consists of five cards drawn sequentially from the deck. In lieu of simulating an actual deal of the cards, we adopt the following (easier to code) two-step approach.

Click here to see the program created so far.

Poker Hand Types

A useful function counts the number of times a face appears in a hand.

faceCountInHand(face, hand) = 
  sum(card in hand) {card.face==face};
The following function sums the face counts of each card in the hand:
key(hand) = sum(card in hand) {faceCountInHand(card.face, hand)};
For example, in the hand
twoPair = collect(Card(6, SPADE), Card(2, SPADE), Card(6, CLUB), 
                    Card(8, DIAMOND), Card(8, HEART));
the expression key(twoPair) evaluates to 9. Many poker hands can be uniquely identified from this key. For example, any hand whose key equals 9 must be a two-pair hand.

Click here to see a program that generates sample two-pair hands.

Statistics

Sample statistics are calculated using the iterator sum. For example:

N = 10000;

stats = 
  sum(i=1 to N, hand=randomHand | isValidHand(hand))
    {(isFullHouse(hand), isStraight(hand), isFlush(hand))};

spFullHouse = stats[1]/N;
spStraight = stats[2]/N; 
spFlush = stats[3]/N;

Click here to view the complete sampling solution.