Cribbage: Optimal Hand (part 1)

Cribbage is one of my favourite card games and have been playing it ever since I could count. It is a unique card game, in fact it is often said there are 3 types of card games, trick style games, rummy style games and cribbage!

What I aim to do in a series of posts is take a data science approach to the game. I will simulate the game and eventually train an agent using a reinforcment learning model to play the game. Along the way I’ll simulate possible strategies such as keeping the highest scoring hand, keeping the best pegging hand, etc. This first post will simulate the deal for a 2 player game and choose the best 2 cards to throw into the crib to maximise your hand. Given how the game is played this may not actually be the best strategy, but before I get into that I’ll quickly explain the rules.

The rules

The objective of the game is to be the first player to reach 121 points. Once a player reaches 121 the game ends immediately. There are several ways to get points,

  • 2 points per unique combination of cards that sum to 15
  • 2 points per unique pairs e.g. 2 Jacks = 2 points, 3 Jacks = 6 points (2*{{3}\choose{2}} = 6)
  • 1 point per card for runs of 3 or more e.g. A, 2, 3
  • 1 point per card for a flush of 4 or more cards (at least 4 cards need to be in your hand)
  • 1 point for knobs i.e. a Jack in your hand the same suit as the card on the deck

Picture cards are all value of 10 and aces are low. There are more unique ways to pick up points which will be explained in one round.

  1. The dealer deals 6 cards to each player.
  2. Each player chooses 2 cards to throw into the crib. The crib is a second hand which is scored by the dealer at the end of the round.
  3. The non-dealer cuts the deck and the dealer turns the top most card face up and places it on the deck. This counts as a 5th card for scoring purposes.
  4. Starting with the non-dealer, each player plays a card keeping count of the cumulative score. If a player plays a card which sums to 15, creates a pair, or a run, they gain points accordingly. The cumulative score must not excede 31. The player to play the final card which ends on 31 gains 2 points. If a player cannot play a card they say “go”. Each player must play a card if they can. The last player to play a card gains 1 point. This continues until each player has played all their cards. Scoring points in this play is often called “pegging”.
  5. Each player reveals their hand. Starting with the non-dealer (their first take) scores their hand (including the face up card on the deck) and moves their peg accordingly on the game track. Finally the dealer scores their hand.
  6. The dealer reveals the crib and adds the score to their score.
  7. The dealer swaps and 1-6 repeats.

For more info check out the wiki page.

Other random rules:

  • You can only score a flush in the crib when there are 5 cards of the same suit.
  • If the dealer turns over a Jack during the cut, they get 2 points for doing it.

Strategy simulation

Here we will look at 5 possible strategies to begin with:

  1. Expected maximum score for the hand
  2. Highest potential score for the hand
  3. Maximising both the hand and crib (when it’s your crib)
  4. Maximising the hand and minimising the crib (when it’s your opponents crib)
  5. Random selection for control

It is expected that strategy 5 will be the worst by far but it’s good to have a baseline. In many cases strategy 1 and 2 would end up choosing the same hand however there will be cases where they will suggest different hands. Strategy 3 may show an edge over the first 2. It could pick up on certain rules of thumb such as not breaking a pair or fifteen. Strategy 1 will often have multiple choices for the the maximum possible score. In this case the algorithm will choose which of those has the highest expected score.

Another consideration is when discarding cards into the crib, if it is your crib you’ll also want to maximise the score but if it’s your opponents you’ll want to minimise the score. These will be slightly different objective functions. We should see the performance of these strategies between strategies 3 and 4.

The easiest way to select the optimal hand given the strategy is through brute force and calculate the score for every possible combination of 4 out of 6 cards to keep in your hand ({{6}\choose{4}}=15), and the cut card. Calculating the crib score is more challenging since there are more combinations given the other source of randomness of your opponent throwing cards into the crib.

Cribbage scoring functions

The below functions will score any given hand including the cut card.

A few hands will be passed to the total.score() function just to ensure it is calculating the score correctly.

All hands check out.

Expected score

