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:
For fun, we add in the following categories that do not appear in the traditional game (you may want to add more):
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!
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.