



|
Play Yahtzee
Yahtzee Rules.
Players share a set of five dice. Each player has a score sheet. They take turns using the dice and acquiring points that are then written on the score sheet. Each turn, the player rolls the dice, decides which dice to change and rerolls them, and once again decides which to change and rerolls them. After three rolls, the player must decide which rule to use to score the dice.
There are thirteen ways to score the dice, and each player will take 13 turns and use each way once. The first six of these ways are "Aces", "Twos", "Threes", "Fours", Fives" and "Sixes", which are scored by adding up the face value of the dice with value 1, 2, 3, 4, 5, and 6, respectively. In contrast, the "Yahtzee" rule gives a score of 50 if the dice are five of a kind, and 0 otherwise. So, a set of 5 twos will give a score of 10 according to the "Twos" rule, a score of 0 according to the "Threes" rule, and a score of 50 according to the "Yahtzee" rule. The player gets to choose which rule will be used, but once a rule is used, it cannot be used again.
Implement each of these features in order. Note that the first ten require learning, but the last six should be easy.
- A single die will take on integer values 1 through 6. It changes its value when it is rolled. The "roll" command will cause it to take on a new value, with an equal probability of being any of the integers 1 through 6. Note that the class Random provides a stream of random numbers.
- Make a display for a die. Make a button which, when pressed, rolls the die.
- Make a display of 5 dice. Each has a button that can be on or off. There is another button which, when pressed, rolls all dice that are "on" . The idea is that people will turn off the buttons for the dice that they want to keep, and then roll the others. Note that you probably want all your dice to use the same random number generator, so your instance of Random should probably be stored in a class variable instead of keeping a separate value for each die.
- Make a display for the "Aces" rule. It will display the value of the dice according to that rule. In other words, it gives the total of all dice with value 1.
- Make a display for the "Twos" rule. It will display the value of the dice according to that rule. Note that there are now two rules; rolling the dice will change the displays of both the rules.
- Give each rule a button. If you press a rule's button, the rule's display will remember its current value and will no longer change when the dice are rolled.
- Make a display for the "Threes" rule, the "Fours" rule, the "Fives" rule, and the "Sixes" rule. They should behave like the first two.
- Make a "Total" display, which displays the total score. When you choose a rule and make its value permanent, add its value to the total.
- A player can only roll three times, then they must choose a rule. Each rule can only be chosen once per game.
- There can be several players. Make a separate window for each player. The game will let one player move, then will let the next player move. Only one window will be active at a time, and pressing a rule's button (which chooses that rule to be the score for the turn) will make the next player's window active.
- The rule "3 of a kind" gives the total of all dice if three are equal, or 0 if there are not three equal.
- The rule "4 of a kind" gives the total of all dice if four are equal, or 0 if there are not four equal.
- The rule "full house" gives 25 points if there are three of one value and two of another, or 0 otherwise.
- The rule "small straight" gives 30 points if there is a sequence of four, or 0 otherwise.
- The rule "large straight" gives 40 points if there is a sequence of five, or 0 otherwise.
- The rule "Chance" gives the total of all dice.
- Five of a kind is a "Yahtzee", and is worth 50 points.
Play Yahtzee
|