Poker is a family of card games, where effective betting strategies require knowledge of the probabilities of being dealt various hands.
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:
N
,
of random hands (draws of five cards) from a card deck.Ct
, that have hand type t.Ct/N
to estimate the probability of being dealt hand type t.
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);
struct Card(face, suit);
random
, this step is easy:
function randomCard = Card(random(FACES), random(SUITS));
function randomHand = collect(i=1 to handSize) {randomCard};
handSize = 5;
first(i=2 to handSize, j=1 to i-1 | hand[i]==hand[j]) {(i,j)}
first(i=2 to handSize, j=1 to i-1 | hand[i]==hand[j]) {ERROR}
first
returns IGNORE
).
Click here to see the program created so far.
A useful function counts the number of times a face appears in a hand.
faceCountInHand(face, hand) =
sum(card in hand) {card.face==face};
key(hand) = sum(card in hand) {faceCountInHand(card.face, hand)};
twoPair = collect(Card(6, SPADE), Card(2, SPADE), Card(6, CLUB),
Card(8, DIAMOND), Card(8, HEART));
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.
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.