-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.hpp
270 lines (250 loc) · 7.76 KB
/
board.hpp
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
#include <chrono>
#include <iostream>
#include <vector>
#include <random>
#include <string>
#include <thread>
#include <curses.h>
#include "snake.hpp"
#define WALLCOLOR 1
#define FOODCOLOR 2
#define SNAKECOLOR 3
class Board
{
public:
// Makes a new board with 100 milliseconds between
// each snake move
Board();
// Starts the main game loop
void run();
~Board();
private:
int height, width, score, multiplier;
bool paused, dead, immortal;
Coord food;
Snake snake;
WINDOW *win;
std::vector<std::vector<chtype>> board;
std::chrono::system_clock::time_point last_moved, food_created;
std::chrono::milliseconds speed_time, food_time;
std::uniform_int_distribution<int> generator;
std::mt19937 rand_eng;
void update_screen();
// Generates a random Coord for food that is within the board and
// not occupied by the snake and sets food_time to a random
// amount of time between the time it would take the snake
// head to get to food (x1.5) and double that time (so between x1.5
// optimal time and x3 optimal time)
void gen_food();
};
Board::Board()
: speed_time(std::chrono::milliseconds(100)), paused(false), dead(false), immortal(false),
food({0, 0}), score(0), multiplier(1)
{
initscr(); // Initialize ncurses terminal window
getmaxyx(stdscr, height, width); // Get the height/width of the terminal window
win = stdscr;
cbreak(); // Disable line buffering
noecho(); // Don't echo key presses to the terminal
nodelay(win, true); // Enable non blocking input capture
keypad(win, true); // Capture arrow keys as input
curs_set(0); // Set the cursor to invisible
start_color(); // Enable colors
snake.begin(height / 2, width / 2); // Start the snake in the middle of the screen
init_pair(WALLCOLOR, COLOR_RED, COLOR_RED);
init_pair(FOODCOLOR, COLOR_GREEN, COLOR_MAGENTA);
init_pair(SNAKECOLOR, COLOR_BLUE, COLOR_BLACK);
last_moved = std::chrono::system_clock::now();
std::random_device rd;
rand_eng.seed(rd());
generator = std::uniform_int_distribution<int>(1, height > width ? height : width);
gen_food();
}
Board::~Board()
{
endwin(); // Restore terminal settings
}
void Board::update_screen()
{
wmove(win, 0, 0);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0 || i == 0 || j == width - 1 || i == height - 1)
{
waddch(win, ACS_BLOCK | A_BOLD | COLOR_PAIR(WALLCOLOR));
}
else if (snake.ishead(i, j))
{
waddch(win, '0' | COLOR_PAIR(SNAKECOLOR));
}
else if (snake.occupies(i, j))
{
waddch(win, 'O' | COLOR_PAIR(SNAKECOLOR));
}
else if (food == (Coord{i, j}))
{
waddch(win, 'A' | COLOR_PAIR(FOODCOLOR));
}
else
{
waddch(win, ' ');
}
}
}
wmove(win, 0, 0);
waddstr(win, " Score ");
waddstr(win, std::to_string(score).c_str());
wrefresh(win);
}
void Board::run()
{
int ch = 0;
while (ch != 'q')
{
if ((ch = getch()) != ERR)
{
switch (ch)
{
case KEY_UP:
snake.redirect(North);
break;
case KEY_DOWN:
snake.redirect(South);
break;
case KEY_RIGHT:
snake.redirect(East);
break;
case KEY_LEFT:
snake.redirect(West);
break;
case 'p':
paused = !paused;
if (paused)
{
wmove(win, (height / 2), (width / 2) - 5);
waddstr(win, "PAUSED");
wrefresh(win);
}
break;
case 'c':
if (paused)
{
wmove(win, height - 1, 0);
waddstr(win, "Cheat code: ");
wrefresh(win);
nocbreak();
echo();
curs_set(1);
nodelay(win, false);
char cstr[20];
getstr(cstr);
std::string str = cstr;
auto twentymills = std::chrono::milliseconds(20);
if (str == "faster")
{
if (speed_time > twentymills)
{
speed_time -= twentymills;
}
}
else if (str == "slower")
{
speed_time += twentymills;
}
else if (str == "speeddemon")
{
speed_time = std::chrono::milliseconds(5);
}
else if (str == "slowpoke")
{
speed_time = std::chrono::milliseconds(500);
}
else if (str == "greedy")
{
multiplier++;
}
else if (str == "greedier")
{
multiplier += 10;
}
else if (str == "immortal")
{
immortal = !immortal;
}
else if (str == "saint")
{
immortal = false;
speed_time = std::chrono::milliseconds(100);
multiplier = 1;
}
curs_set(0);
nodelay(win, true);
cbreak();
noecho();
paused = false;
}
break;
case 'r':
if (dead)
{
snake = Snake();
snake.begin(height / 2, width / 2);
dead = false;
score = 0;
gen_food();
}
}
}
if (paused || dead)
{
continue;
}
auto now = std::chrono::system_clock::now();
if (now - last_moved >= speed_time)
{
auto next_move = snake.next_move();
last_moved = now;
if (next_move.x == 0 || next_move.y == 0 || next_move.x == height - 1 || next_move.y == width - 1 || snake.occupies(next_move))
{
if (immortal)
continue;
dead = true;
wmove(win, (height / 2), (width / 2) - 5);
waddstr(win, "GAME OVER");
wrefresh(win);
continue;
}
bool eat = next_move == food;
snake.move();
if (eat)
{
snake.eat(multiplier);
score += multiplier;
gen_food();
}
}
now = std::chrono::system_clock::now();
if (now - food_created >= food_time)
{
gen_food();
}
update_screen();
}
}
void Board::gen_food()
{
Coord xy;
do
{
xy = {generator(rand_eng), generator(rand_eng)};
} while (snake.occupies(xy.x, xy.y) || xy.x >= height || xy.y >= width);
Coord snakehead = snake.head();
int xx = abs(snakehead.x - xy.x) + abs(snakehead.y - xy.y);
xx += xx / 2;
std::uniform_int_distribution<int> gen(xx, xx * 2);
food_time = speed_time * gen(rand_eng);
food_created = std::chrono::system_clock::now();
food = xy;
}