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

Write a program that simulates a game where a player throws 5 dice and can rerol

ID: 3558546 • Letter: W

Question

Write a program that simulates a game where a player throws 5 dice and can
reroll all of the dice (all or none) if the player wants. A player can only reroll all
the dice once but does not have to do the reroll. After the original roll and after
possibly rerolling all the dice the sum of all the dice is the players score. The dice
are a little unusual. The first one only has 4 sides, the second has 5 sides, the third
has 6 sides, the fourth has seven sides and the fifth has eight sides. The sides of the first die are
numbered from 1 to 4. The sides of the second die are numbered from 1 to 5, etc.
1) Your program will be submitted on time or you will not receive credit for this assignment.
2) Your program will be submitted using the directions for HW submission supplied on the class
web site. (Any errors in this will result in a lower grade and possibly no credit for this
assignment.)
3) Your program should be object oriented and have a .h file and a .cpp file for each object.
4) Your program will have a main.cpp file for the main function.
5) There should be a Dice class and a SetOfDice class.
6) Your program should run each simulation 20000 times. (There will be three separate simulation
runs.)
7) After the first simulation it will neatly display on the screen the estimate of the expected
average score for a player who plays an optimal strategy. (Can you figure out the optimal
strategy? We will discuss it briefly in class.)
8) After the second simulation it will neatly display on the screen the estimate of the expected
average score for a player who never chooses to reroll all the dice.
9) After the third simulation it neatly display on the screen the estimate of the percentage of time
that the player from item 7 is expected to beat the player from item 8.
10) The program will then ask you if you want to rerun the simulation if you answer with

Explanation / Answer

This is my code:

---Die.h------
class Die
{
public:
Die(); //Default constructor
Die(int sides); //Explicit value constructor
void Print(); //Prints a test
~Die(); //Destructor
void roll();
void conditionalReRoll();
int getValue();
private:
int value;
int numberOfSides;
};


---Die.cpp------
Die::Die()
{
//cout<<"Hello, It's good to be alive."<<endl;
value=0;
numberOfSides=6;
}

Die::Die(int sides)
{
//cout<<"Hello, It's good to be alive."<<endl;
value=0;
numberOfSides=sides;
}

Die::~Die()
{
cout<< "Good bye, cruel world" <<endl;
}

void Die::roll()
{
value=1+(rand()%numberOfSides);
}

void Die::conditionalReRoll()
{
double expectedValue = (numberOfSides+1)/2.0;
if(value<expectedValue)
{
roll();
}
}

int Die::getValue()
{
return value;
}


---SetOfDice.h-------
class SetOfDice
{
public:
//void addDiceToSet(Die die);
void operator += (Die die);
void roll();
void conditionalReRoll();
int getValue();
private:
vector<Die> vectorOfDice;
};


---SetOfDice.cpp----
void SetOfDice::addDiceToSet(Die die)
{
vectorOfDice.push_back(die);
}

void SetOfDice::operator += (Die die)
{
addDiceToSet(die);
}


void SetOfDice::roll()
{
for(unsigned int i=0;i<vectorOfDice.size();i++)
{
vectorOfDice[i].roll();
}
}


void SetOfDice::conditionalReRoll()
{
for(unsigned int i=0;i<vectorOfDice.size();i++)
{
vectorOfDice[i].conditionalReRoll();
}
}


int SetOfDice::getValue()
{
int result=0;
for(unsigned int i=0;i<vectorOfDice.size();i++)
{
result+=vectorOfDice[i].getValue();
}
return result;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote