-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
353 lines (304 loc) · 7.23 KB
/
Source.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* ==========================================================================
* /File: Source.cpp /
* /Brief: Realization of the game /
* /Creator: Leonid Artemev | Leartiz /
* /Date: 2018 /
* ==========================================================================
*/
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <clocale>
#include <Windows.h>
#define WIDTH 24 // - Width map
#define HEIGHT 18 // | Height map
// Start
int difficult();
void instruction();
void initialization_map(char(&map)[HEIGHT][WIDTH]);
// Output
void out_map_console(int *tail, const int &score, const int &amount,
char(&map)[HEIGHT][WIDTH], const int& xfoo, const int& yfoo);
void out_array(int *tail, int const& amount);
// Way
enum class Control { STOP = 0, W, S, A, D };
// Snake
void motion(int *tail, Control& step);
void route(Control& step);
void rewrite_array(int *tail, const int& amount,
char(&map)[HEIGHT][WIDTH], const int& xfoo, const int& yfoo);
void knock_wall(int *tail, bool& end);
void eat_food(int *tail, int & score, int &amount, int& xfoo, int& yfoo);
void ad_tail(int *tail, const int& amount, const int& xbuf, const int& ybuf);
void tail_motion(int *tail, const int& amount);
// System
void cls();
void pause_end(bool& end);
int main()
{
// Console window
HANDLE out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD crd;
crd.X = WIDTH + 25;
crd.Y = HEIGHT + 15;
SMALL_RECT src = { 0, 0, crd.X - 1, crd.Y - 1 };
SetConsoleWindowInfo(out_handle, true, &src);
SetConsoleScreenBufferSize(out_handle, crd);
SetConsoleTitle("Snake 0.1"); // Name
char map[HEIGHT][WIDTH]; // Map
Control step = Control::STOP;
bool end = false;
int score = 0;
int tail[WIDTH * HEIGHT]; // Body
int amount = 0; // + To body
// Head
tail[0] = WIDTH / 2;
tail[1] = HEIGHT / 2;
// Food Coordinates
int xfoo = (rand() % (WIDTH - 2)) + 1;
int yfoo = (rand() % (HEIGHT - 2)) + 1;
// Output of the instruction
instruction();
// Filling in the map
initialization_map(map);
// Choice of difficulty level
// Difficulty level
int diff_lvl = difficult();
while (!end)
{
out_map_console(tail, score, amount, map, xfoo, yfoo); // Output
motion(tail, step); // Constant motion
route(step);
pause_end(end); // ...
knock_wall(tail, end);
eat_food(tail, score, amount, xfoo, yfoo);
rewrite_array(tail, amount, map, xfoo, yfoo);
tail_motion(tail, amount);
Sleep(diff_lvl);
cls();
}
_getch();
return 0;
}
void instruction()
{
printf("\"Instruction\" \n\n");
printf("W - UP \n");
printf("S - DOWN \n");
printf("A - LEFT \n");
printf("D - RIGHT \n");
printf("To pause use ESC \n");
printf("\n\n");
_getch();
system("CLS");
}
int difficult()
{
int diff_lvl = 0;
std::cout << "Difficulty level: \n\n";
std::cout << "1. Very difficult \n";
std::cout << "2. Difficult \n";
std::cout << "3. Average \n";
std::cout << "4. Easy \n";
std::cin >> diff_lvl;
diff_lvl = diff_lvl * 20;
system("CLS");
return diff_lvl;
}
void initialization_map(char(&map)[HEIGHT][WIDTH])
{
for (int i = 0; i < HEIGHT; ++i)
{
for (int j = 0; j < WIDTH; ++j)
{
if (i == 0 || j == 0 || i == HEIGHT - 1 || j == WIDTH - 1)
map[i][j] = '#';
else
map[i][j] = ' ';
}
}
}
void out_map_console(int *tail, const int &score, const int &amount,
char(&map)[HEIGHT][WIDTH], const int& xfoo, const int& yfoo)
{
for (int i = 0; i < HEIGHT; ++i)
{
for (int j = 0; j < WIDTH; ++j)
{
std::cout << map[i][j];
}
std::cout << std::endl;
}
std::cout << std::endl << "Score = " << score << std::endl;
std::cout << std::endl << "Amount = " << amount << std::endl;
std::cout << "Head: \n";
std::cout << "X = \b\b" << tail[0] << std::endl;
std::cout << "Y = \b\b" << tail[1] << std::endl;;
std::cout << "Food: \n";
std::cout << "X = \b\b" << xfoo << std::endl;
std::cout << "Y = \b\b" << yfoo << std::endl;
}
void route(Control &step)
{
if (_kbhit())
{
int key = _getch();
switch (key)
{
case 'W': case 'w': case 72:
step = Control::W;
break;
case 'S': case 's': case 80:
step = Control::S;
break;
case 'D': case 'd': case 77:
step = Control::D;
break;
case 'A': case 'a': case 75:
step = Control::A;
break;
case 27:
_ungetch(key);
return;
break;
}
}
}
void pause_end(bool& end)
{
if (_kbhit())
{
int key = _getch();
switch (key)
{
case 27:
std::cout << "\n --- PAUSE --- \n";
std::cout << "Continue = 1 \n";
std::cout << "Exit = any \n";
std::cin >> key;
if (key != 1)
end = true; // Exit / End
system("CLS");
break;
default:
break;
}
}
}
void motion(int *tail, Control & step)
{
switch (step)
{
case Control::W:
--tail[1];
break;
case Control::S:
++tail[1];
break;
case Control::A:
--tail[0];
break;
case Control::D:
++tail[0];
break;
}
}
void rewrite_array(int *tail, const int& amount,
char(&map)[HEIGHT][WIDTH], const int& xfoo, const int& yfoo)
{
// Editing without borders
for (int i = 1; i < HEIGHT - 1; ++i)
{
for (int j = 1; j < WIDTH - 1; ++j)
{
if (i == yfoo && j == xfoo)
{
map[i][j] = 'F';
}
else if (i == tail[1] && j == tail[0])
{
map[i][j] = '%';
}
else
{
map[i][j] = ' ';
if (amount > 0)
{
for (int o = 2; o < amount * 2 + 1; o += 2)
{
map[tail[o + 1]][tail[o]] = 'o';
}
}
}
}
}
}
void knock_wall(int *tail, bool& end)
{
// Without impact
//if (tail[0] == 0) ++tail[0];
//if (tail[0] == WIDTH - 1) --tail[0];
//if (tail[1] == 0) ++tail[1];
//if (tail[1] == HEIGHT - 1) --tail[1];
// Through the wall
if (tail[0] == 0) tail[0] = WIDTH - 2;
if (tail[0] == WIDTH - 1) tail[0] = 1;
if (tail[1] == 0) tail[1] = HEIGHT - 2;
if (tail[1] == HEIGHT - 1) tail[1] = 1;
// End of the game
//if (tail[0] == 0) end = true;
//if (tail[0] == WIDTH - 1) end = true;
//if (tail[1] == 0) end = true;
//if (tail[1] == HEIGHT - 1) end = true;
}
void eat_food(int *tail, int & score, int &amount, int& xfoo, int& yfoo)
{
int xbuf = 0;
int ybuf = 0;
if (tail[0] == xfoo && tail[1] == yfoo)
{
amount++;
xbuf = xfoo;
ybuf = yfoo;
xfoo = (rand() % (WIDTH - 2)) + 1;
yfoo = (rand() % (HEIGHT - 2)) + 1;
score += 5;
ad_tail(tail, amount, xbuf, ybuf);
}
}
// A record of coordinates where a fruit is eaten, into a one-dimensional array
void ad_tail(int *tail, const int& amount, const int& xbuf, const int& ybuf)
{
for (int i = amount * 2 + 1; i > 1; i -= 2)
{
tail[i] = tail[i - 2];
tail[i - 1] = tail[i - 3];
}
tail[2] = xbuf;
tail[3] = ybuf;
}
void out_array(int *tail, int const& amount)
{
for (int i = 0; i < amount * 2 + 1; i += 2)
{
std::cout << "x = " << tail[i] << " y = " << tail[i + 1];
std::cout << std::endl;
}
}
void tail_motion(int *tail, const int& amount)
{
for (int i = amount * 2 + 1; i > 1; i -= 2)
{
tail[i] = tail[i - 2];
tail[i - 1] = tail[i - 3];
}
}
void cls()
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
COORD cd;
cd.X = 0;
cd.Y = 0;
SetConsoleCursorPosition(hd, cd);
}