Question

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

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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 ...\n";
srand(static_cast<unsigned int>(time(NULL))); // Seeds the random No. generator

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

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

return 0;
}

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

cout << endl << D.getSides( ) << "-sided dice, pair\n";
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();
}

Add a comment
Know the answer?
Add Answer to:
hey, struggling with this assignment, wondering if if I could get some help. Here is the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • Using the PairOfDice class from PP 4.7, design and implement an application that rolls a pair...

    Using the PairOfDice class from PP 4.7, design and implement an application that rolls a pair of dice 1000 times, counting the number of box cars (two sixes) that occur. You're given - //******************************************************************** // PairOfDice.java Author: Lewis/Loftus // // Solution to Programming Project 4.8 //******************************************************************** public class PairOfDice { private Die die1, die2; //----------------------------------------------------------------- // Creates two six-sided Die objects, both with an initial // face value of one. //----------------------------------------------------------------- public PairOfDice() { die1 = new Die(); die2 =...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • (Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1...

    (Java Coding) Game1: You win if one get one six in four rolls of one dice. (To be Simulated 1,000,000 times) Game2: You win if one get double sixes in twenty four rolls of two dice. (To be simulated 1,000,000 times) Given below that the class die, oddstester, and gameSimulator(partially complete and the bold missing parts need to be done), need to find the probability of winning game 1 and 2 (after the 1 million plays) hence the getWins and...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • In need of some help with a LCR C++ game. The code will run, but I...

    In need of some help with a LCR C++ game. The code will run, but I need it to cycle through the turns automatically so that the only input from the user is the number of players. It's also showing the winner as the person with 0 chips which is incorrect and I can't figure out why. Any help is greatly appreciated as this is already late. Game rules: Develop a program that follows the rules of Left Center Right...

  • Required in C++ I'm asked to: Print a histogram in which the total number of times...

    Required in C++ I'm asked to: Print a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character like * that number of times. Below is my current code. I am not allowed to use arrays or anything too advanced as I just started the class. I would really appreciate the help as I've been stuck on it for a while now. I can only get it to print...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • I need help with these Java programming assignements. public class Die { //here you declare your...

    I need help with these Java programming assignements. public class Die { //here you declare your attributes private int faceValue; //operations //constructor - public Die() { //body of constructor faceValue=(int)(Math.random()*6)+1;//instead of 1, use random approach to generate it } //roll operation public void roll() { faceValue=(int)(Math.random()*6)+1; }    //add a getter method public int getFaceValue() { return faceValue; }    //add a setter method public void setFaceValue(int value) { faceValue=value; } //add a toString() method public String toString() { String...

  • Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...

    Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include <iostream> using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test the money class ****************************************************************/ int main() {    int dollars;    int cents;    cout << "Dollar amount: ";    cin >> dollars;    cout << "Cents amount: ";    cin >> cents;    Money m1;    Money m2(dollars);    Money m3(dollars, cents);    cout << "Default constructor: ";    m1.display();    cout << endl;    cout << "Non-default constructor 1: ";    m2.display();    cout << endl;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT