Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

implement and use the methods for a class called Die. The Die class The Die class is used to represent a 6-sided die. Data Members

implement and use the methods for a class called Die.

The 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 symbolic constant that will work for all compilers:

  1. In the class definition, define the symbolic constant with the keyword "static" before the definition. So:
  2. static const int NUM_SIDES;
  3. Before int main(), initialize the symbolic constant with its value. So:
  4. const int Die::NUM_SIDES = 6;
  5. 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

The constructor for the class is used to create Die object. It is a default constructor, which means that it takes no arguments.

It should simply call therollmethod that is described below to initialize the integer data member that holds the value of the side of the die that is up.

Methods

The following methods are required for the Die class.

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.

Note: DO NOT call the srand() function in this method.

int getSide()

This public 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 the methods work. A short program has been written that will test each of the methods individually.

The test program can be downloaded fromhttp://faculty.cs.niu.edu/~byrnes/csci240/pgms/240test8.cpp

The WINDOWS output that is produced by the test program:

The current side is 5 Roll 1: You rolled a 2 Roll 2: You rolled a 6 Roll 3: You rolled a 2 Roll 4: You rolled a 5 Roll 5: You rolled a 4 Roll 6: You rolled a 5 Roll 7: You rolled a 5 Roll 8: You rolled a 3 Roll 9: You rolled a 3 Roll 10: You rolled a 5 

The MAC output that is produced by the test program:

The current side is 1 Roll 1: You rolled a 1 Roll 2: You rolled a 3 Roll 3: You rolled a 5 Roll 4: You rolled a 4 Roll 5: You rolled a 6 Roll 6: You rolled a 1 Roll 7: You rolled a 3 Roll 8: You rolled a 3 Roll 9: You rolled a 2 Roll 10: You rolled a 5 

The CODECHEF output that is produced by the test program:

The current side is 4 Roll 1: You rolled a 2 Roll 2: You rolled a 5 Roll 3: You rolled a 6 Roll 4: You rolled a 3 Roll 5: You rolled a 6 Roll 6: You rolled a 5 Roll 7: You rolled a 4 Roll 8: You rolled a 5 Roll 9: You rolled a 5 Roll 10: You rolled a 6 

If the output, using the 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 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

Use a value of 9 to seed the random number generator.

Create Die object that will be rolled by the player.

Ask the player for the number of times they would like to cast.

Write 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.
  • Use a switch statement to increment or decrement the player's score according to what was caught.

Once all the casts have been made, display the player's final score.

Programming Requirements

  1. Each method MUST have a documentation box like a function.
  2. Hand in a copy of the source code from part 2 of the assignment using Blackboard.

Programming Note

  1. Changes may be made to the items that are caught if things like "big fish" and "toilet seat" are too boring. However, the point values MUST be kept the same as listed above for each side of the die. Any changes to the items MUST be kept clean/G-rated - if an item would make one blush when talking about it with one's grandmother, don't use it in the program.
  2. Any changes that are made to the items that are caught MUST be thoroughly documented.

Output

Run 1 (using srand(9);) on Windows PC

How many times do you want to try to catch a fish? 15 Roll 1: You rolled a 2 and caught an old shoe! -10 points Roll 2: You rolled a 2 and caught an old shoe! -10 points Roll 3: You rolled a 1 and caught a big fish! 20 points Roll 4: You rolled a 5 and caught a toilet seat! -20 points Roll 5: You rolled a 6 and caught a shark! 40 points Roll 6: You rolled a 4 and caught a fish! 10 points Roll 7: You rolled a 1 and caught a big fish! 20 points Roll 8: You rolled a 5 and caught a toilet seat! -20 points Roll 9: You rolled a 2 and caught an old shoe! -10 points Roll 10: You rolled a 5 and caught a toilet seat! -20 points Roll 11: You rolled a 4 and caught a fish! 10 points Roll 12: You rolled a 5 and caught a toilet seat! -20 points Roll 13: You rolled a 1 and caught a big fish! 20 points Roll 14: You rolled a 2 and caught an old shoe! -10 points Roll 15: You rolled a 1 and caught a big fish! 20 points Your final score is 20 points. 

Run 2 (using srand(9);) on a Mac

How many times do you want to try to catch a fish? 15 Roll 1: You rolled a 3 and caught a goldfish! 5 points Roll 2: You rolled a 4 and caught a fish! 10 points Roll 3: You rolled a 3 and caught a goldfish! 5 points Roll 4: You rolled a 3 and caught a goldfish! 5 points Roll 5: You rolled a 6 and caught a shark! 40 points Roll 6: You rolled a 1 and caught a big fish! 20 points Roll 7: You rolled a 1 and caught a big fish! 20 points Roll 8: You rolled a 4 and caught a fish! 10 points Roll 9: You rolled a 2 and caught an old shoe! -10 points Roll 10: You rolled a 4 and caught a fish! 10 points Roll 11: You rolled a 6 and caught a shark! 40 points Roll 12: You rolled a 6 and caught a shark! 40 points Roll 13: You rolled a 1 and caught a big fish! 20 points Roll 14: You rolled a 4 and caught a fish! 10 points Roll 15: You rolled a 6 and caught a shark! 40 points Your final score is 265 points. 

Run 3 (using srand(9);) on CodeChef

How many times do you want to try to catch a fish? 15 Roll 1: You rolled a 3 and caught a goldfish! 5 points Roll 2: You rolled a 2 and caught an old shoe! -10 points Roll 3: You rolled a 2 and caught an old shoe! -10 points Roll 4: You rolled a 3 and caught a goldfish! 5 points Roll 5: You rolled a 6 and caught a shark! 40 points Roll 6: You rolled a 3 and caught a goldfish! 5 points Roll 7: You rolled a 5 and caught a toilet seat! -20 points Roll 8: You rolled a 3 and caught a goldfish! 5 points Roll 9: You rolled a 3 and caught a goldfish! 5 points Roll 10: You rolled a 5 and caught a toilet seat! -20 points Roll 11: You rolled a 2 and caught an old shoe! -10 points Roll 12: You rolled a 5 and caught a toilet seat! -20 points Roll 13: You rolled a 2 and caught an old shoe! -10 points Roll 14: You rolled a 2 and caught an old shoe! -10 points Roll 15: You rolled a 6 and caught a shark! 40 points Your final score is -5 points. 

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions