Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a dice simulator. Start by writing a die class that can represent a certain number of sides such as a standard D6. Create a

Create a dice simulator. Start by writing a die class that can represent a certain number of sides such as a standard D6.

Create a constructor for setting up the die with appropriate sides.

Make a member function for rolling the die. This function should simulate a random number between 1-x (x=sides of die). The number will then be assigned to the value parameter of the die

Make a member function that will get the value of the die. This will be a simple accessor function.

Write a program to implement two dice using the class and that will simulate their functionality use whatever programming you feel necessary to display and show off your dice simulator. Ask the user for how many sides they would like on their dice. Allow the user to continue rolling until they choose to quit testing the simulation.

Tips for success

This is a chance for your design and skills application to shine. Don't skimp on the functionality.

Don't forget to use constants when you can

Make sure the class is in a .h file and member functions are in a .cpp file

Make sure you're returning ints and not chars/strings when getting dice values. It's crucial we be able to treat these as mathematical values.

Remember, data is private by default. Keep it that way and build lanes to safely access the data. Keep that vision.

this is my code but its not running properly. What am I doing wrong?

#pragma once //header file

class Dice

{

private:

int DiceValue;

int NumberofSides;

public:

Dice(const int);

void throwDice();

int getDiceValue();

};

#include "Dice.h" //implementation file

#include

Dice::Dice(const int NumberofSides)

{

this->NumberofSides = NumberofSides;

}

void Dice::throwDice()

{

DiceValue = rand() % NumberofSides + 1;

}

int Dice::getDiceValue()

{

return DiceValue;

}

#include //main file

#include "Dice.cpp"

using namespace std;

int main()

{

int value1;

int value2;

char choice;

cout << "Enter the number of sides you want for dice number 1 ";

cin >> value1;

cout << "Enter the number of sides you want for dice number 2 ";

cin >> value2;

Dice dice1(value1);

Dice dice2(value2);

cout << "Do you wish to throw the dice? ";

cin >> choice;

if(choice == 'Y' || choice == 'y')

{

dice1.throwDice();

dice2.throwDice();

cout << "The value for dice number 1 is: " << dice1.getDiceValue() << endl;

cout << "The value for dice number 2 is: " << dice2.getDiceValue() << endl;

}

else

{

cout << "You are finished playing dice ";

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

What is the relation of physical mathematics with examples?

Answered: 1 week ago

Question

What are oxidation and reduction reactions? Explain with examples

Answered: 1 week ago