and here is the pet code you need For this assignment, you will be modifying the
ID: 3703331 • Letter: A
Question
and here is the pet code you need
For this assignment, you will be modifying the Pet Program we worked with earlier. You should work from the version you created in assignment 7. If you didn't complete Assignment 7 you may work from the original Pet program, but make that clear in the comments at the beginning of your program. For either approach it is recommended that you begin with a copy of the code, so you have the old version to go back to The general idea for this program is that the player has four pets, stored as an array, rather than just one pet that they can interact with. This will require making the following additions to the progran: Create an array of four pet objects Change the ending condition of the main loop so that the loop only ends if all four pets die. This will probably require creating an extra variable to keep track of the number of pets that are still alive (or that have died) Each loop of the program should begin by giving the player a choice of pets (by name) to interact with. For example Which pet do you want to interact with? 1) Bob 2) Sally 3) Rover 4) Tiddles The player then enters a number (1-4) and the program moves onto the regular interaction menu (play, feed, etc.). Note that pets that have died should not be included in the list .When the player interacts with a pet that interaction should only effect that pet. For example, if the player chooses to feed Bob then only Bob should get less hungry However, the random events may occur to other pets. So, even though the player fed Bob Sally may have had a random event occur to herExplanation / Answer
#include<iostream>
#include<string>
using namespace std;
// declaration of the pet class
class pet{
private:
int hunger; // private data member
string name; // private data member
int happy; // private data member
int id;
public:
pet(); // constructor
void play(); // public member function
void feed(); // public member function
void print(); // public member function
int check_health(); // public member function
};
int main()
{
pet pets[4];
int choice;
int alive=4;
do{
cout<<" Which pet do you want to interact with? ";
int arr_mapper[5];
int display_id=1;
for(int i=0;i<4;i++)
if(pets[i].health_check()!=1)
{
arr_mapper[display_id]=i;
cout<<display_id++<<" "<<pets[i].name<<endl;
}
int pet_choice;
cin>>pet_choice;
int original_pet=arr_mapper[pet_choice];
cout<<pets[original_pet].print();
cout << "What would you like to do with your pet? ";
cout << " Play (1) Feed (2) Exit (0) ";
cin >> choice;
switch(choice){
case 1:
pets[original_pet].play();
break;
case 2:
pets[original_pet].feed();
break;
}
alive=0;
for(int i=0;i<4;i++)
if(pets[i].check_health()!=1)
alive++;
}while(alive!=0);
cin.ignore();
cout << "Press enter to exit." << endl;
cin.ignore();
return 0;
}
/* Constructor, creates a new pet with starting values. */
pet::pet(){
static int id_stat=1;
id=id_stat++;
hunger = 50;
happy = 50;
cout << "Pet No: "<<id<<" name? (One word) ";
cin >> name;
}
/* Member function play(), allows playing with a pet. */
void pet::play(){
int choice = 0;
cout << "What should we play? ";
cout << " Fetch (1) Roll over (2) ";
cin >> choice;
switch(choice){
case(1):
happy += 10;
hunger += 5;
break;
case(2):
happy += 5;
hunger += 1;
break;
default:
cout << "Not a valid choice." << endl;
}
}
/* Member function feed(), allows the user to feed a pet. */
void pet::feed(){
cout << " MMM, Yummy! ";
hunger -= 5;
}
/* Member function print(), prints information about a pet. */
void pet::print(){
cout << " Your pet " << name << " is ";
cout << "Happy: " << happy << endl;
cout << "Hungry: " << hunger << endl;
}
/* Member function check_health(), checks the health of a pet. */
int pet::check_health(){
if(hunger >= 100){
cout << " Your pet has starved. ";
return 1;
}
if(happy <= 0){
cout << " Your pet has died of a broken heart. ";
return 1;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.