Die class The Die class is used to represent a 6-sided die. Data Members The cla
ID: 3695156 • Letter: D
Question
Die class
The Die class is used to represent a 6-sided die.
Data Members
The class contains two data members.
An integer that holds the value of the side of the die that is up
An integer symbolic constant that holds the maximum number of sides on the die. Use a value of 6.
C++ 11 allows for symbolic constants to be initialized in the class definition. However, it's not something that is supported by all compilers because class definitions are treated as patterns for what the class should contain, and therefore, they don't take up any memory. To create a symbolic constant that will work for all compilers:
In the class definition, define the symbolic constant with the keyword "static" before the definition. So:
static const int NUM_SIDES;
Before main, initialize the symbolic constant with its value. So:
const int Die::NUM_SIDES = 6;
Note the dropping of the keyword "static" and the inclusion of the "Die::" in front of the constant name. This lets the compiler know that the constant belongs to the Die class.
Constructor
This class has one constructor, a default constructor (ie. one that takes no arguments). It should simply call the roll method that is described below to initialize the value of the side of the die that is up.
Methods
void roll()
This method simulates the rolling of the die. It takes no arguments and returns nothing.
The die will be "rolled" by using the rand() function to generate a random value for the side of the die that is up. The value should be between 1 and 6. Use the symbolic constant data member to help to limit the values. The result should be assigned to the integer data member.
int getValue()
This accessor method returns the current side of the die that is facing up. It takes no arguments and returns an integer.
Part 1: Testing the Die class
Before using the Die class as part of a larger project, it should be tested to make sure that the constructor and all of the methods work. A short program has been written that will test each one of the methods individually.
The test program can be downloaded from Blackboard or the course website: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240test9.cpp
The output that is produced by the test program:
The current side is 1
Roll 1:
You rolled a 2
Roll 2:
You rolled a 4
Roll 3:
You rolled a 6
Roll 4:
You rolled a 1
Roll 5:
You rolled a 3
Roll 6:
You rolled a 6
Roll 7:
You rolled a 4
Roll 8:
You rolled a 5
Roll 9:
You rolled a 1
Roll 10:
You rolled a 3
If the output, using your Die class, matches the above output, move on to part 2 of this assignment. Otherwise, fix the constructor/methods until the output does match.
Part 2: Using the Die class
This is the part of the assignment that will be submitted for grading.
For this part of the program, create a game that uses the Die class.
The concept of the game is fairly simple. A pair of standard six-sided die will be rolled by a dealer. A player will "wager" on whether the sum total of the two dice will be even or odd. If the player wagered correctly, they win and receive a point. If the player wagered incorrectly, they lose and the dealer receives a point.
Implementing the game
Set the seed value that is used by the random number generator by using the srand() function. Pass the value time(0) or time(NULL) to the srand function.
Create two die object that will be rolled by the dealer.
Ask the player for the number of times they would like to have the die rolled by the dealer and whether the rolls will be even or odd. Have the user enter an integer value of 0 to represent even or 1 to represent odd.
Write a loop that will execute the number of times the player wants to have the die rolled. Inside of the loop:
Roll the die objects.
Check if the sum of the die is even or odd. If the player wagered correctly, display a message that the player won and award him/her a point. If the player wagered incorrectly, display a message that the dealer won and give them a point.
Once the die have been rolled the correct number of times, display the player's score, the dealer's score, and who won the game or if it was a tie.
Programming Requirements
Make sure to add #include statements for the cstdlib, ctime, and cstring libraries.
Each method must have a documentation box like a function.
Hand in a copy of the source code from part 2 of the assignment using Blackboard
For up to 5 points of extra credit, add a method called draw to the Die class. The draw method should "draw" the side of the die that is currently up. For example, if 3 is the side that is up, then the draw method should "draw" something like:
-----
|O |
| O |
| O|
-----
on the screen.
In main, use the draw method inside of the loop. It should be called to display what the dealer rolled rather than having that value displayed in text.
For reference, the sides of the die should resemble:
Image courtesy of http://www.clker.com/clipart-7188.html.
Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don't take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.
Extra Credit Output
Run 1:
How many times do you want to play? 5
Even or odd (0 or 1)? 0
Roll 1:
-----
|O |
| |
| O|
-----
-----
|O |
| O |
| O|
-----
Dealer wins!
Roll 2:
-----
|O |
| O |
| O|
-----
-----
|O O|
| O |
|O O|
-----
You win!
Roll 3:
-----
| |
| O |
| |
-----
-----
|O O|
|0 0|
|O O|
-----
Dealer wins!
Roll 4:
-----
|O O|
| O |
|O O|
-----
-----
|O O|
| O |
|O O|
-----
You win!
Roll 5:
-----
|O O|
| O |
|O O|
-----
-----
| |
| O |
| |
-----
You win!
Your final score is 3 points.
Dealer final score is 2 points.
You win!
the basic cco is
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
//********** Put the Die class definition after this line **********
int main()
{
//seed the random number generator
//Note: this must be done before creating any Die class objects
srand(5);
//Create a Die class object and display the side that is currently up
Die die1;
cout << "The current side is " << die1.getValue();
//Roll the die 10 times to test the roll and getValue methods
for( int cnt = 1; cnt <= 10; cnt++ )
{
die1.roll();
cout << endl << endl << "Roll " << cnt << ":" << endl
<< "You rolled a " << die1.getValue() << endl;
}
return 0;
}
//********** Code the Die class constructor and methods after this line **********
Explanation / Answer
#include #include #include #include using namespace std; // Function Prototypes void greeting(); void greeting2(); int die1(); int die2(); int dice (int, int); int results (int); // Main Funtion int main () { int counter; char secondstart; greeting (); int firstdie = die1(); greeting2 (); int secondie = die2(); int total = dice(firstdie, secondie); results (total); //coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.