For any given hand the expected score and maximum possible score will be calculated for each combination of 4 cards kept in the hand. With regards to the crib we only know the 2 cards we are putting into the crib and the remaining 2 cards plus the cut card are effectively random. In practice this isn’t entirely the case though because your opponent is likely to throw out cards which limit the score gaining potential in the crib, as you would do if it was your opponents crib. Even with this simulation if we had two identical agents playing off against each other and each agents strategy was to maximise points in their hand, this would alter probability distribution for the cards to be discarded to the crib, for example 5’s are very valuable because there are lots of cards with a value of 10 in the deck so they are less likely to be in the crib. These probabilities will be estimated through an iterative process.

Given we only have knowledge of 2 cards out of the 5, scores will need to be calculated for 44*{{46}\choose{2}} = 45540 potential crib hands. To speed up the processing a master look up table will be created for every possible hand rather than computing 45k scores every simulation.

The optimal hand function will select the best hand for each of the 5 strategies. It will output the key metrics for it’s decision such as the expected score, maximum score and expected crib score. This calculation will be relatively slow. There are many ways we could optimise the computation time, for example many hands will be the same since suit doesn’t factor into the score and therefore only need to compute the expected values for these hands once, although some times suit does play a roll. That becomes a more complex calculation so for now this will do. I’ll include the code for you to replicate/improve if you wish.

For this hand using strategy 3 the expected score is 8.7. This hand is also a good example of where strategy 1 and 2 suggest different cards to discard into the crib.

Simulation

We will now simulate 5000 hands and score each hand by the 5 strategies and output the results.

plot of chunk plot simulation results

The random strategy is as expected the worst strategy which is clear to see in the summary table and the histograms, The mean score is 3-4 points lower than the others. There is less of a difference between the other 4 srategies however the table shows that strategy 3 will on average give the best score when it is your crib, and strategy 4 will when it’s your opponents crib. While it’s not much of an edge it could mean domination over 25 years of playing cribbage! But it’s also good to see the maths and logic checks out.

The distribution of cards that were chosen to remain in the hand for strategies 3 and 4 shows when it’s your crib 7’s and 8’s are the cards more likely to be thrown into the crib. Otherwise it’s a relatively uniform distribution for the other cards. When it’s your opponents crib it’s quite clear it’s better to keep 5’s (which makes perfect sense) and throw out K’s.

plot of chunk hand card dist

This is strengthened by the distribution of cards thrown into the crib. When it’s your crib it’s best to throw out 7 and 8’s, and 2 and 3’s. When it’s your opponents crib it is best to throw out Kings and Queens. And there are very few cases where it is better for you to throw out a 5, which again makes sense.

plot of chunk crib hand distribution

The optimal hands where simulated by assuming that the cards thrown into the crib were random. It is clear that if you opponent was using a better strategy the cards expected in the crib are very different. We can now re-score the optimal hands by using the above estimated probability distribution. This will again generate a different probability distribution. This is repeated until it converges on the maximum likelihood estimates.

Final thoughts; Future posts

For this phase of the game it is clear strategy 3 and 4 will work out best but this is only part of the game. The “play” phase of the game is where a lot of points can be gained. There are cards which are better for scoring in the play phase or what is often called “pegging”. This is much harder to do via brute force since it depends on which cards your opponent was dealt, which ones they kept and in which order they played them. We’ll have to employ machine learing techniques to optimise which 4 cards to keep to maximise the score over the full play of the hand assuming the cards are played in the optimal order.

This is just the beggining of this analysis. In future posts I will

  1. Improve on expected crib score by using the probability distributions of an opponent using strategy 3 or 4
  2. Simulate the play phase of the game
  3. Simulate the entire game
  4. Fit a reinforcement learning model to train an agent to play crib. This will be the most intersting part of the project and given the game is played in two unique phases but are dependent on the initial deal and card selection, it will be a challenging exercise.
  5. And just for fun – build an image recognition algorithm so you can take a photo of your hand and it will output the best selection!

Stay tuned.

Follow me on social media: