I think I’m making good progress on my implementation of a YahtzeeHand
to analyze the game. However, I don’t have any logic right now to detect a Full House — three of one number combined with two of another number. This is a bit difficult to implement with the current logic, so I’m going to expand the SimpleHand
class to include a new method: get_counts()
. This method will return a tuple of size num_sides
, where each item will indicate the number of dice in the hand bearing the same number as the index. For example, with the following hand [1, 4, 1, 4, 2], get_counts()
would return (2, 1, 0, 2, 0, 0) — which would indicate 2 x ones, 1 x twos, 0 x threes, 2 x fours, and 0 fives and sixes. Having this functionality will really help discover full houses, and will also help a lot down the road when I have to figure out the “upper” part of the Yahtzee scorecard (ones, twos, threes, etc).
It turns out that get_counts()
is pretty easy to implement with a comprehension:
def get_counts(self) -> tuple: """ Get the count of each number appearing in the hand. e.g. - a hand of [3, 3, 4, 4, 1] will return (1, 0, 2, 2, 0, 0) :return: A tuple of integers with the value at each index indicating the number of occurrences of number n-1 in the hand. """ retval = tuple(self.hand.count(i) for i in range(1, self.num_sides+1)) return retval
I also added a docstring because frankly it’s not quite clear what the return value from this method is and someone using it might be confused.
Having this simple get_counts()
method allows me to pretty easily add a method to detect full houses:
class YahtzeeHand(SimpleHand): ... def is_full_house(self) -> bool: counts = self.get_counts() return 3 in counts and 2 in counts
Adding that to our overall test for “first time” rolls, let’s us see that full houses are more common than I thought!
100000 rolls: LargeStraight : 3191 / 100000 -- 3.19% SmallStraight : 15446 / 100000 -- 15.45% FullHouse : 3867 / 100000 -- 3.87% 3ofaKind : 21334 / 100000 -- 21.33% 4ofaKind : 2020 / 100000 -- 2.02% Yahtzee! : 76 / 100000 -- 0.08%
Now that I’ve got this working, I’m going to move on to do the “top” part of the score card and also construct the overall game logic that will help us use our new class to play some Yahtzee!