Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

This assignment will make use of arrays and functions to simulate a game.

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.
    There is a bonus scoring box worth 35 points if the other six add up to 63 or more (not relevant to the basic assignment).
  • 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 suf 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. So, instead of storing an array of five values for the five dice, the program would gain more by having a six-element array, that counts the dice of each number.

With such a storage mechanism, it would be very simple to compute the scores for the upper half of the Yahtzee score pad. Also, it would be very easy to share code to evaluate the lower portion of the score card.

Also, such a storage mechanism would also make it easy to reroll dice. For example, in the first reroll above, it was easy to identify that there actually as a 2 to reroll, and to count one less 2 and one more 6. There was no need to go searching an array of values for a 2.

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 after Thanksgiving) 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.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M9527070

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Question 1 what is hadoop explaining hadoop 2 what is

Question: 1. What is Hadoop (Explaining Hadoop) ? 2. What is HDFS? 3. What is YARN (Yet Another Resource Negotiator)? The response must be typed, single spaced, must be in times new roman font (size 12) and must follow t ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Background informationthis assignment tests your

Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in the second half of the unit build upon the fun ...

Php amp session managment assignment -this assignment looks

PHP & SESSION MANAGMENT ASSIGNMENT - This assignment looks at using PHP for creating cookies and session management. Class Exercise - Web Project: Member Registration/Login This exercise will cover adding data connectivi ...

Question 1 what is a computer program what is structured

Question: 1. What is a Computer program? What is structured programming? 2. What is modular programming? Why we use it? 3. Please evaluate Sin (x) by infinite series. Then write an algorithm to implement it with up to th ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As