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

Part 3: Dice Provide a group of C++ classes that implements a dice rolling facil

ID: 3747674 • Letter: P

Question

Part 3: Dice Provide a group of C++ classes that implements a dice rolling facility to be used during the attack phase. The dice rolling facility should enable the player object to decide how many 6-sided dice are being rolled (from 1 to 3 dice), and then return the values in a sorted container. Each player will have his own dice rolling object, and each dice rolling object must keep track of the percentage of each value that has been rolled up to now for this player in the game. You must deliver a driver that creates at least 2 dice rolling facility objects, tests that 1) one can request from 1 to 3 dice being rolled, 2) that the container returns the right number of values, 3) that no value outside of the 1-6 range is ever returned, 4) that there is an equal share of 1-6 values returned and 5) that every dice rolling facility object maintains a percentage of all individual values rolled up to now Enable the plaver obiect to decide how many 6-sided dice are being rolled (from 1 to 3 dice Put the rolled values in a sorted container Each plaver has a separate dice rolling obiect Each dice rolling object keeps track of each value that has been rolled up to now for this laver in the game Driver that test: 1) one can request from 1 to 3 dice being rolled, 2) that the container returns the right number of values in a sorted container, 3) that no value outside of the 1-6 range is ever returned, 4) that there is an equal share of 1-6 values returned and 5) that every dice rolling facility obiect maintains a percentage of all individual values rolled up to now

Explanation / Answer

// File Name: Diece.cpp
#include <iostream>
#include <stdlib.h> // srand, rand
#include <time.h> // time
using namespace std;

// Class Player definition
class Player
{
// Pointer to store die values
int *dieValues;
// To store number of rolls
int size;
public:
// Prototype of member functions
Player(int);
void playerRoll();
void sortDie();
void display();
double percentage();
};// End of class

// Parameterized constructor definition
Player::Player(int no)
{
// Dynamically allocates memory of size parameter value no
dieValues = new int[no];
// Sets the size to no
size = no;
}// End of parameterized constructor

// Function to roll die
void Player::playerRoll()
{
// Loops till number of rolls
for(int x = 0; x < size; x++)
// Generates random number between 1 and 6 and stores it in x position
dieValues[x] = rand() % 6 + 1;
}// End of function

// Function to sort die values
void Player::sortDie()
{
// Loops till number of rolls
for(int x = 0; x < size; x++)
{
// Loops till number of rolls minus outer loop value minus one times
for(int y = 0; y < size-x-1; y++)
{
// Checks if the current value is greater than the next value
if(dieValues[y] > dieValues[y+1])
{
// Swapping process
int temp = dieValues[y];
dieValues[y] = dieValues[y+1];
dieValues[y+1] = temp;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

// Function to display the die values
void Player::display()
{
// Loops till number of rolls
for(int x = 0; x < size; x++)
// Displays each roll value
cout<<" Roll - "<<(x + 1)<<": "<<dieValues[x];
}// End of function

// Function to calculate and return percentage of rolls
double Player::percentage()
{
double total = 0;
// Loops till number of rolls
for(int x = 0; x < size; x++)
// Calculates total
total += dieValues[x];
// Calculate and returns percentage
return (total / size);
}// End of function

// main function definition
int main()
{
int roll;
// Accepts number of rolls from user
cout<<" Enter number of roll (from 1 to 3 dice): ";
cin>>roll;

// Creates two player objects using parameterized constructor by passing number of rolls
Player player1(roll), player2(roll);

// Use current time as seed for random generator
srand(time(0));

// Loops till number of rolls
for(int x = 0; x < roll; x++)
{
// Calls the function for both the players to generate die roll
player1.playerRoll();
player2.playerRoll();
}// End of for loop

// Calls the function to sort the die values for both the players
player1.sortDie();
player2.sortDie();

// Calls the function to calculate percentage for both the players
player1.percentage();
player2.percentage();

// Calls the function to display the roll values for both the players
cout<<" Player - 1 rolled values: ";
player1.display();
cout<<" Player - 2 rolled values: ";
player2.display();

// Calls the function to display percentage for both the players
cout<<" Player - 1 percentage: "<<player1.percentage();
cout<<" Player - 2 percentage: "<<player2.percentage();

// Checks if player one percentage is greater than the percentage of player two
// then display player one won
if(player1.percentage() > player2.percentage())
cout<<" Player - 1 won.";
// Otherwise cChecks if player two percentage is greater than the percentage of player one
// then display player tow won
else if(player2.percentage() > player1.percentage())
cout<<" Player - 2 won.";
// Otherwise display tie
else
cout<<" Tie.";
return 0;
}// End of main function

Sample Output 1:

Enter number of roll (from 1 to 3 dice): 3

Player - 1 rolled values:
Roll - 1: 3
Roll - 2: 5
Roll - 3: 6
Player - 2 rolled values:
Roll - 1: 1
Roll - 2: 2
Roll - 3: 6
Player - 1 percentage: 4.66667
Player - 2 percentage: 3
Player - 1 won.

Sample Output 2:

Enter number of roll (from 1 to 3 dice): 2

Player - 1 rolled values:
Roll - 1: 3
Roll - 2: 5
Player - 2 rolled values:
Roll - 1: 4
Roll - 2: 4
Player - 1 percentage: 4
Player - 2 percentage: 4
Tie.