hey struggling with this assignment wondering if if I could

hey, struggling with this assignment, wondering if if I could get some help. Here is the project details

The following lists a Dice class that simulates rolling a die with a different number of sides. The default is a standard die with six sides. The rollTwoDice function simulates rolling two dice objects and returns the sum of their values. The srand function requires including cstdlib.

my class

------------------------------------


class Dice
{
public:
Dice();
DIce(int numSides);
virtual int rollDice()const;
protected:
int numSides;
};
Dice::Dice()
{
numSides = 6;
srand(time(NULL)); //Seeds random number generator
}
Dice::Dice(int numSides)
{
this->numSides = numSides;
srand(time(NULL)); //Seeds random number generator
}
int Dice::rollDice()const
{
return (rand() % numSides) + 1;
}
//Take Two dice objects, roll them, and return the sum
int rollTwoDice(const Dice& die1, const Dice& die2)
{
return die1.rollDice() +die2.rollDice();
}

Write a main function that creates two Dice objects with a number of sides of your choosing. Invoke the rollTwoDice function in a loop that iterates ten times and verify that the functions are working as expected.

My main

----------------------

int main( )
{
   //Uncomment the line below for regular dice
   //Dice die1(6), die2(6);
   LoadedDice die1(6), die2(6);

   // This would be the game; here we just simulate it rolling 10 times
   for (int i = 0; i < 10; i++)
   {
       int total = rollTwoDice(die1, die2);
       cout << total << \" \";
   }
   cout << endl;

   return 0;
}

Next create your own class, LoadedDIce, that is derived from DIce. Add a default constructor and a constructor that takes the number of sides as input. Override the rollDice function in LoadedDice so that with a 50% chance he function returns the largest number possible (i.e., numSides), otherwise it returns what Dice\'s rollDice function returns.

Test your class by replacing the Dice objects in main with LoadedDice objects. You should nto need to change anything else. There should be many more dice rolls with the highest possible value. Polymorphism results in LoadedDice\'s rollDice function to be invoked instead of Dice\'s rollDice function inside rollTwoDice.

Solution

#define ROLLS 20
#include \"Dice.h\"
#include <iostream> // cout endl
#include <time.h> // time
using ncoop::Dice;
using ncoop::LoadedDice;
using std::cout;
using std::endl;

void testDice( Dice & );
int rollTwoDice(const Dice& die1, const Dice& die2);

int main( )

{
cout << \"... Dice and LoadedDice driver ...\ \";
srand(static_cast<unsigned int>(time(NULL))); // Seeds the random No. generator

cout << \"\ First, normal dice\";
Dice D6, D4(4);
testDice( D6 );
testDice( D4 );

cout << \"\ Next, loaded dice\";
LoadedDice D6l, D10l(10);
testDice( D6l );
testDice( D10l );

return 0;
}

void testDice( Dice &D ) {
cout << endl << D.getSides( ) << \"-sided die, single\ \";
for (size_t j = 0; j < ROLLS; j++)
cout << D.rollDice( ) << \" \";

cout << endl << D.getSides( ) << \"-sided dice, pair\ \";
for (size_t j = 0; j < ROLLS; j++)
cout << rollTwoDice( D, D ) << \" \";

cout << endl;
}

// Take dice objects and roll them and return the sum
int rollTwoDice(const Dice& die1, const Dice& die2)
{
return die1.rollDice() + die2.rollDice();
}

hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling
hey, struggling with this assignment, wondering if if I could get some help. Here is the project details The following lists a Dice class that simulates rolling

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site