implement and use the methods for a class called Die. Die class The Die class is
ID: 3763266 • Letter: I
Question
implement and use the methods for a class called Die.
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:
static const int NUM_SIDES;
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.
In the class definition, define the symbolic constant with the keyword "static" before the definition. So:
Before main, initialize the symbolic constant with its value. So:
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 http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240test9.cpp
The output that is produced by the test program:
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 fishing game that uses the Die class.
The concept of the game is fairly simple. A player will try to catch as many fish as possible using a specific number of casts. Each item that is caught will either add points to the player's score or deduct points from the player's score.
Each cast will be simulated by rolling a single die. The side of the die that is up will represent what is caught. A value of:
1 represents a big fish and is worth 20 points
2 represents an old shoe and is worth -10 points
3 represents a goldfish and is worth 5 points
4 represents a fish and is worth 10 points
5 represents a toilet seat and is worth -20 points
6 represents a shark and is worth 40 points
Implementing the game
Set the seed value that is used by the random number generator by using the srand() function. Pass the value time(NULL) to the srand function.
Create a die object that will be rolled by the player.
Ask the player for the number of times they would like to cast.
Write a loop that will execute the number of times the player wants to cast. Inside of the loop:
Roll the die object.
Display a count of the number of times the die has been rolled, what the value of the side of the die that is up, what the user caught, and how many points the player earned or lost.
Increment or decrement the player's score according to what they caught.
Once the player has used all of their casts, display their final score.
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.
Programming Note
You may change the items that are caught if things like "big fish" and "toilet seat" are too boring. However, you must keep the point values the same as listed above for each side of the die. If you do make changes to the items, please try to keep them clean/G-rated - if an item would make you blush when talking about it with your grandmother, don't use it in the program.
Output
A couple runs of the program might resemble the following but keep in mind that the random number generator is being used so your output will probably not match this output completely.
Run 1:
Run 2:
Explanation / Answer
/***************************************************************
CSCI 240 Program 9 Fall 2015
Test the Die class
***************************************************************/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
//********** Put the Die class definition after this line **********
class Die {
int currentSide;
public:
Die() {
currentSide=1;
}
int getValue() {
return currentSide;
}
void roll() {
currentSide = (rand()%6)+1;
}
};
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 **********
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/***************************************************************
CSCI 240 Program 9 Fall 2015
Test the Die class
***************************************************************/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
//********** Put the Die class definition after this line **********
class Die {
int currentSide;
public:
Die() {
currentSide=1;
}
int getValue() {
return currentSide;
}
void roll() {
currentSide = (rand()%6)+1;
}
};
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;
//Roll the die 10 times to test the roll and getValue methods
cout<<"How many times do you want to try to catch a fish? ";
int n;
cin>>n;
for( int cnt = 1; cnt <= n; cnt++ )
{
die1.roll();
int val=die1.getValue();
cout << endl << endl << "Roll " << cnt << ":" << endl
<< "You rolled a " << val ;
cout<<" and caught ";
switch (val) {
case 1:
cout<<"a big fish: 20 points";
break;
case 2:
cout<<"an old shoe: -10 points";
break;
case 3:
cout<<"a goldfish: 5 points";
break;
case 4:
cout<<"a fish: 10 points";
break;
case 5:
cout<<"a toilet seat: -20 points";
break;
case 6:
cout<<"a shark: 40 points";
break;
}
cout<<" ";
}
return 0;
}
//********** Code the Die class constructor and methods after this line **********
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.