-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimone.cpp
More file actions
168 lines (137 loc) · 2.91 KB
/
simone.cpp
File metadata and controls
168 lines (137 loc) · 2.91 KB
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
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <algorithm>
#include <cstdlib>
#include <ctime>
const int WDD = 10;
const int HDD = 10;
class Human
{
public:
int x;
int y;
int hngr;
int power;
bool isAlive = true;
Human()
{
x = rand() % WDD;
y = rand() % HDD;
hngr = 40;
power = 100;
}
void Update(std::vector<std::vector<char>>& grid)
{
hngr += 2;
power -= 1;
if(hngr > 80)
{
Eat(grid);
}
if(power < 30)
{
Rest();
}
else
{
Wander();
}
hngr = std::clamp(hngr, 0, 120);
power = std::clamp(power, 0, 100);
if(hngr >= 120 || power <= 0)
{
isAlive = false;
}
}
void Eat(std::vector<std::vector<char>>& grid)
{
if(grid[y][x] == 'F')
{
std::cout<<"Human eats food.\n";
hngr -= 40;
grid[y][x] = ' ';
}
else
{
std::cout<<"Hungry... searching for food.\n";
Move();
}
}
void Rest()
{
std::cout<<"Human is resting.\n";
power += 15;
}
void Wander()
{
std::cout << "Human is wandering...\n";
Move();
}
void Move()
{
int dir = rand() % 4;
if(dir == 0 && y > 0)
{
y--;
}
else if(dir == 1 && y < HDD - 1)
{
y++;
}
else if(dir == 2 && x > 0)
{
x--;
}
else if(dir == 3 && x < WDD - 1)
{
x++;
}
}
};
void Draw(const std::vector<std::vector<char>>& grid, const Human& h)
{
for(int i = 0; i < HDD; i++)
{
for(int j = 0; j < WDD; j++)
{
if(i == h.y && j == h.x)
{
std::cout<<'H'<< ' ';
}
else
{
std::cout<<grid[i][j]<< ' ';
}
}
std::cout << "\n";
}
}
int main()
{
srand(time(0));
std::vector<std::vector<char>> grid(HDD,std::vector<char>(WDD, ' '));
for(int i = 0; i < 10; i++)
{
int fx = rand() % WDD;
int fy = rand() % HDD;
grid[fy][fx] = 'F';
}
Human h;
while(true)
{
h.Update(grid);
Draw(grid, h);
std::cout<<"\nHunger: "<< h.hngr;
std::cout<<"\nEnergy: "<< h.power;
std::cout<<"\nAlive: "<< (h.isAlive ? "Yes" : "No") << "\n\n";
if(!h.isAlive)
{
std::cout<<"Human is dead.\n";
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(550));
}
return 0;
}