Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***Please help me implement a game using the Die class I created in C++. Instructions are below as well as desired output, and following is

***Please help me implement a game using the "Die" class I created in C++. Instructions are below as well as desired output, and following is my code that I have made so far. I am using a Mac.

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

Make Die object that will be rolled by the player.

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

Make 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.
  • 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.

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. 

My code:

#include

#include

#include

using namespace std;

//class definition

class Die

{

public:

Die();//default constructor used when die is created

void roll(); //roll method

int getSide();//getSide returns the private int side

private:

int side; //holds the random number generated by roll()

static const int NUM_SIDES; //these will not change, value is always 6, use this with rand() to roll

}; //end of Die class

const int Die::NUM_SIDES = 6;

const int BIG_FISH = 20; //dice value of 1, worth 20 points

const int OLD_SHOE = -10; //value of 2

const int GOLDFISH = 5; //value of 3

const int FISH = 10; //value of 4

const int TOILET_SEAT = -20; //value of 5

const int SHARK = 40; //value of 6

//driver program to test dice class

int main()

{

//seed the random number generator

//Note: this must be done BEFORE creating any Die class objects

srand(6);

//Make a Die class object and display the side that is currently up

Die die1;

cout << "The current side is " << die1.getSide() << endl << endl;

//Roll the die 10 times to test the roll and getSide methods

for( int cnt = 1; cnt <= 10; cnt++ )

{

die1.roll();

cout << "Roll " << cnt << ":" << endl

<< "You rolled a " << die1.getSide() << endl << endl;

}

return 0;

}

Die::Die() //default constructor

{

roll();

}

void Die::roll() //roll method body

{

side = rand() % NUM_SIDES +1; //set private member side to random number between 1 and 6 using constant NUM_SIDES

}

int Die::getSide()

{

return side; //returns the value in side

}

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