Unformatted text preview:

Math 121: Introduction to Computing Handout #21Assignment #5—Yahtzee! Yahtzee in this course was introduced by Todd Feldman; much of this handout was written by Julie Zelenski.Due: Monday, November 20With mastery of arrays, your ability to write interesting programs will take a dramaticleap forward. To solidify your understanding, Assignment #5 uses arrays in a variety ofcontexts to implement a popular multiplayer dice game. There are arrays for the dice,arrays for the dice to reroll, arrays for the player names, arrays for a player’s score, andeven an array of arrays to handle the entire scorecardThe goalYour task is to create a computer version of the game Yahtzee™. The game is simple tolearn. There are five dice and one to four players. A round of the game consists of eachplayer taking a turn. On each turn, a player rolls the five dice with the hope of gettingthem into a configuration that corresponds to one of 13 categories (see the followingsection on “Dice Categories”). If the first roll doesn’t get there, the player may choose toroll any or all of the dice again. If the second roll is still unsuccessful, the player may rollany or all of the dice once more. By the end of the third roll, however, the player mustassign the final dice configuration to one of the thirteen categories on the scorecard. Ifthe dice configuration meets the criteria for that category, the player receives theappropriate score for that category; otherwise the score for that category is 0. Since thereare thirteen categories and each category is used exactly once, a game consists of thirteenrounds. After the thirteenth round, all players will have received scores for all categories.The player with the total highest score is declared the winner.Dice categoriesThe thirteen categories of dice configurations and their scores are:1. Ones. Any dice configuration is valid for this category. The score is equal to thesum of all of the 1’s showing on the dice, which is 0 if there are no 1’s showing.2–6. Twos, Threes, Fours, Fives, and Sixes. (same as above but for different values).Any dice configuration is valid for these categories. The score is equal to the sumof the 2’s, 3’s, 4’s, and so on, showing on the dice.7. Three of a Kind. At least three of the dice must show the same value. The score isequal to the sum of all of the values showing on the dice.8. Four of a Kind. At least four of the dice must show the same value. The score isequal to the sum of all of the values showing on the dice.– 2 –9. Full House. The dice must show three of one value and two of another value. Thescore is 25 points.10. Small Straight. The dice must contain at least four consecutive values, such as thesequence 2-3-4-5. The score is 30 points. 11. Large Straight. The dice must contain five consecutive values, such as thesequence 1-2-3-4-5. The score is 40 points.12. Yahtzee! All of the dice must show the same value. The score is 50 points.13. Chance. Any dice configuration is valid for this category. The score is equal tothe sum of all of the values showing on the dice.Running the programAs outlined in the section to follow entitled “What is provided,” all the methods toimplement the graphics and mouse interaction have been written for you. This sectiondescribes the way the program works as a whole.When the program begins, it displays a welcome message and asks the user to enter thenumber of players. It then asks the user to enter the names of the players, one at a time.Suppose that there are two players—Eric and Julie—locked in a cutthroat, head-to-head,winner-take-all showdown. After you use the pop-up dialog boxes to enter the namesEric and Julie, the applet displays the starting Yahtzee scorecard and dice in thegraphics window, as shown in Figure 1.Figure 1 (After configuring a new two-player game with Eric and Julie)– 3 –The Yahtzee scoreboardIt’s worth taking a minute to two to look at the Yahtzee scoreboard. The 13 categoriesthat make up the game are divided into two sections. The upper section contains thecategories Ones, Twos, Threes, and so forth. At the end of the game, the values in thesecategories are added to generate the value in the entry labeled Upper Score. Moreover, ifa player’s score for the upper section ends up totaling 63 or more, that player is awarded a35-point bonus on the next line. The scores in the lower section of the scorecard are alsoadded together to generate the entry labeled Lower Score. The total score for each playeris then computed by adding together the upper score, the bonus (if any), and the lowerscore.Playing a sample gameThe game shown in Figure 1 is now ready to begin. Eric is first, so his name ishighlighted in the scorecard, which also displays the following message:Eric's turn. Click "Roll Dice" button to roll the dice.When Eric clicks the Roll Dice button, the dice are randomly rolled, resulting in a displaythat looks like the diagram shown in Figure 2.Figure 2 (After Eric’s first roll)At first glance, these numbers look wonderful, with three 5’s already! Thinking that hehas a chance for the Yahtzee category, Eric wants to reroll the 3 and the 4. To indicatethis choice, all Eric has to do is click on these two dice. Doing so highlights these dice asfollows:Figure 3 (Eric has selected the 3 and 4 and is ready to reroll)– 4 –To reroll the selected dice, all the player has to do is click on the Roll again button. Untilthis button is clicked, the player can select or deselect any particular die by clicking on it.For example, if Eric decided instead to try for some kind of straight, he could deselect the3 and the 4 by clicking on them and then selecting new dice—presumably two of the 5’s—by clicking on these.Always overconfident, Eric decides to go for the Yahtzee and rerolls just the 3 and 4.Unfortunately, Eric doesn’t get any more 5’s on his second roll, so he selects those sametwo dice for his final roll, but is again unsuccessful in his quest for more 5’s. Eric endsup with the three 5’s, a 6, and a 3, as shown in Figure 4.Figure 4 (After Eric’s final roll of his first turn, choosing category Three of a Kind )– 5 –While Eric didn’t manage to secure a Yahtzee, he did come up with a reasonably decentThree of a Kind. When asked to


View Full Document

REED MATH 121 - Study Guide

Download Study Guide
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Guide and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Guide 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?