14. Fishing Game Simulation For this assignment, you will write a program that s
ID: 3699481 • Letter: 1
Question
14. Fishing Game Simulation For this assignment, you will write a program that simulates a fishing game. In this game, a six-sided die is rolled to determine what the user has caught. Each possible item is worth a certain number of fishing points. The points will not be displayed until the user has finished fishing, then a message is displayed congratulating the user depending on the number of fishing points gained. Here are some suggestions for the game's design: . Each round of the game is performed as an iteration of a loop that repeats as long as the player wants to fish for more items. . At the beginning of each round, the program will ask the user whether he or she wants to continue fishing. The program simulates the rolling of a six-sided die (use the Die class that was demonstrated in this chapter). .Each item that can be caught is represented by a number generated from the die. For example, 1 for "a huge fish," 2 for "an old shoe," 3 for "a little fish," and so on. . Each item the user catches is worth a different amount of points. . The loop keeps a running total of the user's fishing points. . After the loop has finished, the total number of fishing points is displayed, along with a message that varies depending on the number of points earned.Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
// Class Die definition
class Die
{
private:
// To store number of sides
int numberOfSides;
// To store die value
int dieValue;
public:
// Prototype of member functions
Die(int = 6);
void rollDie();
int getNumberOfSides();
int getDieValue();
};// End of class
// Parameterized constructor to initialize data members
Die::Die(int numberOfSides)
{
unsigned seed = time (0);
srand(seed);
this->numberOfSides = numberOfSides;
// Calls the function roll die
rollDie();
}// End of constructor
// Function to roll die
void Die::rollDie()
{
const int MINVALUE = 1;
// Generates random number between 1 and number of sides
dieValue = (rand() % (numberOfSides - MINVALUE + 1 )) + MINVALUE;
}// End of function
// Function to return number of sides
int Die::getNumberOfSides()
{
return numberOfSides;
}// End of function
// Function to return die value
int Die::getDieValue()
{
return dieValue;
}// End of function
// main function definition
int main()
{
// Constant for number of sides
const int DIESIDES = 6;
// Creates an object of Die class using parameterized constructor with number of sides as 6
Die dieObject(DIESIDES);
// Creates different type of point values
int hugePoint = 20, oldPoint = -7, tinyPoint = 5, normalPoint = 10, sockPoint = -1, toiletPoint = -20;
// To store different point total
int hugeTotal = 0, oldTotal = 0, tinyTotal = 0, normalTotal = 0, sockTotal = 0, toiletTotal = 0, roundTotal = 0;
// To store grand total
double grandTotal = 0;
// To store user choice
char choice;
// To store fish type
string fishType;
// Welcome message
cout<<"***************************************************************************** ";
cout<<"* Welcome to my Fishing Game * ";
cout<<"***************************************************************************** ";
// Clears screen
system("cls");
// Loops till user choice is not 'n' or 'N'
do
{
// Calls the function to roll die
dieObject.rollDie();
// Calls the function to get die value and checks its value is 1
if(dieObject.getDieValue() == 1)
{
// Stores the fish type caught
fishType = "HUGE FISH";
// Calculates grand total
grandTotal += hugePoint;
// Calculates round total
roundTotal = hugePoint;
// Calculates type total
hugeTotal++;
}// End of if condition
// Calls the function to get die value and checks its value is 2
if(dieObject.getDieValue() == 2)
{
// Stores the fish type caught
fishType = "Old Shoe";
// Calculates grand total
grandTotal += oldPoint;
// Calculates round total
roundTotal = oldPoint;
// Calculates type total
oldTotal++;
}// End of if condition
// Calls the function to get die value and checks its value is 3
if(dieObject.getDieValue() == 3)
{
// Stores the fish type caught
fishType = "Tiny Fish";
// Calculates grand total
grandTotal += tinyPoint;
// Calculates round total
roundTotal = tinyPoint;
// Calculates type total
tinyTotal++;
}// End of if condition
// Calls the function to get die value and checks its value is 4
if (dieObject.getDieValue() == 4)
{
// Stores the fish type caught
fishType = "Normal Sized Fish";
// Calculates grand total
grandTotal += normalPoint;
// Calculates round total
roundTotal = normalPoint;
// Calculates type total
normalTotal++;
}// End of if condition
// Calls the function to get die value and checks its value is 5
if(dieObject.getDieValue() == 5)
{
// Stores the fish type caught
fishType = "Socks";
// Calculates grand total
grandTotal += sockPoint;
// Calculates round total
roundTotal = sockPoint;
// Calculates type total
sockTotal++;
}// End of if condition
// Calls the function to get die value and checks its value is 6
if(dieObject.getDieValue() == 6)
{
// Stores the fish type caught
fishType = "Toilet Paper";
// Calculates grand total
grandTotal += toiletPoint;
// Calculates round total
roundTotal = toiletPoint;
// Calculates type total
toiletTotal++;
}// End of if condition
// Displays the current information
cout<<" The die value is "<<dieObject.getDieValue()<< " so, you got a "<<fishType<<endl;
cout<<" Your "<<fishType<<" will give you "<<roundTotal<<" points. ";
cout<<" Your total score till now: "<<grandTotal<<endl;
cout<<" ";
cout<<" Would you like to continue? ";
// Accepts user choice
cout<<" Please enter Y for YES or N for NO :";
cin>>choice;
// Checks if the choice is 'N' or 'n' come out of the loop
if(choice == 'N' || choice == 'n')
break;
}while (choice == 'y' || choice == 'Y'); // End of do while loop
// Displays the final score with information
cout<<"******************************************************************************* ";
cout<<"* Your Score * ";
cout<<"******************************************************************************* ";
cout<< " You caught a HUGE FISH "<<hugeTotal<<" times. You got: "<<(hugeTotal * 20)<<" points " ;
cout<< " You caught a Old Shoe "<<oldTotal<<" times. You got: "<<(oldTotal * -7)<<" points " ;
cout<< " You caught a Tiny Fish "<<tinyTotal<<" times. You got: "<<(tinyTotal * 5)<<" points " ;
cout<< " You caught a Normal Fish "<<normalTotal<<" times. You got: "<<(normalTotal * 10)<<" points " ;
cout<< " You caught a Mud Puppy "<<sockTotal<<" times. You got: "<<(sockTotal * -1)<<" points " ;
cout<< " You caught a Toilet Seat "<<toiletTotal<<" times. You got: "<<(toiletTotal * -20)<<" points " ;
cout<<"******************************************************************************* ";
cout<< " Your total score is :"<<grandTotal<<" ";
cout<<"******************************************************************************* ";
return 0;
}// End of main function
Sample Output:
The die value is 5 so, you got a Socks
Your Socks will give you -1 points.
Your total score till now: -1
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 5 so, you got a Socks
Your Socks will give you -1 points.
Your total score till now: -2
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 3 so, you got a Tiny Fish
Your Tiny Fish will give you 5 points.
Your total score till now: 3
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 5 so, you got a Socks
Your Socks will give you -1 points.
Your total score till now: 2
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 5 so, you got a Socks
Your Socks will give you -1 points.
Your total score till now: 1
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 2 so, you got a Old Shoe
Your Old Shoe will give you -7 points.
Your total score till now: -6
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 6 so, you got a Toilet Paper
Your Toilet Paper will give you -20 points.
Your total score till now: -26
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 1 so, you got a HUGE FISH
Your HUGE FISH will give you 20 points.
Your total score till now: -6
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 1 so, you got a HUGE FISH
Your HUGE FISH will give you 20 points.
Your total score till now: 14
Would you like to continue?
Please enter Y for YES or N for NO :y
The die value is 5 so, you got a Socks
Your Socks will give you -1 points.
Your total score till now: 13
Would you like to continue?
Please enter Y for YES or N for NO :n
*******************************************************************************
* Your Score *
*******************************************************************************
You caught a HUGE FISH 2 times. You got: 40 points
You caught a Old Shoe 1 times. You got: -7 points
You caught a Tiny Fish 1 times. You got: 5 points
You caught a Normal Fish 0 times. You got: 0 points
You caught a Mud Puppy 5 times. You got: -5 points
You caught a Toilet Seat 1 times. You got: -20 points
*******************************************************************************
Your total score is :13
*******************************************************************************
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.