A Simple Yahtzee Simulation in Python

This is the beginning of what will be a multi-part series on simulating the game Yahtzee in Python. It builds upon some of the previous work I’ve done with the simple dice hand simulator.

Using this simulator, let’s see how we can extend it to model some common dice games. An important thing to realize is that there are two basic ways to understand events that involve chance: theory or simulation. This is very similar to the split between theoretical and experimental physics. Each approach has advantages and disadvantages, however my goal is to practice my Python programming, so using simulation to understand complex problems seems much more appropriate than trying to mathematically model dice games using probability theory.

Yahtzee is a game that I’ve been playing a bit lately and was wondering about some of the probabilities / strategies, so I’ve decided to explore that first.

Using SimpleHand as a base class, we can extend it to build the concept of a YahtzeeHand that is able to count the differing types of hands that appear in the game Yahtzee. We will start with a few of the most basic Yahtzee hands — Small Straight (4 sequential number), Large Straight (5 sequential numbers), and a Full House (a triple and a double in the same hand). Notice how that we can use the foundation we’ve built to make the implementation of this quite easy:

class YahtzeeHand(SimpleHand):
    def __init__(self, seed: int = None):
        super(YahtzeeHand, self).__init__(num_sides=6, num_dice=5, seed=seed)

    def is_large_straight(self) -> bool:
        return self.num_sequential() == 5

    def is_small_straight(self) -> bool:
        return self.num_sequential() >= 4

    def is_full_house(self) -> bool:
        counts = self.get_counts()
        return 3 in counts and 2 in counts

That’s it! Now we’ve got the basics of a Yahtzee hand and can use it to calculate some probabilities. Next time, I think I’ll extend this to include the other types of hands like three of a kind, four of a kind, Yahtzee and more.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top