126 Chapter 6
Roll the Dice: Creating a Yahtzee-Style Game
We used arrays in our card game to help simplify the code needed
to deal a card, and to test which card was higher in value based on
its position in the list of cards. In this section, we’ll use the array
concept to generate five random dice and check to see if we roll
three of a kind, four of a kind, or five of a kind, like a simplified
version of the dice game Yahtzee.
In Yahtzee, you have five dice. Each die has six sides, with
each side showing a number of dots from one to six. In the full
game, the user rolls all five dice, trying to get points by rolling
three dice of the same value (which we call three of a kind) and
other various “hands,” similar to the card game poker. Rolling
five of the same value (say, all five dice land with the six-dot side
facing up) is called a Yahtzee and scores the highest points pos-
sible. In our simplified version of the game, we’re just going to
simulate the roll of five dice and check whether the user rolled
three of a kind, four of a kind, or Yahtzee and let them know the
outcome.
s tting e up the Game
Now that we understand the game’s objective, let’s talk about
how we’ll code the game. First, we’ll need to set up a game loop
so that the user can keep rolling until they want to quit. Second,
we’ll need to set up a hand of five simulated dice as an array that
can hold five random values, from 1 to 6, representing the value