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

C++ USING CLASSES (NEED A HEADER FILE AND PROGRAM) Use the classes you have writ

ID: 3803545 • Letter: C

Question

C++ USING CLASSES (NEED A HEADER FILE AND PROGRAM)

Use the classes you have written in the lab (Die and Pair of Dice) to design and implement the MyGame class. This class represents a game where the user computes against the computer. The goal is to get 100 points before your opponent. The game rules are as follows:

On each turn, the current player rolls a pair of dice repeatedly until 1 is rolled (on either dice) or the player chooses to hold. If 1 is rolled, all of the points rolled (total of dice faces) during that turn are added to the score except when 1 was rolled. If the user chooses to hold, all the points during that turn are added to the score.

If 1 is rolled on both dice, that player’s turn is over with no points earned, and the dice control moves to the other player.


Scoring Examples:

Elma rolls a (3, 3) and decides to continue. She then chooses to roll seven more times {(6,2), (6, 4), (5, 6), (4, 3), (5, 2), (6, 6), (1,1)}. Because she rolled (1, 1), her turn ends and she earns 0 points.

Craig rolls (6, 2) and decides to continue. He then chooses to roll four more times {(3, 3), (4, 2), (5, 2), (6, 6)} and decides to hold. He earns 39 points.

Dana rolls (6, 2) and decides to continue. She then chooses to roll four more times {(2, 3), (3, 2), (5, 5), (6, 1)}. Because she rolled (6, 1), her turn ends and she earns 28 points.

Implement the game such that the computer player always holds after accumulating 20 or more points in any given round, and once a player scores >= 100 points, the player wins (without waiting for a hold, or 1 to be rolled for the points to be added).

Explanation / Answer

#include<iostream>
#include <cstdlib>
//To define maximum player
#define MAX 11
using namespace std;
//Class MyGame definition
class MyGame
{
//Data member declaration
string playerName[MAX];
int playerPoint[MAX];
public:
int playerCount;
//Accept number of players and player name
void accept()
{
//Accepts number of players
cout<<" Enter how many players (must be less than 10): ";
cin>>playerCount;
//Loops till number of players and accept name
for(int x = 0; x < playerCount; x++)
{
cout<<" Enter player "<<(x + 1)<<" name: ";
cin>>playerName[x];
playerPoint[x] = 0;
}//End of loop
}//End of function

//Roll Dice for player number specified
void rollDice(int pno)
{
//For dice1 and dice2
int d1, d2;
//For choice
char ch;
//Displays the name of the player for rolling
cout<<endl<<playerName[pno]<<" rolls "<<endl;
//Loops till player score is greater than 100 or dice1 and dice2 is one
do
{
//Roll dice1
d1 = (rand()%6)+1;
//Roll dice2
d2 = (rand()%6)+1;
//Displays dice1 and dice 2 value
cout<<" Dice1 = "<<d1<<" Dice2 = "<<d2;
//Checks if Dice1 and Dice2 value is one then break
if(d1 == 1 && d2 == 1)
{
break;
}//End of if
//Otherwise add dice1 and dice2 value and add it with previous playerPoint value
else
playerPoint[pno] += (d1 + d2);
//Checks if playerPoint is greater than 100 then display the name of player won with points
if(playerPoint[pno] >= 100)
{
cout<<" Player "<<playerName[pno]<<" won the game: "<<playerPoint[pno];
exit(0);
}//End of if
//Accept data for holding
cout<<" Do you want to hold: (y / n)";
cin>>ch;
//If choice is y then break
if(ch == 'Y' || ch == 'y')
{
break;
}//End of if
//Displays the player name and current player point after each roll
cout<<" Player "<<playerName[pno]<<" "<<playerPoint[pno];
}while(1);
}//End of function
};//End of class

//Main function
int main()
{
//Class object created
MyGame mg;
//Accepts number of players with their name
mg.accept();
int x = 0;
//Loops till players score is greater than 100
do
{
mg.rollDice(x);
x++;
if(x == mg.playerCount)
x = 0;
}while(1);
}//End of main

Output:

Enter how many players (must be less than 10): 2

Enter player 1 name: Pyari

Enter player 2 name: Mohan

Pyari rolls

Dice1 = 6 Dice2 = 6
Do you want to hold: (y / n)n

Player Pyari 12
Dice1 = 5 Dice2 = 5
Do you want to hold: (y / n)n

Player Pyari 22
Dice1 = 6 Dice2 = 5
Do you want to hold: (y / n)n

Player Pyari 33
Dice1 = 1 Dice2 = 1
Mohan rolls

Dice1 = 5 Dice2 = 3
Do you want to hold: (y / n)n

Player Mohan 8
Dice1 = 6 Dice2 = 6
Do you want to hold: (y / n)n

Player Mohan 20
Dice1 = 2 Dice2 = 4
Do you want to hold: (y / n)n

Player Mohan 26
Dice1 = 2 Dice2 = 6
Do you want to hold: (y / n)n

Player Mohan 34
Dice1 = 2 Dice2 = 3
Do you want to hold: (y / n)n

Player Mohan 39
Dice1 = 4 Dice2 = 1
Do you want to hold: (y / n)y

Pyari rolls

Dice1 = 4 Dice2 = 1
Do you want to hold: (y / n)n

Player Pyari 38
Dice1 = 3 Dice2 = 4
Do you want to hold: (y / n)n

Player Pyari 45
Dice1 = 5 Dice2 = 5
Do you want to hold: (y / n)n

Player Pyari 55
Dice1 = 4 Dice2 = 3
Do you want to hold: (y / n)n

Player Pyari 62
Dice1 = 3 Dice2 = 6
Do you want to hold: (y / n)y

Mohan rolls

Dice1 = 6 Dice2 = 1
Do you want to hold: (y / n)n

Player Mohan 51
Dice1 = 6 Dice2 = 1
Do you want to hold: (y / n)n

Player Mohan 58
Dice1 = 4 Dice2 = 5
Do you want to hold: (y / n)n

Player Mohan 67
Dice1 = 6 Dice2 = 2
Do you want to hold: (y / n)n

Player Mohan 75
Dice1 = 2 Dice2 = 1
Do you want to hold: (y / n)n

Player Mohan 78
Dice1 = 6 Dice2 = 4
Do you want to hold: (y / n)n

Player Mohan 88
Dice1 = 3 Dice2 = 4
Do you want to hold: (y / n)y

Pyari rolls

Dice1 = 4 Dice2 = 3
Do you want to hold: (y / n)n

Player Pyari 78
Dice1 = 4 Dice2 = 2
Do you want to hold: (y / n)n

Player Pyari 84
Dice1 = 6 Dice2 = 5
Do you want to hold: (y / n)y

Mohan rolls

Dice1 = 6 Dice2 = 3
Player Mohan won the game: 104

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