-
Notifications
You must be signed in to change notification settings - Fork 5
/
virtual-pet.cpp
117 lines (114 loc) · 2.27 KB
/
virtual-pet.cpp
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Pet
{
private:
string name;
int hunger;
int boredom;
public:
Pet(string petName = "Puppy")
{
name = petName;
hunger = 0;
boredom = 0;
}
void play()
{
boredom -= 5;
if (boredom < 0)
{
boredom = 0;
}
hunger += 1;
}
void feed()
{
hunger -= 3;
if (hunger < 0)
{
hunger = 0;
}
}
void checkStatus()
{
if (hunger >= 10)
{
cout << name << " is hungry!" << endl;
}
if (boredom >= 10)
{
cout << name << " is bored!" << endl;
}
if (hunger == 0 && boredom == 0)
{
cout << name << " is happy and healthy!" << endl;
}
}
bool isAlive()
{
return hunger < 10 && boredom < 10;
}
string getName()
{
return name;
}
int getHunger()
{
return hunger;
}
int getBoredom()
{
return boredom;
}
};
int main()
{
srand(time(NULL));
Pet myPet;
cout << "Welcome to Virtual Pet Game!" << endl;
cout << "What would you like to name your pet? ";
string name;
cin >> name;
myPet = Pet(name);
while (myPet.isAlive())
{
myPet.checkStatus();
cout << "What would you like to do? (1 = Play, 2 = Feed, 3 = Quit) ";
int choice;
cin >> choice;
switch (choice)
{
case 1:
myPet.play();
break;
case 2:
myPet.feed();
break;
case 3:
exit(0);
default:
cout << "Invalid choice. Try again." << endl;
break;
}
int randomEvent = rand() % 3;
if (randomEvent == 0)
{
myPet.feed();
cout << "Your pet found some food to eat!" << endl;
}
else if (randomEvent == 1)
{
myPet.play();
cout << "Your pet found a toy to play with!" << endl;
}
else
{
cout << "Your pet is resting." << endl;
}
}
cout << "Game over. " << myPet.getName() << " has died." << endl;
return 0;
}