Simulate the game of yahtzee

Assignment Help Basic Computer Science
Reference no: EM13322347

ASSIGNMENT DESCRIPTION

This program will simulate part of the game of Yahtzee!

The player will roll five dice, and then have two chances to reroll any dice that are desired, and then the outcomes are evaluated.

The program must allow for further turns after each scoring opportunity.

The basic assignment does not need to keep score for further rounds -- that would be left as an Extra Credit feature.

A Yahtzee score card has two portions:

  • The upper portion has spaces for six scores, obtained by adding up all of the 1's, 2's, 3's, etc.
  • The lower portion has special scores for various combinations:
    • Three of a kind -- at least 3 dice are the same number;
      the score is the sum of all five dice
    • Four of a kind -- at least 4 dice are the same number;
      the score is the sum of all five dice
    • Small straight -- four consecutive numbers are represented, e.g. 2345;
      the score is 25 points
    • Large straight -- five consecutive numbers are represented, e.g. 23456;
      the score is 30 points
    • Full House -- three of one kind, two of another; the score is 30 points
    • Yahtzee! -- five of a kind; the score is 50 points
    • Chance -- nothing special; the score is the sum of all five dice

Here are some sample results from the instructor's solution.
For readability, I put the upper and lower portions side by side.
The random number generator was being especially friendly today.

Rolling 5 dice: 3 5 4 2 2
Enter dice to reroll (e.g. 15 rerolls a 1 and a 5); 0 to quit: 2
Rerolling 1 dice: 6
Enter dice to reroll (e.g. 15 rerolls a 1 and a 5); 0 to quit: 0

                 Three of a Kind   0
Sets of 1's: 0   Four of a Kind    0
Sets of 2's: 2   Full House        0
Sets of 3's: 3   Small Straight    25
Sets of 4's: 4   Large Straight    30
Sets of 5's: 5   Yahtzee           0
Sets of 6's: 6   Chance            20

Another (y/n)? y
Rolling 5 dice: 1 4 1 1 2
Enter dice to reroll (e.g. 15 rerolls a 1 and a 5); 0 to quit: 24
Rerolling 2 dice: 6 1
Enter dice to reroll (e.g. 15 rerolls a 1 and a 5); 0 to quit: 6
Rerolling 1 dice: 1

                 Three of a Kind   5
Sets of 1's: 5   Four of a Kind    5
Sets of 2's: 0   Full House        0
Sets of 3's: 0   Small Straight    0
Sets of 4's: 0   Large Straight    0
Sets of 5's: 0   Yahtzee           50
Sets of 6's: 0   Chance            5

Another (y/n)? y
Rolling 5 dice: 6 6 4 4 4
Enter dice to reroll (e.g. 15 rerolls a 1 and a 5); 0 to quit: 0

                 Three of a Kind   24
Sets of 1's: 0   Four of a Kind    0
Sets of 2's: 0   Full House        30
Sets of 3's: 0   Small Straight    0
Sets of 4's: 12  Large Straight    0
Sets of 5's: 0   Yahtzee           0
Sets of 6's: 12  Chance            24

Another (y/n)? n

PROGRAM DESIGN HINT

The order in which the dice were rolled is never relevant in this game. What does matter is how many 1's, 2's, 3's, were rolled. Array usage should be designed with this fact in mind.

A SPECIAL PROGRAMMING TIP

This little tip is not at all required for the assignment, but ends up being helpful, especially for those pursuing the extra credit. There are several different ways of evaluating the dice rolls above. It does help to have functions for those evaluation methods -- but it may seem a little awkward calling them for the output display.

Certainly you could do an if statement or a switch statement to say something like "on row 1, evaluate for 3 of a kind; on row 2, evaluate for 4 of a kind;", etc. but it turns out an array can even be used here!

Unfortunately an array of functions is not quite correct -- different functions might have different amounts of code, so they would not all be the same size. But pointers (which we will see later) are all the same size, and may make suitable array elements.

Here are a few lines of code from the instructor's solution:

const char nameLower[7][16] = { "Three of a Kind", "Four of a Kind", "Full House",
        "Small Straight", "Large Straight", "Yahtzee", "Chance" };

int (*scoreLower[7])( int[] ) = { threeOfaKind, fourOfaKind, fullHouse,
        smallStraight, largeStraight, yahtzee, chance };

      cout << setw(18) << nameLower[i] << scoreLower[i](counts) << endl;

The first array declaration is much like one seen before.

The second defines an array of 7 elements, each of which is a pointer to a function that accepts an integer array as a parameter. (Function prototypes for all of them would appear before this statement).

The last line above shows both arrays being subscripted, and that the function is being passed an array parameter (here named counts).

Again: This is not required for the assignment; feel free not to do it!

EXTRA CREDIT OPTION


Actually keep score for an entire solitaire came of Yahtzee!

This game would consist of 13 turns (there are 13 categories to score).

After displaying the possible scores for each category, ask the user which score to keep (it might not be the largest). Once the box is filled, it cannot be scored again.

It would be very helpful to have the display somehow indicate which boxes have already been filled, and which scores are available.

In addition, there are these things to note about the score:

  • There is a bonus score of 35 points if the upper half scores add up to more than 63. You can display this below those upper scores; it would also be nice to have a total score for everything (which can be placed under the lower scores).
  • If a player rolls a Yahtzee! and has already scored 50 points for Yahtzee, they may instead score 50 points in any of the other lower-score boxes, in place of the regular scoring mechanism. However, this cannot be done if a 0 has already been placed in the Yahtzee box (or if that box is empty).

Of course, on the 13th round, when there is only one unscored box, there should be no need to ask the user where to place that last score.

With this Extra Credit option, a player should be able to play a complete game solitaire.

Reference no: EM13322347

Questions Cloud

The quantity of money in the hands of the public to increase : Why might the Fed find it significantly easier to expand the money stock in a period of prosperity than in a period of recession? What must the Fed be able to do if it wants the quantity of money in the hands of the public to increase?
Find the rms current in the circuit : In a purely inductive circuit of 36 mH, the freq is 71 Hz and the maxium voltage is 317 V. Find the rms current in the circuit
Writing a new class that is derived from another class : Writing a new class that is derived from another class.You will be given the code for a Car class. Your job is to write a CustomCar class that is derived from Car and adds new features to it.
How much oil spills : A copper flask with a volume of 80 cm^3 is filled to the brim with olive oil. If the temp of the system is increased from 8C to 52C, how much oil spills
Simulate the game of yahtzee : After displaying the possible scores for each category, ask the user which score to keep (it might not be the largest). Once the box is filled, it cannot be scored again.
Find how much work is done on the system : A cylnder holds .5 moles of an ideal gas at temp 334K. The gas expands isothermally from an initail volume of .03 m^3 to a final volume of .4m^3
Compute the after-tax cost : Assuming a 25 percent tax rate, compute the after-tax cost of the following business expenditures.
Explain the experimental value of the solubility : This half cell is connected to a SHE half-cell. The measured potential for this cell is 0.476 V at 25 degree C. What is the experimental value of the solubility constant for silver oxalate
How much heat flows through the two rods : Two metal rods, silver and copper, are attached to a steam chamber with a temp 100 C, at one end and an ice bath ( 0 C ) at the other end. How much heat flows through the two rods in 60s

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Write a c++ program to calculate closet points from a list

Write a C++ program to calculate the closet points from a list. Output will be the name of 2 points Your input format will be : name x-value y-value The following is the input to test your program. A 10 50 B 30 70 C 20 40 D 40 10 E 30 20 F 20 50..

  Describe constraints which encode that cell is observed

Describe constraints which encode that cell (1, 2) is observed and indicates mine is not present, that cell (2, 1) is observed and indicates a mine is present, and that remaining cells are unobserved.

  The village of marengo conducted a census

But instead of writing your pseudocode/program for "fewer than 300" households, write your pseudocode/program for EXACTLY 7 households.

  Write a sequence of assembler directives to store

Write a sequence of assembler directives to store the message "Welcome to the robot demonstration!" starting from the memory location $2000.

  Write two distinct fuction prototypes for a function

Write two DISTINCT fuction prototypes for a function that accepts three values and return three values calculated within a function to the calling function.

  Prepare disaster recovery plan for organization

Prepare a Disaster Recovery (DR) Plan for following organization; Company; Widget Works Electronics, Employees; 250, Infrastructure; 1 Central Office building housing primary datacenter. 10 satellite sales branches.

  Create an html5document

Create an HTML5document that contains links to 5 daily deal sites (like Groupon or Living Social) The page should contain the heading "My Favorite Daily Deals Web Sites." Should be able to click on the links to test the page.

  Generate dropdown to create example c++ code

Submit your C++ source code that you generated from RAPTOR with comments added to each line or where necessary to explain program flow. Also submit the RAPTOR file (flowchart) of your working program.

  What is your opinion of the criteria used

Based on your understanding of how AES was chosen, what is your opinion of the criteria used?

  Determine minimum number of pieces for location of centroid

Determine the minimum number of pieces which can be used in determining location of the centroid? For finiding the centroid of area, two square segments are considered; square ABCD and square DEFG.

  Compute the number of different possible ways

Compute the number of different possible ways in which the instructions of the two processes can be interleaved when the two processes are executed concurrently.

  Change arp entry in computer for mac address

Print your ARP cache table. Find a server on your local network. Change its ARP entry in your computer to point to your computer's MAC address. Print new ARP cache table. Now use the service and see what happens.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd