Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3534079 • Letter: T

Question

This assignment will also make use of arrays and functions to simulate a game. There is no need for any tools introduced since the previous assignment.

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:

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.

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:

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!

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:

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



SAMPLE DESIGN -


Explanation / Answer

#include <iomanip>
#include <cstdlib>
#include <iostream>
using namespace std;

int main()

{

int sum = 0; //sum of score
int num[5]; //number of rolls
int count[6]; //the possible cases from the die
int score[13]; //marks the 13 possible scores

cout <<"Enter your 5 numbers"<<endl; //request for 5 numbers


for (int i = (rand()% 6) +1; i < 5; i++) //Simulates rolling 5 die using the rand function, stores the results in the array num.
cin >> num[i];

for (int i = 0; i < 5; i++)
num[i] = rand() % 6 + 1;


if (num[5] > 0 && num[5] < 7) //represents the 6 cases for the count of each die rolled
{
++count[num[5]-1];
}



//rules of scores from 1 to 13

for (int i = 0; i <6; i++)

if
{
score[i] = count[i]*(i + 1); //number total sum of the number of 1's to the number of 6's
}

else if


(count[i]/3 >=1) //rules for score 6 to score 13
{
score[6] = sum;
}

else if
(count[i]/4>=1)
{
score[7] = sum;
}

else if
(count[i]/3 >=1 && count[i]/2 >=1)
{
score[8] = 25;
}


((count[0]*count[1]*count[2]*count[3]) ==1 ||
(count[1]*count[2]*count[3]*count[4]) == 1 ||
(count[2]*count[3]*count[5]) ==1)
{
score[9] = 30;
}


((count[0]*count[1]*count[2]*count[3]*count[4]) == 1 ||
(count[1]*count[2]*count[3]*count[4]*count[5]) ==1)

{
score[10] = 40;
}

(count[i]/5 >= 1)
{
score[11] = 50;
}

score[12] = sum; //end of 13 score

{
cout<<setw(30)<<"******* YAHTZEE RESULTS *****"<<endl; //output of scores
cout<<"Ace's"<<setw(14)<<score[0]<<endl;
cout<<"Two's"<<setw(14)<<score[1]<<endl;
cout<<"Three's"<<setw(12)<<score[2]<<endl;
cout<<"Four's"<<setw(13)<<score[3]<<endl;
cout<<"Five's"<<setw(13)<<score[4]<<endl;
cout<<"Six's"<<setw(14)<<score[5]<<endl;
cout<<"Three of a Kind"<<setw(4)<<score[6]<<endl;
cout<<"Four of a Kind"<<setw(5)<<score[7]<<endl;
cout<<"Full House"<<setw(9)<<score[8]<<endl;
cout<<"Small Straight"<<setw(5)<<score[9]<<endl;
cout<<"Large Straight"<<setw(5)<<score[10]<<endl;
cout<<"YAHTZEE"<<setw(12)<<score[11]<<endl;
cout<<"Chance"<<setw(13)<<score[12]<<endl;
cout<<setw(30)<<"******************************"<<endl;
}



return 0;



}