Write a Dice game for C++ program??Help Write a program that simulates a game wh
ID: 3560171 • Letter: W
Question
Write a Dice game for C++ program??Help
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.
Criteria
1) Your program should be object oriented and have a .h file and a .cpp file for each object.
2) Your program will have a main.cpp file for the main function.
3) There should be a Dice class and a SetOfDice class.
4) Your program should run each simulation 20000 times. (There will be three separate simulation
runs.)
5) 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.)
6)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.
7)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.
8)The program will then ask you if you want to rerun the simulation if you answer with
Explanation / Answer
---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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.