News & Articles:

Kata: Popular Dice Game

A popular dice game (some call it Yahtzee) is played with dice. The player rolls five dice and scores depending on what the dice show; there are various categories, and the player must decide which too use for their throw. This Code Kata involves developing a program that displays the score for a roll under the various categories of the game.


Yahtzee is a game played with dice. It works like this.

The player rolls five dice and examines the results. If desired, some or all of the dice may be rolled again. The player gets three chances to roll in total.

The score the player gets for the roll depends on which category the player chooses to allocate to the roll. Examples make this clear. The categories in the traditional Yahtzee game are:

  • Ones: The sum of all the dice showing 1.
  • Twos: As above, but the dice showing 2.
  • Threes, Fours, Fives, Sixes: As above. (Collectively these first six categories are known as the Upper Section).
  • Chance: The total of the five dice. This (and those below) are part of the Lower Section of scores.
  • Three of a Kind: The player gets the total of the three highest matching dice. Note that if four match, the player can still get this score, but scores only three times the die.
  • Four of a Kind: As above, but four must match (and five matching will also be fine, scoring four times the die).
  • Small Straight: 30 points if the dice contain any four numbers in sequence. Zero otherwise.
  • Large Straight: 40 points if the dice are in sequence (1 to 5 or 2 to 6). Zero otherwise.
  • Full House: 25 points for three-of-a-kind with a pair. Zero otherwise.
  • Yahtzee: 50 points if all dice show the same. Zero otherwise.

For fun, we add in the following categories that do not appear in the traditional game (you may want to add more):

  • Pair: The sum of the best pair (more may match, but only double the die is scored). Consider “1, 1, 1, 6, 6″ here, which scores 12, not 2 (or even 3).
  • Pauper: The sum of the dice, but only if that is less than 12 (zero otherwise).
  • Money Bags: The sum of the dice, but only if it is more than 22 (zero otherwise).
  • Four Bagger: The sum of the four highest dice.
  • Three Bagger: As above, but the sum of the best three dice.


Your program should take the user through the process of rolling the dice, and should then display how their final roll would score in each category described above.


There is a lot going on here. The patterns found in the usual game present a good collection of issues; other patterns can be added to taste.

The difficulty of the Kata also depends on the language used. We coded our first solution in Python. The high-level nature of this, and all the built-in operations for handling lists, made many of the patterns trivial. Finding the best way is still a good exercise, though.

For a new programmer, the initial rolling of the dice alone probably presents a good exercise. The key to this is selection of a good data structure, and devising a way to mark which of the dice are to be rolled again.

Of the patterns themselves, “Small Straight” requires some effort to get an elegant solution. Most of the others are trivial once they have been thought through – which is the whole point!

Creative Commons Licence
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Posted in Technical

Tags: , ,