-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
505 lines (438 loc) · 9.68 KB
/
main.cpp
File metadata and controls
505 lines (438 loc) · 9.68 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include <ncurses.h>
#include <random>
#include <array>
#include <string>
#include <iostream>
#include <memory>
#include <chrono>
#include <string_view>
#include <algorithm>
// local namespace
namespace {
enum AppStatus {
MENU,
GAME,
INFO,
EXIT
};
enum GameStatus {
RUN,
PAUSE,
GAME_OVER
};
// In this style 'cause ncurse already has function 'UP'
enum Direction {
Up,
Right,
Down,
Left
};
typedef std::pair<unsigned short, unsigned short> coordinates; // first = x; second = y;
} // local namespace
/*
* RandomCoordinatesGenerator
*/
class RandomCoordinatesGenerator {
public:
RandomCoordinatesGenerator(unsigned short width, unsigned short height) noexcept :
_w_distribution(1, width - 1), _h_distribution(1, height - 1)
{
std::random_device random_device;
_random_generator = std::mt19937(random_device());
}
coordinates get() noexcept {
return {
_w_distribution(_random_generator),
_h_distribution(_random_generator)
};
};
private:
std::mt19937 _random_generator;
std::uniform_int_distribution<unsigned short> _w_distribution;
std::uniform_int_distribution<unsigned short> _h_distribution;
};
/*
* Snake
*/
class Snake {
public:
Snake(unsigned short init_x, unsigned short init_y, Direction direction) : _direction{direction} {
_body_parts.reserve(100);
_body_parts.emplace_back(init_x, init_y);
}
void setDirection(Direction direction) noexcept {
_direction = direction;
}
const Direction & getDirection() const {
return _direction;
}
void reset() noexcept {
_body_parts.resize(1);
}
void init(unsigned short init_x, unsigned short init_y) {
_body_parts.emplace_back(init_x, init_y);
}
void render() noexcept {
clear();
move_body();
draw_body();
}
coordinates getHead() const noexcept {
return _body_parts[0];
};
void grow_up() noexcept {
_will_be_grown = true;
}
bool is_part_of_body(const coordinates& coords) {
return std::any_of(_body_parts.cbegin(), _body_parts.cend(),
[&coords](const coordinates& part) {
return part == coords;
}
);
}
bool check_self_abuse() {
return std::any_of(_body_parts.cbegin() + 1, _body_parts.cend(),
[this](const coordinates& part) {
return part == _body_parts.front();
}
);
}
private:
void draw_body() {
std::for_each(_body_parts.begin(), _body_parts.end(),
[this](const std::pair<unsigned short, unsigned short>& part) {
mvprintw(part.second, part.first, _body_fill);
}
);
}
void move_body() {
coordinates last;
if (_will_be_grown) {
last = _body_parts.back();
}
for (auto it = _body_parts.rbegin(), end = _body_parts.rend() - 1; it != end; ++it) {
*it = *(it + 1);
}
if (_will_be_grown) {
_body_parts.emplace_back(last);
_will_be_grown = false;
}
switch (_direction) {
case Up:
_body_parts[0].second--;
break;
case Right:
_body_parts[0].first++;
break;
case Down:
_body_parts[0].second++;
break;
case Left:
_body_parts[0].first--;
break;
default:
break;
}
}
bool _will_be_grown{false};
std::vector<coordinates> _body_parts;
const char * _body_fill{"@"};
Direction _direction;
};
/*
* Screen
*/
class Screen {
public:
Screen(unsigned short & width, unsigned short & height) :
_width{width}, _height{height} {}
virtual ~Screen() = default;
virtual void render() noexcept = 0;
virtual void input_handler(int, AppStatus&) noexcept = 0;
protected:
unsigned short get_width() const noexcept {
return _width;
}
unsigned short get_height() const noexcept {
return _height;
}
void clear_once() noexcept {
if (!_was_cleaned) {
clear();
_was_cleaned = true;
}
}
void on_leave() noexcept {
_was_cleaned = false;
}
void print_on_center(std::string_view msg) const noexcept {
mvprintw(_height / 2, (_width / 2) - (msg.size() / 2), msg.data());
}
private:
unsigned short & _width;
unsigned short & _height;
bool _was_cleaned{false};
};
/*
* Game
*/
class Game : public Screen {
public:
Game(unsigned short &width, unsigned short &height) : Screen(width, height) {
_coords_generator = new RandomCoordinatesGenerator(width, height);
_snake = new Snake{10, 10, Right};
generate_food();
}
~Game() override {
delete _snake;
delete _coords_generator;
}
void render() noexcept override {
clear_once();
switch (_game_status) {
case RUN:
{
auto now = std::chrono::system_clock::now();
if ( std::chrono::duration_cast<std::chrono::milliseconds>(now - _previous_render).count() > _speed ) {
_previous_render = now;
_snake->render();
draw_food_trace();
draw_score();
draw_food();
if (check_food()) {
_score++;
_speed -= (_speed > 20) ? 5 : 0;
_snake->grow_up();
generate_food();
}
if (check_collision() || _snake->check_self_abuse()) {
_game_status = GAME_OVER;
}
}
break;
}
case PAUSE:
print_on_center("game paused, press p to unpause");
break;
case GAME_OVER:
print_on_center("GAME OVER. Press r to restart or q to quit in menu");
break;
default:
break;
}
}
void input_handler(int input, AppStatus& status) noexcept override {
switch (input) {
case KEY_UP:
if (_snake->getDirection() != Down)
_snake->setDirection(Up);
break;
case KEY_RIGHT:
if (_snake->getDirection() != Left)
_snake->setDirection(Right);
break;
case KEY_DOWN:
if (_snake->getDirection() != Up)
_snake->setDirection(Down);
break;
case KEY_LEFT:
if (_snake->getDirection() != Right)
_snake->setDirection(Left);
break;
case 'q':
on_leave();
status = MENU;
break;
case 'p':
_game_status = (_game_status == PAUSE) ? RUN : PAUSE;
break;
case 'r':
restart();
_game_status = PAUSE;
default:
break;
}
}
private:
bool check_collision() {
auto [pos_x, pos_y] = _snake->getHead();
return !(pos_x > 0 && pos_y > 0 && pos_x < get_width() && pos_y < get_height());
}
void generate_food() {
_food = _coords_generator->get();
while (_snake->is_part_of_body(_food)) {
_food = _coords_generator->get();
}
}
void draw_food() {
mvprintw(_food.second, _food.first, "$");
}
bool check_food() {
return _food == _snake->getHead();
}
void draw_score() {
mvprintw(1, get_width() / 2, "score %d", _score);
}
void draw_food_trace() {
mvprintw(1, 2, "x: %d", _food.first);
mvprintw(2, 2, "y: %d", _food.second);
}
void restart() noexcept {
_snake->reset();
}
unsigned short _speed{150};
unsigned short _score{0};
std::chrono::system_clock::time_point _previous_render;
coordinates _food;
RandomCoordinatesGenerator* _coords_generator;
Snake* _snake;
GameStatus _game_status{RUN};
};
/*
* MENU
*/
class Menu : public Screen {
public:
Menu(unsigned short &width, unsigned short &height) : Screen(width, height) {}
void render() noexcept override {
clear_once();
for (int i = 0; i < _menu_options.size(); ++i) {
if (i == _current_option) {
attron(COLOR_PAIR(1));
mvprintw(get_height() / 2 + i, get_width() / 2, _menu_options[i].c_str());
attroff(COLOR_PAIR(1));
} else {
mvprintw(get_height() / 2 + i, get_width() / 2, _menu_options[i].c_str());
}
}
}
void input_handler(int input, AppStatus& status) noexcept override {
switch (input) {
case KEY_UP:
if (_current_option != 0) {
_current_option--;
} else {
_current_option = _menu_options.size() - 1;
}
break;
case KEY_DOWN:
if (_current_option != _menu_options.size() - 1) {
_current_option++;
} else {
_current_option = 0;
}
break;
case '\n':
on_leave();
status = static_cast<AppStatus>(_current_option + 1);
default:
break;
}
}
private:
std::array<std::string, 3> _menu_options{ {"Start", "Info", "Exit"} };
unsigned short _current_option{0};
};
/*
* Info
*/
class Info : public Screen {
public:
Info(unsigned short &width, unsigned short &height) : Screen(width, height) {}
void render() noexcept override {
clear_once();
print_on_center("print q to back in menu");
}
void input_handler(int input, AppStatus& status) noexcept override {
switch (input) {
case 'q':
on_leave();
status = MENU;
break;
default:
break;
}
}
};
class SnakeGame {
public:
SnakeGame() {
// Init screen
initscr();
// Get current size of terminal window
getmaxyx(stdscr, _height, _width);
_menu = new Menu(_width, _height);
_info = new Info(_width, _height);
_game = new Game(_width, _height);
}
~SnakeGame() {
delete _game;
delete _info;
delete _menu;
}
void start() {
init();
render();
}
private:
void init() {
// Disable cursor display
curs_set(0);
// Make available arrows
keypad(stdscr, true);
// Disable buffering
noecho();
// Input in no-blocking regime
nodelay(stdscr, true);
// Make available colors
start_color();
// Init own color scheme
init_pair(1, COLOR_CYAN, COLOR_BLUE);
}
void render() {
while (_status != EXIT) {
box(stdscr, 0, 0);
refresh();
if ( (_input = getch()) == ERR ) {
switch (_status) {
case MENU:
_menu->render();
break;
case INFO:
_info->render();
break;
case GAME:
_game->render();
default:
break;
}
} else {
switch (_status) {
case MENU:
_menu->input_handler(_input, _status);
break;
case INFO:
_info->input_handler(_input, _status);
break;
case GAME:
_game->input_handler(_input, _status);
break;
default:
break;
}
}
}
endwin();
}
Menu* _menu;
Info* _info;
Game* _game;
AppStatus _status{MENU};
unsigned short _height{}, _width{};
int _input{};
};
int main() {
auto game = std::make_unique<SnakeGame>();
game->start();
return 0;
}