You will create the Die, LoadedDie, and Game classes. You will use them in the s
ID: 3663054 • Letter: Y
Question
You will create the Die, LoadedDie, and Game classes. You will use them in the simply dice rolling game. • Die class- it requires an integer N that is the number of sides and returns a random integer between 1 and N. • LoadedDie class- it inherits the behavior and elements of Die, but the number it returns is biased such that the average of rolls is higher than for Die • Game class- it will implement the simple dice-rolling game. The user will specify the number of sides on the dice used by each “player”. They do not need to be the same size. The user will indicate if either or both “players” are using loaded dice. The user will also enter the number of rounds in the game. The Game class will create the necessary objects, play the game, and display the results to the user. The output should indicate the size of die used for each player, if it was the loaded die, and the final score.
Explanation / Answer
Die.cpp
#include<iostream>
#include <cstdlib>
using namespace std;
class Die
{
public:
Die();
Die(int numSide);
virtual int roll() const;
int rollTwoDice(Die d1, Die d2);
int side;
};
Die::Die():side(6){}
Die::Die(int numSide):side(numSide)
{
}
int Die::roll() const
{
return rand() % side + 1;
}
int Die::rollTwoDice(Die d1, Die d2)
{
return d1.roll()+d2.roll();
}
Loaded die.cpp
#include<iostream>
#include <cstdlib>
#include "Die.cpp"
using namespace std;
class LoadedDie: public Die
{
public:
LoadedDie();
LoadedDie(int numSide);
virtual int loadedroll() const;
};
LoadedDie::LoadedDie():side(6)
{
}
LoadedDie::LoadedDie(int numSide):side(numSide)
{
}
int LoadedDie::loadedroll() const
{
if ((rand() % 2)+1) = 1)
{
return side;
}
return (rand() % (side-1)) + 1;
}
int LoadedDie::rollTwoDice(LoadedDie d1, LoadedDie d2)
{
return d1.roll()+d2.roll();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.