For this assignment, you will write a program that simulates a fishing game. In
ID: 3767541 • Letter: F
Question
For this assignment, you will write a program that simulates a fishing game. In this game, a sixsided 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, and then a message is displayed congratulating the user depending on the number of fishing points gained.
I have attached the code below. The problem is input validation within the game and the code breaks very easily. Please help with fixing input validation so that nothing other than the proper input values work, and with the game looping continuously until the user does not want to play anymore.
Dice.H
#ifndef DICE_H // Dice header file
#define DICE_H// Dice header file
#include<time.h> // for random function
#include <stdlib.h>// for random function
using namespace std; // for cout, cin
class Dice // Dice class declaration
{
public: // public member functions
int dicevalue; // dicevalue holds value after dice is rolled; data type int
int rollDice(){ // rollDice function rolls the dice
srand(time(NULL)); // seeds random number generator; initializes time to NULL
int dicevalue = rand() %6 +1; // dicevalue integer is assigned to a random number 1 through 6
return dicevalue; // random number stored in dice value is returned
}// end of rolldice function
}; // end of class declaration
#endif // end of header file
Points.H
#ifndef POINTS_H // Points header file
#define POINTS_H// Points header file
#include<iostream> // for cout, cin
#include "Dice.h"// include Dice header file
using namespace std; // for cout,cin
class Points{ // Points class declaration
public: // public member variables
int dicevalue; // variable that holds value of dice; data type int
int total; // variable that holds total points earned in game round; data type int
int getpoints(int dicevalue){ // getPoints function to assign rolled number with points earned and item caught while fishing; passes dicevalue
switch(dicevalue){ // switch case declaration; cases 1-6 ( values of possible die rolls)
case 1: // when 1 is rolled
cout<<" You rolled "<<dicevalue<<" "<<endl; // displays to user what number has been rolled
cout<<"Huge Fish "; // displays what the user caught while fishing
total=0+20; // total is incremented by 20
break; // break
case 2:// when 2 is rolled
cout<<" You rolled "<<dicevalue<<" "<<endl;// displays to user what number has been rolled
cout<<"Old shoe ";// displays what the user caught while fishing
total=0+5;// total is incremented by 5
break; // break
case 3: // when 3 is rolled
cout<<" You rolled "<<dicevalue<<" "<<endl; // displays to user what number has been rolled
cout<<"Little Fish ";// displays what the user caught while fishing
total=0+10; // total is incremented by 10
break; // break
case 4:// when the user rolls 4
cout<<" You rolled "<<dicevalue<<" "<<endl;// displays to user what number has been rolled
cout<<"Small prawn ";// displays what the user caught while fishing
total=0+7;// total is incremented by 7
break;// break
case 5:// when the user rolls 5
cout<<" You rolled "<<dicevalue<<" "<<endl; // displays to user what number has been rolled
cout<<"Shark ";// displays what the user caught while fishing
total=0+40;// total is incremented by 40
break; // break
case 6:// when 6 is rolled
cout<<" You rolled "<<dicevalue<< " "<<endl;// displays to user what number has been rolled
cout<<"Huge Catfish ";// displays what the user caught while fishing
total=0+30;// total is incremented by 30
break;// break
}
return total;// return total from number rolled
}
int getPoints() { // accessor function to get points earned from 1 round of the game
return total;
}// end of function
};// end of class
#endif // end of header file
Game.h
#ifndef GAME_H // Game header file declaration
#define GAME_H // Game header file declaration
#include "Points.h" // include Points header file
#include<iostream> // for cout,cin
using namespace std;// for cout,cin
class Game: public Points{ // Game class declaration; extended from Points class
// private member objects
private:
Dice dice; // dice is an object of the dice class
Points points; //points is an object of the points class
public: // public member functions
int playgame(int total){ // function playgame definition, passes total variable
// the playgame function calls methods from other functions to simulate game play
int choice, points_to_add; // variables choice and points to add are declared; data type int
points_to_add=0; // points to add is initialized to 0
int dicevalue = dice.rollDice(); // dicevalue variable is assigned to rollDice function; function is called to roll dice; returns dice value
total = total+points.getpoints(dicevalue); // variable total is assigned to itself plus object of getpoints function;
// when object of getpoints function is called, the die value is assigned to the points system in the case switch
// getpoints function passes value of die roll; will return the total points earned in that round of game play
// total is is now assigned to itself to the total value returned from the getpoints function
cout<<"You earned:"<<total<<" points in that round."<<endl; // displays isolated points earned in each round of game play
return total;// return variable total
}// end of function
};// end of class
#endif// end of header file
// GoFish.cpp
#include<iostream>// for cout,cin
#include<time.h>// for random function
#include<stdlib.h>// for random function
#include<string>// string library
#include "Points.h" // include header file
#include"Dice.h" // include header file
#include"Game.h" // include header file
using namespace std; // for cout, cin
int total; // variable total; data type int
int main() // main function
{
int choice; // variable choice; data type int; holds user's choice to continue game play
int mytotal=0, total, points_to_add; // variable mytotal; data type int; initialized to 0
// points to add variable has data type int
int total1=0; //variable total1; data type int; initialized to 0
bool play; // variable play; data type bool
Game game; // Game object game declared
cout<<"Welcome to Go Fish 2.0!"<<endl; // welcome message
cout<<"Would you like to play?"<<endl; // Prompts user to play
cout<<"1 to play, 0 to exit:"<<endl; // Provides user with input choices
cin>>choice; // user enters game play choice
while (choice == 1) // while user chooses to play game
{
play = true; // play boolean is set to true
break;// break
}
while (choice == 0) // while user chooses to end game
{
play = false; // play boolean is false
break; // break
}
while ((choice != 0) || (choice != 1)) { // while user entry is not valid
// display correct inpout choices again
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input
if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}
total=0; // variable total is initialized to 0
while (play == true) { // while play boolean is set to true; user wants to play game
total1 += game.playgame(total); // total1 variable keeps a running total of the game
//game.playgame(total) uses the game object to call the playgame function that passes the variable total
// when game.playgame(total) is called this funciton essentially mobilizes game play of die rolling and point accumulation
//each time this function is called (as many times as user wants to play game), the running total is incremented
cout<< "Do you want to play again? (0 for no, 1 for yes)"<<endl;// asks user if they want to play again
cin >> choice;// user enters choice
if (choice == 1) // if user enters 1
{
play = true; // play is assigned to true
}
else if (choice == 0) // if user enters 0
{
play = false; // play is assigned to false
}
else { // is input is invalid
cout <<"You entered invalid data. Please enter 1 if you want to play again or 0 if you dont."<<endl;// prompt user to enter valid data
}
}
if (play == false)// if play is false (user chose not to play)
{
cout<<"My Total game points are "<<total1<<endl; // displays user's total points from whole game
if (total1>100) // if game total greater than 100
cout<<"Awesome! You're a great fisher!"<<endl; // congratulate user
if (total1<100)// if game total is less than 100
cout<<"Good job, but play again so you can fish some more."<<endl;// chastise user
}
return 0;// end main function
}
Explanation / Answer
Here is the code for you. Rest of the code left as it is, I only modified the GoFish.cpp main() function, so that the loop works properly. If you have any further queries, just get back to me.
// GoFish.cpp
#include<iostream>// for cout,cin
#include<time.h>// for random function
#include<stdlib.h>// for random function
#include<string>// string library
#include "Points.h" // include header file
#include"Dice.h" // include header file
#include"Game.h" // include header file
using namespace std; // for cout, cin
int total; // variable total; data type int
int main() // main function
{
int choice; // variable choice; data type int; holds user's choice to continue game play
int mytotal=0, total, points_to_add; // variable mytotal; data type int; initialized to 0
// points to add variable has data type int
int total1=0; //variable total1; data type int; initialized to 0
bool play; // variable play; data type bool
Game game; // Game object game declared
cout<<"Welcome to Go Fish 2.0!"<<endl; // welcome message
cout<<"Would you like to play?"<<endl; // Prompts user to play
cout<<"1 to play, 0 to exit:"<<endl; // Provides user with input choices
cin>>choice; // user enters game play choice
if (choice == 1) // while user chooses to play game
play = true; // play boolean is set to true
else if (choice == 0) // while user chooses to end game
play = false; // play boolean is false
while ((choice != 0) && (choice != 1)) { // while user entry is not valid
// display correct inpout choices again
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input
if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}
total=0; // variable total is initialized to 0
while (play == true) { // while play boolean is set to true; user wants to play game
total1 += game.playgame(total); // total1 variable keeps a running total of the game
//game.playgame(total) uses the game object to call the playgame function that passes the variable total
// when game.playgame(total) is called this funciton essentially mobilizes game play of die rolling and point accumulation
//each time this function is called (as many times as user wants to play game), the running total is incremented
cout<< "Do you want to play again? (0 for no, 1 for yes)"<<endl;// asks user if they want to play again
cin >> choice;// user enters choice
if (choice == 1) // if user enters 1
{
play = true; // play is assigned to true
}
else if (choice == 0) // if user enters 0
{
play = false; // play is assigned to false
}
while ((choice != 0) && (choice != 1)) { // while user entry is not valid
// display correct inpout choices again
cout <<"You entered invalid data. Please enter the numerical value 1 if you want to play again or 0 if you dont."<<endl;
cin >> choice; // hopefully user enters valid input
if (choice == 1) { // if user chooses proper input after improper input
play = true;// play boolean is set to true
break;// break
}
}
}
if (play == false)// if play is false (user chose not to play)
{
cout<<"My Total game points are "<<total1<<endl; // displays user's total points from whole game
if (total1>100) // if game total greater than 100
cout<<"Awesome! You're a great fisher!"<<endl; // congratulate user
if (total1<100)// if game total is less than 100
cout<<"Good job, but play again so you can fish some more."<<endl;// chastise user
}
return 0;// end main function
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.