-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tamagachi.cpp
141 lines (131 loc) · 3.36 KB
/
Tamagachi.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <cstdlib>
#include <iostream>
#include <string>
#include <time.h>
#include "Tamagachi.h"
using namespace std;
Tamagachi::Tamagachi(string n):
name{n}
{}
bool Tamagachi::isAlive() {
return _isAlive();
}
void Tamagachi::draw() {
_drawCharacter();
_printStats();
_promptUser();
}
bool Tamagachi::heartbeat() {
if (_isAlive()) {
mood = moodDuration <= 0
? _getRandInt(10)
: mood;
moodDuration = moodDuration <= 0
? _getRandInt(10)
: moodDuration;
if (mood > 9) {
sleeping = false;
thought = name + " wants to play! Give it a toy:\n";
_wantsSomething = true;
} else if (mood < 9 && mood >= 7) {
sleeping = false;
thought = name + " is indifferent right now.";
sleeping = false;
} else if (mood < 7 && mood > 4) {
sleeping = false;
thought = name + " is Hungry! What do you want to feed it?\n";
_wantsSomething = true;
} else {
if (!sleeping) {
thought = name + " fell asleep. ZzZzz";
} else {
thought = name + " sleep, gaining hp...";
}
sleeping = true;
hp++;
}
age++;
hp--;
moodDuration--;
return true;
}
return false;
}
bool Tamagachi::_promptUser() {
if (_wantsSomething) {
_wantsSomething = false;
cout << "Enter input:\n";
string answer;
cin >> answer;
int reaction = (rand() % (2 - 1 + 1)) + 1;
bool likesIt = reaction > 0;
string innerMessage{};
string endMessage{};
if (likesIt) {
innerMessage = " likes ";
endMessage = " +10 HP!";
hp += 10;
} else {
innerMessage = " hates ";
endMessage = " -10HP!";
hp -= 10;
}
thought = name + innerMessage + answer + endMessage + "\n";
moodDuration = 0;
_drawCharacter();
_printStats();
return likesIt;
}
return false;
}
bool Tamagachi::_isAlive() {
return hp > 0;
}
void Tamagachi::_drawCharacter() {
bool tickTock = age % 2 == 0;
string zzz = sleeping ? " zZzZzz..." : "";
string ears = " /\\___ \\" + zzz;
string happyFace = " ^ U ^ ";
string emotionlessFace = " - _ - ";
string face = hp > 90 ? happyFace : emotionlessFace;
string leftArm = "/";
string rightArm = "\\";
string body = leftArm + "(" + face + ")" + rightArm;
string standing = " L L";
string crouching = " > >";
string legs = tickTock ? standing : crouching;
if (!_isAlive()) {
face = " x n x ";
legs = " < >";
} else if (sleeping) {
face = tickTock ? " - . - " : " - _ - ";
leftArm = ",";
rightArm = ",";
legs = " J J";
} else if (_isHungry) {
face = tickTock ? " * O * " : " . o . ";
leftArm = "\\";
rightArm = "/";
legs = " | \\";
}
string drawing = "\n" + ears + "\n" + body + "\n" + legs + "\n";
cout << drawing << endl;
}
void Tamagachi::_printStats() {
string sleepMsg = sleeping ? "Yes" : "No";
if (!_isAlive()) {
cout << "Your tamagachi has died!";
}
cout << thought.c_str() << endl;
cout << "----------------";
cout << "Mood: " << to_string(mood) << endl;
cout << "Mood Duration: " << to_string(moodDuration) << endl;
cout << "Name: " << name.c_str() << endl;
cout << "HP: " << to_string(hp) << endl;
cout << "Age: " << to_string(age) << endl;
cout << "Asleep: " << sleepMsg << endl;
}
int Tamagachi::_getRandInt(int max = 100) {
srand(time(NULL));
return rand() % (max + 1);
}