-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
242 lines (199 loc) · 5.61 KB
/
main.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <iomanip>
#include <random>
class Snake{
private:
int height;
int width;
int snakeBodyLenght;
char** mapMatrix;
int snakeHead[2];
char direction;
int** snakeBody;
int food[2] = {0,0};
char mapChar = '0';
char headChar = 'H';
char bodyChar = 'B';
char foodChar = 'f';
public:
Snake(int heightCr, int widthCr) :
height(heightCr),
width(widthCr),
snakeBodyLenght(1)
{
int X = heightCr / 2;
int Y = widthCr / 2;
mapMatrix = createMap(height, width);
snakeHead[0] = X;
snakeHead[1] = Y;
direction = 'd';
snakeBody = new int*[snakeBodyLenght];
snakeBody[0] = new int[2];
snakeBody[0][0] = X;
snakeBody[0][1] = Y - 1;
food[0] = X;
food[1] = width - 2;
}
~Snake(){
std::cout << "Destroyed" << std::endl;
cleanUp();
}
private:
char** createMap(int heightC, int widthC);
public:
void showMap();
void snakeMove(char direction);
void reset();
void generateFood();
void cleanUp();
};
char** Snake::createMap(int heightC, int widthC){
char** map = new char*[heightC];
for (int i = 0; i < heightC; i++){
map[i] = new char[widthC];
for (int j = 0; j < widthC; j++){
map[i][j] = mapChar;
}
}
return map;
}
void Snake::showMap(){
for (int i = 0; i < snakeBodyLenght; i++){
mapMatrix[snakeBody[i][0]][snakeBody[i][1]] = bodyChar;
}
mapMatrix[snakeHead[0]][snakeHead[1]] = headChar;
mapMatrix[food[0]][food[1]] = foodChar;
system("clear");
for(int i=0; i < height; i++){
for(int j=0; j < width; j++){
std::cout << std::setw(2) << mapMatrix[i][j];
}
std::cout << std::endl;
}
for (int i = 0; i < snakeBodyLenght; i++){
mapMatrix[snakeBody[i][0]][snakeBody[i][1]] = mapChar;
}
mapMatrix[snakeHead[0]][snakeHead[1]] = mapChar;
}
void Snake::snakeMove(char directionAux){
int auxHead[2] = {snakeHead[0], snakeHead[1]};
int oldHead[2] = {snakeHead[0], snakeHead[1]};
switch (directionAux)
{
case 'd':
auxHead[1]++;
direction = directionAux;
break;
case 's':
auxHead[0]++;
direction = directionAux;
break;
case 'a':
auxHead[1]--;
direction = directionAux;
break;
case 'w':
auxHead[0]--;
direction = directionAux;
break;
default:
snakeMove(direction);
return;
}
snakeHead[0] = auxHead[0];
snakeHead[1] = auxHead[1];
bool grow;
grow = (auxHead[0] == food[0] and auxHead[1] == food[1]);
if(grow){
int** auxBody = new int*[snakeBodyLenght];
for(int i=0; i < snakeBodyLenght; i++){
auxBody[i] = new int[2];
auxBody[i][0] = snakeBody[i][0];
auxBody[i][1] = snakeBody[i][1];
}
delete snakeBody;
snakeBodyLenght = snakeBodyLenght + 1;
snakeBody = new int*[snakeBodyLenght];
snakeBody[0] = new int[2];
snakeBody[0][0] = oldHead[0];
snakeBody[0][1] = oldHead[1];
for(int i=1; i < snakeBodyLenght; i++){
snakeBody[i] = new int[2];
snakeBody[i][0] = auxBody[i - 1][0];
snakeBody[i][1] = auxBody[i - 1][1];
delete auxBody[i - 1];
}
delete auxBody;
generateFood();
}else {
for(int i = snakeBodyLenght - 1; i > 0; i--){
snakeBody[i][0] = snakeBody[i-1][0];
snakeBody[i][1] = snakeBody[i-1][1];
}
snakeBody[0][0] = oldHead[0];
snakeBody[0][1] = oldHead[1];
}
for(int i = snakeBodyLenght -1; i >= 0; i--){
if ((snakeHead[0] == snakeBody[i][0] and snakeHead[1] == snakeBody[i][1]) or (snakeHead[0] < 0) or (snakeHead[0] >= height) or (snakeHead[1] < 0) or (snakeHead[1] >= width)){
std::cout << "You Lose, Bastard!!" << std::endl;
char hold;
std::cin >> hold;
reset();
return;
}
}
}
void Snake::reset(){
int X = height / 2;
int Y = width / 2;
snakeHead[0] = X;
snakeHead[1] = Y;
snakeBodyLenght = 1;
snakeBody = new int*[snakeBodyLenght];
snakeBody[0] = new int[2];
snakeBody[0][0] = X;
snakeBody[0][1] = Y - 1;
mapMatrix[food[0]][food[1]] = mapChar;
food[0] = X;
food[1] = width - 2;
}
void Snake::generateFood(){
std::random_device generator;
std::uniform_int_distribution<int> distributionH(0,height - 1);
std::uniform_int_distribution<int> distributionW(0,width - 1);
food[0] = distributionH(generator);
food[1] = distributionW(generator);
if (snakeBodyLenght > (height * width) - 2){
std::cout << "You are free now!"<< std::endl;
char hold;
std::cin >> hold;
reset();
return;
}
for(int i = snakeBodyLenght -1; i >= 0; i--){
if ((food[0] == snakeBody[i][0] and food[1] == snakeBody[i][1]) or (food[0] == snakeHead[0] and food[1] == snakeHead[1])){
generateFood();
}
}
}
void Snake::cleanUp(){
for (int i = 0; i < height; i++){
delete mapMatrix[i];
}
delete mapMatrix;
for (int i = 0; i < snakeBodyLenght; i++){
delete snakeBody[i];
}
delete snakeBody;
}
int main(){
char movemento;
Snake teste(4, 4);
teste.showMap();
while(true){
std::cin >> movemento;
teste.snakeMove(movemento);
teste.showMap();
}
teste.cleanUp();
}