1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
//Critter Caretaker
//Simulates caring for a virtual pet
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Critter;
class CritterFarm
{
public:
CritterFarm();
~CritterFarm();
Critter critters[5];
private:
};
//class definition -- defines a new type, Critter
class Critter
{
public:
Critter(int hunger = 0, int boredom = 0);
void Talk(CritterFarm & crt); //displays mood
void Eat(int food = 5); //reduces hunger level
void Play(int fun = 5); //reduces boredom level
void PerformTrick(); //performs a random trick, if happy
friend CritterFarm;
private:
enum Mood { HAPPY, OKAY = 5, FRUSTRATED = 10, MAD = 15 }; //mood levels
static const int NUM_TRICKS = 3; //number of tricks
static const string TRICKS[NUM_TRICKS]; //tricks
int m_Hunger; //hunger level
int m_Boredom; //boredom level
string GetMood() const; //returns mood
void PassTime(int time = 1); //increases hunger, boredom levels
};
//tricks
const string Critter::TRICKS[NUM_TRICKS] = { "roll over",
"jump",
"do a backflip" };
CritterFarm::CritterFarm()
{
}
CritterFarm::CritterFarm()
{
}
//constructor
Critter::Critter(int hunger, int boredom) :
m_Hunger(hunger),
m_Boredom(boredom)
{}
//returns mood
string Critter::GetMood() const
{
string mood;
if (m_Hunger + m_Boredom > MAD)
mood = "mad";
else if (m_Hunger + m_Boredom > FRUSTRATED)
mood = "frustrated";
else if (m_Hunger + m_Boredom > OKAY)
mood = "okay";
else
mood = "happy";
return mood;
}
//increases hunger and boredom levels
void Critter::PassTime(int time)
{
m_Hunger += time;
m_Boredom += time;
}
//displays mood
void Critter::Talk(CritterFarm & crt)
{
cout << "I'm a critter and I feel " << GetMood() << "." << endl;
this->PassTime();
}
//reduces hunger level
void Critter::Eat(int food)
{
cout << "Brruppp." << endl;
m_Hunger -= food;
if (m_Hunger < 0)
{
m_Hunger = 0;
}
PassTime();
}
//reduces boredom level
void Critter::Play(int fun)
{
cout << "Wheee!" << endl;
m_Boredom -= fun;
if (m_Boredom < 0)
{
m_Boredom = 0;
}
PassTime();
}
//perform a random trick, if happy
void Critter::PerformTrick()
{
//if not happy, no trick
if (GetMood() != "happy")
{
cout << "I don't feel like doing a trick." << endl;
}
//perform a trick
else
{
int choice = (rand() % NUM_TRICKS); //random index number
string trick = TRICKS[choice]; //trick to perform
cout << "I " << trick << "." << endl;
}
PassTime();
}
int main()
{
srand(static_cast<unsigned int>(time(0)));
CritterFarm crit;
int choice;
do
{
cout << endl << "Critter Caretaker" << endl;
cout << "-----------------" << endl << endl;
cout << "0 - Quit" << endl;
cout << "1 - Listen to your critter" << endl;
cout << "2 - Feed your critter" << endl;
cout << "3 - Play with your critter" << endl;
cout << "4 - Ask your critter to perform a trick." << endl << endl;
cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 0:
cout << "Good-bye." << endl;
break;
case 1:
crit.Talk();
break;
case 2:
crit.Eat();
break;
case 3:
crit.Play();
break;
case 4:
crit.PerformTrick();
break;
default:
cout << endl << "Sorry, but " << choice;
cout << " isn't a valid choice." << endl;
}
} while (choice != 0);
return 0;
}
|