-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.cpp
371 lines (325 loc) · 9.72 KB
/
snake.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <thread>
#include <ncurses.h>
#include <panel.h>
int myrandom (int i) { return std::rand()%i;}
class ColorCode {
public:
static int BLACK_ON_GREEN;
static int BLACK_ON_RED;
static int BLACK_ON_BLUE;
static int BLACK_ON_BLACK;
static void initialize_all() {
init_pair(BLACK_ON_GREEN, COLOR_BLACK, COLOR_GREEN);
init_pair(BLACK_ON_RED, COLOR_BLACK, COLOR_RED);
init_pair(BLACK_ON_BLUE, COLOR_BLACK, COLOR_BLUE);
init_pair(BLACK_ON_BLACK, COLOR_BLACK, COLOR_BLACK);
}
};
int ColorCode::BLACK_ON_BLACK = 1;
int ColorCode::BLACK_ON_RED = 2;
int ColorCode::BLACK_ON_BLUE = 4;
int ColorCode::BLACK_ON_GREEN = 5;
class Coordinates {
public:
int x, y;
Coordinates() {}
Coordinates(int x_, int y_) : x(x_), y(y_) {}
};
void print_blank(Coordinates c) {
mvprintw(c.x, c.y, "%s", " ");
}
class Canvas {
WINDOW *window;
public:
unsigned char available_blue_coordinates;
unsigned int score;
int maxx, maxy, miny, minx;
Coordinates snake_origin;
std::vector<Coordinates> coordinates;
std::vector<Coordinates> potential_blue_coordinates;
std::vector<Coordinates> blue_coordinates;
std::vector<Coordinates> red_coordinates;
Canvas() {}
Canvas(int y, int x) {
this->available_blue_coordinates = 5;
this->score = 0;
this->minx = 50;
this->miny = 10;
this->maxx = x - 50;
this->maxy = y - 10;
window = newwin(
this->maxy - this->miny,
this->maxx - this->minx,
this->miny,
this->minx);
box(window, 0, 0);
new_panel(window);
update_panels();
doupdate();
wrefresh(window);
refresh();
}
void print_score() {
mvprintw(this->miny - 1, this->minx + 1, "%s", "Score: ");
mvprintw(this->miny - 1, this->minx + 10, "%d", Canvas::score);
}
void generate_coordinates() {
for (int j = this->miny + 1; j < this->maxy - 1; j ++) {
for (int i = this->minx + 1; i < this->maxx - 1; i ++) {
Coordinates c(j, i);
this->coordinates.push_back(c);
}
}
std::srand ( unsigned ( std::time(0) ) );
std::random_shuffle(this->coordinates.begin(), this->coordinates.end(), myrandom);
std::vector<Coordinates> potential_blue_coordinates(
this->coordinates.begin() + 11,
this->coordinates.end() - 1);
this->potential_blue_coordinates = potential_blue_coordinates;
this->snake_origin = this->coordinates[this->coordinates.size() - 1];
}
void print_black_coordinates() {
attron(COLOR_PAIR(ColorCode::BLACK_ON_BLACK));
for (int i = 11; i < this->coordinates.size(); i ++) {
print_blank(this->coordinates[i]);
}
attroff(COLOR_PAIR(ColorCode::BLACK_ON_BLACK));
}
void print_red_coordinates() {
attron(COLOR_PAIR(ColorCode::BLACK_ON_RED));
for (int i = 0; i < 5; i ++) {
print_blank(this->coordinates[i]);
}
attroff(COLOR_PAIR(ColorCode::BLACK_ON_RED));
}
void print_blue_coordinates() {
attron(COLOR_PAIR(ColorCode::BLACK_ON_BLUE));
std::random_shuffle(
this->potential_blue_coordinates.begin(),
this->potential_blue_coordinates.end(),
myrandom);
for (int i = 0; i < 5; i ++) {
print_blank(this->potential_blue_coordinates[i]);
}
attroff(COLOR_PAIR(ColorCode::BLACK_ON_BLUE));
}
};
class Snake {
std::vector<Coordinates> trace;
bool _is_alive;
bool _is_green;
Canvas canvas;
void blacken_current_position() {
blacken_coordinate(trace[0]);
}
void green_current_position(Coordinates c) {
attron(COLOR_PAIR(ColorCode::BLACK_ON_GREEN));
print_blank(c);
attroff(COLOR_PAIR(ColorCode::BLACK_ON_GREEN));
}
void green_current_position() {
green_current_position(trace[0]);
}
bool can_advance_to_position(int x, int y) {
if (!is_alive()) {
return false;
}
if (
(y == this->canvas.minx)
|| (y == this->canvas.maxx - 1)
|| (x == this->canvas.miny)
|| (x == this->canvas.maxy - 1)
){
this->_is_alive = false;
return false;
}
const int next_position_color =
mvinch(x, y) & A_COLOR;
if (next_position_color == COLOR_PAIR(ColorCode::BLACK_ON_RED)) {
this->_is_alive = false;
return false;
}
return true;
}
bool can_advance_to_position(Coordinates c) {
return can_advance_to_position(c.x, c.y);
}
bool should_grow(Coordinates c) {
const int next_position_color =
mvinch(c.x, c.y) & A_COLOR;
bool is_blue_dot = next_position_color == COLOR_PAIR(ColorCode::BLACK_ON_BLUE);
if (is_blue_dot) {
this->canvas.available_blue_coordinates --;
this->canvas.score ++;
}
if (this->canvas.available_blue_coordinates <= 0) {
this->canvas.print_blue_coordinates();
this->canvas.available_blue_coordinates = 5;
}
return is_blue_dot;
}
void blacken_coordinate(Coordinates c) {
attron(COLOR_PAIR(ColorCode::BLACK_ON_BLACK));
print_blank(c);
attroff(COLOR_PAIR(ColorCode::BLACK_ON_BLACK));
}
void blacken_snake() {
attron(COLOR_PAIR(ColorCode::BLACK_ON_BLACK));
for (int i = 0; i < trace.size(); i ++) {
print_blank(trace[i]);
}
attroff(COLOR_PAIR(ColorCode::BLACK_ON_BLACK));
}
void green_snake() {
attron(COLOR_PAIR(ColorCode::BLACK_ON_GREEN));
for (int i = 0; i < trace.size(); i ++) {
print_blank(trace[i]);
}
attroff(COLOR_PAIR(ColorCode::BLACK_ON_GREEN));
}
void redraw(Coordinates next_coordinate) {
if (!can_advance_to_position(next_coordinate)) {
return;
}
if (!should_grow(next_coordinate)) {
Coordinates last = trace[0];
blacken_coordinate(last);
trace.erase(trace.begin());
}
trace.push_back(next_coordinate);
for (int i = 0; i <trace.size(); i ++) {
green_current_position(trace[i]);
}
this->canvas.print_score();
}
public:
Snake(Canvas canvas) {
this->trace.push_back(canvas.snake_origin);
this->_is_alive = true;
this->canvas = canvas;
green_current_position(canvas.snake_origin);
}
void move_up() {
Coordinates next_coordinate(trace[trace.size()-1].x - 1, trace[trace.size()-1].y);
redraw(next_coordinate);
}
void move_down() {
Coordinates next_coordinate(trace[trace.size()-1].x + 1, trace[trace.size()-1].y);
redraw(next_coordinate);
}
void move_left() {
Coordinates next_coordinate(trace[trace.size()-1].x, trace[trace.size()-1].y - 1);
redraw(next_coordinate);
}
void move_right() {
Coordinates next_coordinate(trace[trace.size()-1].x, trace[trace.size()-1].y + 1);
redraw(next_coordinate);
}
void blink() {
const int this_position_color =
mvinch(trace[0].x, trace[0].y) & A_COLOR;
if (this->_is_green) {
blacken_snake();
this->_is_green = false;
} else {
green_snake();
this->_is_green = true;
}
refresh();
}
bool is_alive() {
return _is_alive;
}
};
void exit_unless_colors_are_supported() {
if (has_colors() == false) {
endwin();
printf("This Canvas does not support colors\n");
std::exit(1);
}
}
void exit_if_cannot_change_color() {
if (!can_change_color()) {
std::cout << "This Canvas does not support colors\n";
std::exit(1);
}
}
Canvas init_curses() {
initscr();
curs_set(0);
nonl();
intrflush(stdscr, false);
keypad(stdscr, true);
exit_unless_colors_are_supported();
exit_if_cannot_change_color();
start_color();
cbreak();
mousemask(ALL_MOUSE_EVENTS, NULL);
keypad(stdscr, true);
noecho();
int row, col;
getmaxyx(stdscr, row, col);
Canvas c(row, col);
return c;
}
class KeyContext {
bool _is_alive;
public:
int current_key;
KeyContext() : _is_alive(true), current_key(999999) {}
void manage() {
current_key = getch();
while (this->_is_alive) {
this->current_key = getch();
if (this->current_key == 27) {
kill();
return;
}
}
}
void kill() {
this->_is_alive = false;
}
};
int main(int argc, char* argv[]) {
Canvas canvas = init_curses();
canvas.generate_coordinates();
canvas.print_black_coordinates();
canvas.print_red_coordinates();
canvas.print_blue_coordinates();
ColorCode::initialize_all();
Snake s(canvas);
refresh();
KeyContext kc;
std::thread key_context_thread(&KeyContext::manage, &kc);
while (s.is_alive()) {
if (kc.current_key == KEY_UP) {
s.move_up();
} else if (kc.current_key == KEY_DOWN) {
s.move_down();
} else if (kc.current_key == KEY_LEFT) {
s.move_left();
} else if (kc.current_key == KEY_RIGHT) {
s.move_right();
} else if (kc.current_key == 27) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
refresh();
}
kc.kill();
while (!s.is_alive()) {
s.blink();
refresh();
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
key_context_thread.join();
endwin();
return 0;
}