-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
505 lines (435 loc) · 15.6 KB
/
Copy pathsnake.cpp
File metadata and controls
505 lines (435 loc) · 15.6 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
505
#include <sys/ioctl.h>
#include <termios.h> // for terminal control
#include <unistd.h> // for read()
#include <cstdlib> // for rand()
#include <ctime> // for time()
#include <iostream>
#include <vector>
int WIDTH = 20; // Default fallback if terminal size fails
int HEIGHT = 10;
const char WALL_CHAR = '#';
const char SNAKE_HEAD_CHAR = '@';
const char SNAKE_BODY_CHAR = 'o';
const char FOOD_CHAR = '*';
const char EMPTY_CHAR = ' ';
struct Coord {
int x;
int y;
};
// The snake is a list of coordinates
std::vector<Coord> snake;
Coord food;
enum Direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; // 0 - 4
Direction dir;
bool useHalfSize = false;
bool gameRunning = false;
bool isPaused = false;
void clearScreen() {
// std clearing vs system clear
// Faster: No new process, no new shell, it directly writes bytes to the terminal.
// Portable: Works in most modern terminal emulators that support ANSI
std::cout << "\033[2J\033[H";
}
void setBufferedInput(bool enable) {
static struct termios old;
struct termios newt;
if (!enable) {
// STDIN_FILENO: file descriptor for standard input (stdin)
tcgetattr(STDIN_FILENO, &old); // get current terminal attributes
newt = old; // copy them
// ICANON: turns off line buffering so input is processed immediately
// ECHO: disables echoing characters to screen when typed
// c_lflag: local modes flag (controls various terminal behaviors)
newt.c_lflag &= ~(ICANON | ECHO); // disable buffering and echo
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
} else {
tcsetattr(STDIN_FILENO, TCSANOW, &old); // restore old attributes
}
}
void cleanupAndExit(int status) {
setBufferedInput(true); // Always restore echo before exit
exit(status);
}
void drawMainMenuScreen() {
// clang-format off
const std::vector<std::string> snakeArt = {
"##### ## # # ## ## #####",
"# ### # # # # # ## ",
"##### # ## # ##### ### #### ",
" # # ### # # # # ## ",
"##### # ## # # # # ###### ",
};
// clang-format on
int artHeight = snakeArt.size();
int artWidth = snakeArt[0].length();
int offsetY = (HEIGHT / 2) - (artHeight / 2) - 2;
int offsetX = (WIDTH / 2) - (artWidth / 2);
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (y >= offsetY && y < offsetY + artHeight && x >= offsetX && x < offsetX + artWidth) {
std::cout << snakeArt[y - offsetY][x - offsetX];
} else {
std::cout << EMPTY_CHAR;
}
}
std::cout << std::endl;
}
// Print menu text after drawing art
std::cout << "\n1. " << (gameRunning ? "Resume Game" : "Start Game") << std::endl;
if (!gameRunning) {
std::cout << "2. Toggle Board Size (currently: " << (useHalfSize ? "50%" : "Full") << ")\n";
}
std::cout << "3. View Controls\n";
std::cout << "4. Quit\n";
if (gameRunning) {
std::cout << "\nEnter choice (or press M or Esc to resume): ";
} else {
std::cout << "\nEnter choice: ";
}
}
// Generate new food position, not on top of the snake
Coord generateFoodPosition() {
Coord newFood;
do {
newFood.x = std::rand() % (WIDTH - 2) + 1;
newFood.y = std::rand() % (HEIGHT - 2) + 1;
bool onSnake = false;
for (const auto &part : snake) {
if (part.x == newFood.x && part.y == newFood.y) {
onSnake = true;
break;
}
}
if (!onSnake) break;
} while (true);
return newFood;
}
void initializeDimensions() {
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
WIDTH = w.ws_col;
HEIGHT = w.ws_row;
if (WIDTH < 20) WIDTH = 20;
if (HEIGHT < 10) HEIGHT = 10;
if (HEIGHT > 2) HEIGHT -= 1;
if (useHalfSize) {
WIDTH = WIDTH / 2;
HEIGHT = HEIGHT / 2;
// TODO: consider making verticalFrames global for dynamic snake speed
// verticalFrames = 3;
} else {
// verticalFrames = 2;
}
} else {
std::cerr << "Could not detect terminal size, using default." << std::endl;
}
}
void initializeGame() {
clearScreen(); // clear before first draw
gameRunning = true;
snake.clear(); // Start snake in center
snake.push_back({WIDTH / 2, HEIGHT / 2});
std::srand(std::time(0)); // Seed random generator
food = generateFoodPosition(); // Place food somewhere not on the snake
}
void showControls() {
clearScreen();
std::vector<std::string> lines = {"================= Controls =================",
"",
"During Gameplay:",
" w - Move up",
" a - Move left",
" s - Move down",
" d - Move right",
" p - Pause/Unpause",
" m or Esc - Open main menu",
" x - Stop (snake stops moving)",
"",
"Press any key to return to the main menu..."};
int startY = (HEIGHT / 2) - (lines.size() / 2);
if (startY < 0) startY = 0;
for (size_t i = 0; i < lines.size(); ++i) {
int lineLength = lines[i].length();
int startX = (WIDTH / 2) - (lineLength / 2);
if (startX < 0) startX = 0;
std::cout << std::string(startX, ' ') << lines[i] << std::endl;
}
// Wait for any key press to return
char dummy;
clearScreen();
read(STDIN_FILENO, &dummy, 1);
}
void showMenu(bool allowResize = true) {
clearScreen();
while (true) {
drawMainMenuScreen();
std::cout << std::flush;
char choice;
int bytesWaiting;
ioctl(0, FIONREAD, &bytesWaiting);
// Wait for valid input
while (bytesWaiting == 0) {
ioctl(0, FIONREAD, &bytesWaiting);
usleep(10000); // Sleep briefly to avoid CPU spin
}
read(STDIN_FILENO, &choice, 1);
// The Escape key can be represented as:
// 27 (decimal ASCII code)
// \x1b (hexadecimal escape sequence)
// \033 (octal escape sequence)
// They all mean the same single-byte Escape character.
//
// When arrow keys are pressed, they also start with Escape (27),
// followed by extra bytes (e.g., '[' and 'A' for Up Arrow).
// For example, if you press Up Arrow, your terminal typically sends:
// [27, 91, 65] // ESC, '[', 'A'
// To detect a true Escape key press, we check if no extra bytes follow.
// This avoids incorrectly interpreting arrow keys as Escape
// and helps reduce flickering or unintended menu exits.
if (choice == '\033') {
// When we check if there are more bytes in the buffer (using ioctl(FIONREAD, ...)),
// we can distinguish:
// 1. If no extra bytes → it’s just Escape.
// 2. If extra bytes → it’s an arrow key (or some other escape sequence).
// This is why we read seq[0], seq[1], etc., and only treat it as Escape if nothing else
// is there.
ioctl(0, FIONREAD, &bytesWaiting);
if (bytesWaiting > 0) {
// Start building sequence buffer
std::vector<char> seq;
while (bytesWaiting > 0) {
char next;
read(STDIN_FILENO, &next, 1);
seq.push_back(next);
ioctl(0, FIONREAD, &bytesWaiting);
}
// Redraw menu and forcibly reset cursor here to reset after
// escape keys are scraped
if (useHalfSize) {
clearScreen();
drawMainMenuScreen();
std::cout << "\033[H" << std::flush; // Move cursor to 0,0
}
// seq contains full sequence or multiple sequences
// Instead of treating as plain ESC, we can safely continue and redraw
continue; // Ignore arrow keys, redraw menu
}
// If no more bytes: plain ESC (close menu)
}
clearScreen(); // Always clear before next decision
if (choice == '1' || choice == 'm' || choice == 'M' || choice == '\033') {
if (!gameRunning) {
initializeGame();
}
return; // Resume game
} else if (choice == '2' && allowResize && !gameRunning) {
useHalfSize = !useHalfSize;
initializeDimensions();
} else if (choice == '3') {
showControls();
} else if (choice == '4') {
cleanupAndExit(0);
}
// Otherwise loop back and redraw without recursion
}
}
void drawPauseScreen() {
// Example ASCII "PAUSE" banner
// clang-format off
const std::vector<std::string> pauseArt = {
"##### ### # # #### #####",
"# # # # # # # # ",
"##### ##### # # ### #### ",
"# # # # # # # ",
"# # # ### #### #####"
};
// clang-format on
int artHeight = pauseArt.size();
int artWidth = pauseArt[0].length();
int offsetY = (HEIGHT / 2) - (artHeight / 2);
int offsetX = (WIDTH / 2) - (artWidth / 2);
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
if (y >= offsetY && y < offsetY + artHeight && x >= offsetX && x < offsetX + artWidth) {
std::cout << pauseArt[y - offsetY][x - offsetX];
} else {
std::cout << EMPTY_CHAR;
}
}
std::cout << std::endl;
}
}
void readInput() {
char c;
// Check if there's input ready to read (non-blocking)
int bytesWaiting;
ioctl(0, FIONREAD, &bytesWaiting);
if (bytesWaiting > 0) {
read(STDIN_FILENO, &c, 1);
// Ignore arrow keys: they begin with '\033' (escape sequence)
if (c == '\033') {
// Check if it's a full escape sequence (arrow keys)
char seq[2];
// Non-blocking check: if no more bytes, it's plain ESC
ioctl(0, FIONREAD, &bytesWaiting);
if (bytesWaiting == 0) {
// Plain ESC pressed
showMenu(false);
isPaused = false;
return;
}
if (read(STDIN_FILENO, &seq[0], 1) == 0) return;
if (read(STDIN_FILENO, &seq[1], 1) == 0) return;
// It's an arrow key sequence, ignore it
return;
}
switch (c) {
case 'w':
if (dir != DOWN) dir = UP;
break;
case 's':
if (dir != UP) dir = DOWN;
break;
case 'a':
if (dir != RIGHT) dir = LEFT;
break;
case 'd':
if (dir != LEFT) dir = RIGHT;
break;
case 'p':
case 'P':
isPaused = !isPaused;
break;
case 'm':
case 'M':
case 27: // Escape key aka '\x1b'
showMenu(false); // Do not allow resize during game
isPaused = false; // Auto-unpause after menu
break;
case 'x':
dir = STOP;
break;
default:
break;
}
// std::cout << "Dir: " << dir << std::endl; // uncomment line to debug key strokes
}
}
// Returns empty string if no collision, or a reason message if collision
std::string checkCollision(const Coord &head) {
// Check wall collision
if (head.x == 0 || head.x == WIDTH - 1 || head.y == 0 || head.y == HEIGHT - 1) {
return "You hit a wall!";
}
// Check self collision
for (const auto &part : snake) {
if (part.x == head.x && part.y == head.y) {
return "You ran into yourself!";
}
}
return "";
}
void updateSnake() {
if (dir == STOP) return; // Don't move if stopped
// Get current head position
Coord head = snake.back();
// Move head in current direction
switch (dir) {
case UP:
head.y--;
break;
case DOWN:
head.y++;
break;
case LEFT:
head.x--;
break;
case RIGHT:
head.x++;
break;
default:
break;
}
std::string reason = checkCollision(head);
if (!reason.empty()) {
std::cout << "Game Over! " << reason << std::endl;
cleanupAndExit(0);
}
// Add new head to snake
snake.push_back(head);
// Check if head is on food
if (head.x == food.x && head.y == food.y) {
food = generateFoodPosition();
// No tail removal = snake grows
} else {
// Remove tail to keep same length
snake.erase(snake.begin());
}
}
void drawBoard() {
for (int y = 0; y < HEIGHT; ++y) {
for (int x = 0; x < WIDTH; ++x) {
// Walls (top and bottom rows or first/last columns)
if (y == 0 || y == HEIGHT - 1 || x == 0 || x == WIDTH - 1) {
std::cout << WALL_CHAR;
}
// Food
else if (x == food.x && y == food.y) {
std::cout << FOOD_CHAR;
}
// Snake
else {
bool isSnakePart = false;
for (const auto &part : snake) {
if (part.x == x && part.y == y) {
if (&part == &snake.back()) {
std::cout << SNAKE_HEAD_CHAR;
} else {
std::cout << SNAKE_BODY_CHAR;
}
isSnakePart = true;
break;
}
}
if (!isSnakePart) {
std::cout << EMPTY_CHAR;
}
}
}
std::cout << std::endl;
}
}
int main() {
// terminal throttling vertical vs horizontal due to font discrepancy
int frameCount = 0;
const int horizontalFrames = 1; // Update every frame for horizontal movement
const int verticalFrames = 2; // Adjust this to slow down vertical if needed
initializeDimensions(); // Get terminal size first, for proper art centering
setBufferedInput(false); // Enable raw input
showMenu();
while (true) {
if (isPaused) {
clearScreen();
drawPauseScreen();
readInput();
usleep(100000);
std::cout << "\033[H"; // Move cursor to home position top-left (faster than clear)
continue; // Skip the rest of this iteration while paused
}
drawBoard();
readInput();
// Decide which frame delay to use based on direction
int effectiveMoveFrames = horizontalFrames;
if (dir == UP || dir == DOWN) {
effectiveMoveFrames = verticalFrames;
}
// Only move on specified frames
if (dir != STOP && frameCount % effectiveMoveFrames == 0) {
updateSnake();
}
usleep(100000); // short delay, ~100ms
std::cout << "\033[H"; // Move cursor to home position top-left (faster than clear)
frameCount++;
}
cleanupAndExit(0);
}