You will design 3 classes, Die, LoadedDie and Game. . You will use inheritance t
ID: 3663061 • Letter: Y
Question
You will design 3 classes, Die, LoadedDie and Game.
. You will use inheritance to show that LoadedDie is derived from Die.
• 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
The Game class will not be part of an is-a relation, but a has-a relation. The Game class will need to keep track of the type of dice for each of the 2 players, the number of rounds to play, and some way to maintain the score. You will design a program to play a simplified version of war, using dice instead of cards. There will be only one user, but 2 “players” for the game. The user will indicate the number of rounds to play. The user will also specify for each “player” the number of sides on the dice used and if that player is using regular or loaded dice. For example one player could have a loaded 10-sided die while the other has a regular 4-sided die. No one said it had to be fair!
• 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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Die
{
protected:
int numSides;
string dieType;
public:
Die(int n)
{
numSides=n;
dieType="Not Loaded"; //default is not loaded
}
//virtual function will be overloaded in derived class
virtual int roll()
{
//returns a random number in the range 1 - numSides
return rand() % numSides + 1;
}
string getDieType()
{
return dieType;
}
int getNumSides()
{
return numSides;
}
};
class LoadedDie : public Die
{
public:
LoadedDie(int n) : Die(n)
{
turn = 1;
dieType = "Loaded";
}
/*Overriding the roll function to have a biased behaviour of returning the highest number for every alternate roll */
int roll()
{
//every odd turn return the numSides, (biased) and in even turn number usual roll from Die class.
int val= turn == 1 ? numSides : Die::roll();
turn=(turn+1)%2;
return val;
}
private:
int turn;
};
class Game
{
private:
//store the dice for the players
Die *d1, *d2;
//array to hold the result of each round for the 2 players. max is 100 roundss
int rounds[100][2];
int numRounds;
public :
Game()
{
d1 = d2 = NULL;
}
void input()
{
string response;
int faces;
srand(time(NULL));
cout<<"How many faces for the die of Player 1? ";
cin>>faces;
cout<<"Use loaded die for Player 1 (yes/no) ? " ;
cin>>response;
if(response.compare("yes")==0)
d1=new LoadedDie(faces);
else
d1=new Die(faces);
cout<<"How many faces for the die of Player 2? ";
cin>>faces;
cout<<"Use loaded die for Player 2 (yes/no) ? " ;
cin>>response;
if(response.compare("yes")==0)
d2=new LoadedDie(faces);
else
d2=new Die(faces);
cout << "How many rounds in the game?";
cin>>numRounds;
}
void play()
{
for(int i=0;i<numRounds;i++)
{
rounds[i][0]=d1->roll(); //player1
rounds[i][1]=d2->roll(); //player2
}
}
void results()
{
int points1=0,points2=0;
cout<<"Number of rounds played: "<<numRounds<<endl<<endl;
cout<<"Player 1 uses "<<d1->getDieType() << " die with "<<d1->getNumSides()<<" faces." <<endl;
cout<<"Player 2 uses "<<d2->getDieType() << " die with "<<d2->getNumSides()<<" faces."<<endl;
cout<<"Player 1 Player 2 "<<endl<<endl;
for(int i=0;i<numRounds;i++)
{
cout<<rounds[i][0]<<" "<<rounds[i][1]<<endl;
if(rounds[i][0]>rounds[i][1])
points1++;
else if(rounds[i][0]<rounds[i][1])
points2++;
}
cout<<"Player 1 wins "<<points1<<" times."<<endl;
cout<<"Player 2 wins "<<points2<<" times."<<endl;
if(points1>points2)
cout<<"Player 1 wins !";
else if (points2>points1)
cout<<"Player 2 wins !";
else
cout<<"Its a draw !!";
}
};
int main()
{
Game g;
g.input();
g.play();
g.results();
}
output
How many faces for the die of Player 1? 4
Use loaded die for Player 1 (yes/no) ? yes
How many faces for the die of Player 2? 4
Use loaded die for Player 2 (yes/no) ? no
How many rounds in the game?10
Number of rounds played: 10
Player 1 uses Loaded die with 4 faces.
Player 2 uses Not Loaded die with 4 faces.
Player 1 Player 2
4 4
3 4
4 2
1 3
4 4
2 1
4 2
4 2
4 3
4 3
Player 1 wins 6 times.
Player 2 wins 2 times.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.