-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
1152 lines (1008 loc) · 43.2 KB
/
snake.cpp
File metadata and controls
1152 lines (1008 loc) · 43.2 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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <time.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/kd.h>
#include <cmath>
#include <sys/wait.h>
#include <stdint.h>
#include <iostream>
#include <deque>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <sys/select.h>
// ─── ANSI Constants ─────────────────────────────────────────
#define RESET "\033[0m"
#define BOLD "\033[1m"
#define DIM "\033[2m"
#define REVERSE "\033[7m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define CYAN "\033[36m"
#define BRIGHT_GREEN "\033[92m"
#define BRIGHT_CYAN "\033[96m"
#define BRIGHT_WHITE "\033[97m"
#define ERASE_LINE "\033[K"
#define ERASE_BELOW "\033[J"
// ─── Board ──────────────────────────────────────────────────
static const int BOARD_WIDTH = 40;
static const int BOARD_HEIGHT = 20;
static const int MIN_TERM_W = BOARD_WIDTH * 2 + 10;
static const int MIN_TERM_H = BOARD_HEIGHT + 6;
// ─── Game Constants ─────────────────────────────────────────
static const char* APP_DIR_NAME = "vsnake";
static const char* SCORE_FILENAME = "snake_scores.txt";
static const int APPLE_MAX_TRIES = 1000;
// ─── Timing ─────────────────────────────────────────────────
static const int RENDER_TICK_US = 30000;
static const int BASE_MOVE_US = 120000;
static const int MIN_MOVE_US = 60000;
static const int SPEED_SCORE_STEP = 50;
static const int SPEED_REDUCE_US = 5000;
static const float VERT_SPEED_FACTOR = 1.2f;
// ─── Animation ──────────────────────────────────────────────
static const int APPLE_BLINK_HALF = 16;
static const int HEAD_GLOW_PERIOD = 10;
static const int APPLE_SPARKLE_RATE = 12;
static const int FLASH_DURATION = 24;
// ─── Signal ─────────────────────────────────────────────────
static volatile sig_atomic_t g_interrupted = 0;
void signalHandler(int) { g_interrupted = 1; }
// ─── Direction ──────────────────────────────────────────────
enum Direction { UP, DOWN, LEFT, RIGHT };
static bool isOpposite(Direction a, Direction b) {
return (a == UP && b == DOWN) || (a == DOWN && b == UP) ||
(a == LEFT && b == RIGHT) || (a == RIGHT && b == LEFT);
}
static bool isVertical(Direction d) { return d == UP || d == DOWN; }
struct Point {
int x, y;
bool operator==(const Point& o) const { return x == o.x && y == o.y; }
};
struct ScoreEntry {
std::string timestamp;
int score;
};
// ─── App State Machine ─────────────────────────────────────
enum AppState {
STATE_MENU, STATE_PLAYING, STATE_GAMEOVER,
STATE_RESIZED, STATE_TOO_SMALL, STATE_LEADERBOARD, STATE_EXIT
};
// ─── Game State ─────────────────────────────────────────────
struct GameState {
std::deque<Point> snake;
Point apple;
Direction dir, nextDir;
int score;
int boardWidth, boardHeight;
int termWidth, termHeight;
int offsetX, offsetY;
bool running, gameOver, gameWon;
bool termResized, termTooSmall;
bool paused, restartRequested;
bool dirChangedThisTick, hasQueuedDir;
Direction queuedDir;
long long moveAccumulator;
unsigned long frameCount;
int appleFlashTimer, scoreFlashTimer, prevScore;
std::vector<char> grid;
std::string renderBuf;
void allocateBuffers() {
grid.resize(boardWidth * boardHeight);
renderBuf.reserve((boardWidth * 2 + 80) * (boardHeight + 8));
}
};
// ─── Terminal ───────────────────────────────────────────────
static struct termios origTermios;
static bool rawModeEnabled = false;
void disableRawMode() {
if (rawModeEnabled) {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &origTermios);
rawModeEnabled = false;
}
}
void enableRawMode() {
tcgetattr(STDIN_FILENO, &origTermios);
struct termios raw = origTermios;
raw.c_lflag &= ~(ECHO | ICANON);
raw.c_iflag &= ~(IXON | ICRNL | BRKINT | INPCK | ISTRIP);
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
rawModeEnabled = true;
}
void clearScreen() { write(STDOUT_FILENO, "\033[2J\033[1;1H", 11); }
void hideCursor() { write(STDOUT_FILENO, "\033[?25l", 6); }
void showCursor() { write(STDOUT_FILENO, "\033[?25h", 6); }
void getTerminalSize(int &w, int &h) {
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 ||
ws.ws_col == 0 || ws.ws_row == 0) { w = 80; h = 24; }
else { w = ws.ws_col; h = ws.ws_row; }
}
long long nowMicros() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL;
}
void performCleanup() {
write(STDOUT_FILENO, "\033[?1049l", 8);
write(STDOUT_FILENO, "\033[0m", 4);
write(STDOUT_FILENO, "\033[2J\033[H", 7);
showCursor();
disableRawMode();
}
void atexitCleanup() { performCleanup(); }
// ===== SOUND SYSTEM ========================================
//
// Generates WAV audio in-memory and pipes it to aplay/paplay
// in a detached child process. Works on ALL terminal emulators
// (bypasses the terminal entirely — no BEL dependency).
//
// Requires one of: aplay (alsa-utils) | paplay (pulseaudio)
// Fails silently if neither is installed.
//
static const int SND_RATE = 44100;
// --- Pre-generated WAV buffers (filled once by initSound) ---
static std::vector<uint8_t> g_wavEat;
static std::vector<uint8_t> g_wavGameOver;
static std::vector<uint8_t> g_wavMenuMove;
static std::vector<uint8_t> g_wavMenuSelect;
static std::vector<uint8_t> g_wavPause;
// Append a sine-wave tone to a PCM sample buffer
static void appendTone(std::vector<int16_t>& pcm, float freq,
float duration, float vol = 0.25f,
bool fadeOut = true) {
int n = (int)(SND_RATE * duration);
int attack = SND_RATE * 2 / 1000; // 2 ms click-free ramp
for (int i = 0; i < n; i++) {
float t = (float)i / SND_RATE;
float env = fadeOut ? (1.0f - (float)i / n) : 1.0f;
if (i < attack) env *= (float)i / attack; // smooth attack
float s = sinf(2.0f * (float)M_PI * freq * t) * vol * env;
int32_t v = (int32_t)(s * 32767);
if (v > 32767) v = 32767;
if (v < -32767) v = -32767;
pcm.push_back((int16_t)v);
}
}
// Build a valid WAV file (RIFF) in memory from signed-16-bit mono PCM
static std::vector<uint8_t> buildWAV(const std::vector<int16_t>& pcm) {
uint32_t dataSize = (uint32_t)(pcm.size() * sizeof(int16_t));
std::vector<uint8_t> wav(44 + dataSize);
uint8_t* p = wav.data();
auto w16 = [&](uint16_t v) { memcpy(p, &v, 2); p += 2; };
auto w32 = [&](uint32_t v) { memcpy(p, &v, 4); p += 4; };
auto tag = [&](const char* s){ memcpy(p, s, 4); p += 4; };
tag("RIFF"); w32(36 + dataSize); tag("WAVE");
tag("fmt "); w32(16);
w16(1); // PCM format
w16(1); // mono
w32((uint32_t)SND_RATE); // sample rate
w32((uint32_t)(SND_RATE * 2)); // byte rate (rate × channels × bytes/sample)
w16(2); // block align (channels × bytes/sample)
w16(16); // bits per sample
tag("data"); w32(dataSize);
memcpy(p, pcm.data(), dataSize);
return wav;
}
// Fire-and-forget: pipe WAV data to an audio player in a fully
// detached grandchild process. Uses double-fork so no zombies.
static void playWAVAsync(const std::vector<uint8_t>& wav) {
if (wav.empty()) return;
int pfd[2];
if (pipe(pfd) != 0) return;
pid_t pid = fork();
if (pid < 0) { close(pfd[0]); close(pfd[1]); return; }
if (pid == 0) {
// ── intermediate child ──
pid_t pid2 = fork();
if (pid2 < 0) _exit(1);
if (pid2 > 0) _exit(0); // exit immediately → parent's waitpid returns fast
// ── grandchild (orphaned, adopted by init) ──
setsid(); // detach from terminal session
close(pfd[1]); // close write end
dup2(pfd[0], STDIN_FILENO); // pipe read → stdin
close(pfd[0]);
// silence stdout/stderr so aplay doesn't pollute the terminal
int devnull = open("/dev/null", O_WRONLY);
if (devnull >= 0) {
dup2(devnull, STDOUT_FILENO);
dup2(devnull, STDERR_FILENO);
close(devnull);
}
// try players in order — execlp only returns on failure
execlp("aplay", "aplay", "-q", "-t", "wav", "-", (char*)NULL);
execlp("paplay", "paplay", "-", (char*)NULL);
_exit(1); // no player found — exit silently
}
// ── parent ──
close(pfd[0]);
const uint8_t* data = wav.data();
size_t rem = wav.size();
while (rem > 0) {
ssize_t n = write(pfd[1], data, rem);
if (n <= 0) break; // EPIPE or error — ignore
data += n;
rem -= n;
}
close(pfd[1]);
waitpid(pid, nullptr, 0); // reap intermediate child (instant)
}
// Pre-generate every sound effect once as a WAV buffer
static void initSound() {
// Eat apple
{
std::vector<int16_t> pcm;
appendTone(pcm, 1047.0f, 0.035f, 0.20f, false);
appendTone(pcm, 1319.0f, 0.035f, 0.20f, false);
appendTone(pcm, 1568.0f, 0.06f, 0.20f);
g_wavEat = buildWAV(pcm);
}
// Game over — sad descending tones
{
std::vector<int16_t> pcm;
appendTone(pcm, 440.0f, 0.18f, 0.22f);
appendTone(pcm, 330.0f, 0.18f, 0.20f);
appendTone(pcm, 220.0f, 0.28f, 0.18f);
g_wavGameOver = buildWAV(pcm);
}
// Menu move — short tick
{
std::vector<int16_t> pcm;
appendTone(pcm, 660.0f, 0.035f, 0.15f);
g_wavMenuMove = buildWAV(pcm);
}
// Menu select
{
std::vector<int16_t> pcm;
appendTone(pcm, 550.0f, 0.05f, 0.15f);
g_wavMenuSelect = buildWAV(pcm);
}
// Pause toggle — blip
{
std::vector<int16_t> pcm;
appendTone(pcm, 550.0f, 0.05f, 0.15f);
g_wavPause = buildWAV(pcm);
}
}
// ── Sound API (called from game code) ──
inline void soundEat() { playWAVAsync(g_wavEat); }
inline void soundGameOver() { playWAVAsync(g_wavGameOver); }
inline void soundMenuMove() { playWAVAsync(g_wavMenuMove); }
inline void soundMenuSelect() { playWAVAsync(g_wavMenuSelect); }
inline void soundPauseToggle() { playWAVAsync(g_wavPause); }
// ─── Timestamp ──────────────────────────────────────────────
std::string getCurrentTimestamp() {
time_t now = time(nullptr);
struct tm *t = localtime(&now);
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t);
return std::string(buf);
}
// ─── XDG Score Path ─────────────────────────────────────────
static bool ensureDirectoryExists(const std::string &path) {
struct stat st;
if (stat(path.c_str(), &st) == 0) return S_ISDIR(st.st_mode);
return mkdir(path.c_str(), 0755) == 0;
}
static bool mkdirRecursive(const std::string &path) {
if (path.empty()) return false;
std::string built;
for (size_t i = 0; i < path.size(); i++) {
built += path[i];
if (path[i] == '/' && built.size() > 1) {
struct stat st;
if (stat(built.c_str(), &st) != 0)
if (mkdir(built.c_str(), 0755) != 0 && errno != EEXIST)
return false;
}
}
return ensureDirectoryExists(path);
}
static std::string getScoreFilePath() {
std::string dataDir;
const char* xdg = getenv("XDG_DATA_HOME");
if (xdg && xdg[0] != '\0')
dataDir = std::string(xdg) + "/" + APP_DIR_NAME;
if (dataDir.empty()) {
const char* home = getenv("HOME");
if (home && home[0] != '\0')
dataDir = std::string(home) + "/.local/share/" + APP_DIR_NAME;
}
if (!dataDir.empty() && mkdirRecursive(dataDir))
return dataDir + "/" + SCORE_FILENAME;
return SCORE_FILENAME;
}
// ─── Leaderboard I/O ───────────────────────────────────────
void saveScore(int score) {
std::string path = getScoreFilePath();
std::ofstream file(path.c_str(), std::ios::app);
if (file.is_open())
file << getCurrentTimestamp() << " | " << score << "\n";
}
std::vector<ScoreEntry> loadScores() {
std::string path = getScoreFilePath();
std::vector<ScoreEntry> scores;
std::ifstream file(path.c_str());
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
size_t sep = line.find(" | ");
if (sep != std::string::npos) {
ScoreEntry e;
e.timestamp = line.substr(0, sep);
e.score = std::atoi(line.substr(sep + 3).c_str());
scores.push_back(e);
}
}
}
std::sort(scores.begin(), scores.end(),
[](const ScoreEntry &a, const ScoreEntry &b) {
if (a.score != b.score) return a.score > b.score;
return a.timestamp > b.timestamp;
});
return scores;
}
// ─── Movement ───────────────────────────────────────────────
long long calcBaseInterval(int score) {
int steps = score / SPEED_SCORE_STEP;
long long iv = BASE_MOVE_US - (long long)steps * SPEED_REDUCE_US;
return iv < MIN_MOVE_US ? MIN_MOVE_US : iv;
}
long long calcMoveInterval(int score, Direction d) {
long long iv = calcBaseInterval(score);
if (isVertical(d)) iv = (long long)(iv * VERT_SPEED_FACTOR);
return iv;
}
// ─── Apple Spawning ─────────────────────────────────────────
bool spawnApple(GameState &g) {
int total = g.boardWidth * g.boardHeight;
if ((int)g.snake.size() >= total) return false;
if ((int)g.snake.size() > total * 3 / 4) {
std::vector<char> occ(total, 0);
for (auto &s : g.snake) occ[s.y * g.boardWidth + s.x] = 1;
std::vector<Point> free;
for (int y = 0; y < g.boardHeight; y++)
for (int x = 0; x < g.boardWidth; x++)
if (!occ[y * g.boardWidth + x]) free.push_back({x, y});
if (free.empty()) return false;
g.apple = free[rand() % (int)free.size()];
g.appleFlashTimer = FLASH_DURATION;
return true;
}
for (int a = 0; a < APPLE_MAX_TRIES; a++) {
Point p = {rand() % g.boardWidth, rand() % g.boardHeight};
bool on = false;
for (auto &s : g.snake) if (s == p) { on = true; break; }
if (!on) { g.apple = p; g.appleFlashTimer = FLASH_DURATION; return true; }
}
for (int y = 0; y < g.boardHeight; y++)
for (int x = 0; x < g.boardWidth; x++) {
Point p = {x, y}; bool on = false;
for (auto &s : g.snake) if (s == p) { on = true; break; }
if (!on) { g.apple = p; g.appleFlashTimer = FLASH_DURATION; return true; }
}
return false;
}
// ─── Centering ──────────────────────────────────────────────
static void calcCenteringOffsets(GameState &g) {
int vw = BOARD_WIDTH * 2 + 4;
int vh = BOARD_HEIGHT + 5;
g.offsetX = std::max(0, (g.termWidth - vw) / 2);
g.offsetY = std::max(0, (g.termHeight - vh) / 2);
}
// ─── Init ───────────────────────────────────────────────────
void initGame(GameState &g) {
getTerminalSize(g.termWidth, g.termHeight);
g.termTooSmall = (g.termWidth < MIN_TERM_W || g.termHeight < MIN_TERM_H);
g.boardWidth = BOARD_WIDTH;
g.boardHeight = BOARD_HEIGHT;
calcCenteringOffsets(g);
g.snake.clear();
int cx = g.boardWidth / 2, cy = g.boardHeight / 2;
g.snake.push_back({cx, cy});
g.snake.push_back({cx - 1, cy});
g.snake.push_back({cx - 2, cy});
g.dir = RIGHT; g.nextDir = RIGHT;
g.score = 0; g.running = true;
g.gameOver = false; g.gameWon = false;
g.termResized = false; g.paused = false;
g.restartRequested = false;
g.dirChangedThisTick = false;
g.hasQueuedDir = false; g.queuedDir = RIGHT;
g.moveAccumulator = 0; g.frameCount = 0;
g.appleFlashTimer = 0; g.scoreFlashTimer = 0; g.prevScore = 0;
g.allocateBuffers();
spawnApple(g);
}
// ─── Resize Check ───────────────────────────────────────────
bool checkTerminalResize(GameState &g) {
int nw, nh; getTerminalSize(nw, nh);
if (nw != g.termWidth || nh != g.termHeight) {
g.termResized = true; g.running = false; return true;
}
return false;
}
// ─── Direction Change ───────────────────────────────────────
static void tryChangeDirection(GameState &g, Direction d) {
if (!g.dirChangedThisTick) {
if (!isOpposite(d, g.dir)) {
g.nextDir = d; g.dirChangedThisTick = true; g.hasQueuedDir = false;
}
} else {
if (!isOpposite(d, g.nextDir) && d != g.nextDir) {
g.queuedDir = d; g.hasQueuedDir = true;
}
}
}
// ─── Input ──────────────────────────────────────────────────
void readInput(GameState &g) {
char c = 0;
while (true) {
fd_set fds; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds);
struct timeval tv = {0, 0};
if (select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv) <= 0) break;
if (read(STDIN_FILENO, &c, 1) != 1) break;
if (c == 'q' || c == 'Q') { g.running = false; return; }
if (c == 'r' || c == 'R') { g.restartRequested = true; g.running = false; return; }
if (c == 'p' || c == 'P') { g.paused = !g.paused; soundPauseToggle(); continue; }
if (g.paused) continue;
if (c == '\033') {
char seq[2] = {0, 0};
fd_set f2; struct timeval t2;
FD_ZERO(&f2); FD_SET(STDIN_FILENO, &f2); t2 = {0, 5000};
if (select(STDIN_FILENO + 1, &f2, nullptr, nullptr, &t2) > 0)
read(STDIN_FILENO, &seq[0], 1);
FD_ZERO(&f2); FD_SET(STDIN_FILENO, &f2); t2 = {0, 5000};
if (select(STDIN_FILENO + 1, &f2, nullptr, nullptr, &t2) > 0)
read(STDIN_FILENO, &seq[1], 1);
if (seq[0] == '[') {
switch (seq[1]) {
case 'A': tryChangeDirection(g, UP); break;
case 'B': tryChangeDirection(g, DOWN); break;
case 'D': tryChangeDirection(g, LEFT); break;
case 'C': tryChangeDirection(g, RIGHT); break;
}
}
continue;
}
switch (c) {
case 'w': case 'W': case 'k': case 'K': tryChangeDirection(g, UP); break;
case 's': case 'S': case 'j': case 'J': tryChangeDirection(g, DOWN); break;
case 'a': case 'A': case 'h': case 'H': tryChangeDirection(g, LEFT); break;
case 'd': case 'D': case 'l': case 'L': tryChangeDirection(g, RIGHT); break;
}
}
}
// ─── Game Update ────────────────────────────────────────────
void updateGame(GameState &g) {
if (g.paused) return;
g.dir = g.nextDir;
Point head = g.snake.front(), nh = head;
switch (g.dir) {
case UP: nh.y--; break; case DOWN: nh.y++; break;
case LEFT: nh.x--; break; case RIGHT: nh.x++; break;
}
if (nh.x < 0 || nh.x >= g.boardWidth || nh.y < 0 || nh.y >= g.boardHeight) {
g.gameOver = true; g.running = false; soundGameOver(); return;
}
bool growing = (nh == g.apple);
int limit = (int)g.snake.size() - (growing ? 0 : 1);
for (int i = 0; i < limit; i++) {
if (g.snake[i] == nh) {
g.gameOver = true; g.running = false; soundGameOver(); return;
}
}
g.snake.push_front(nh);
if (growing) {
g.score += 10;
soundEat();
if (!spawnApple(g)) { g.gameWon = true; g.running = false; }
} else {
g.snake.pop_back();
}
}
// ─── Rendering ──────────────────────────────────────────────
void render(GameState &g) {
if (g.score != g.prevScore) {
g.scoreFlashTimer = FLASH_DURATION;
g.prevScore = g.score;
}
bool appleFlashing = g.appleFlashTimer > 0;
bool appleVisible = ((g.frameCount / APPLE_BLINK_HALF) % 2) == 0;
bool appleFlashBright = (g.appleFlashTimer > FLASH_DURATION / 2);
int headPhase = (g.frameCount / HEAD_GLOW_PERIOD) % 3;
int sparklePhase = (g.frameCount / APPLE_SPARKLE_RATE) % 3;
if (!g.paused) {
g.frameCount++;
if (g.appleFlashTimer > 0) g.appleFlashTimer--;
if (g.scoreFlashTimer > 0) g.scoreFlashTimer--;
}
std::fill(g.grid.begin(), g.grid.end(), ' ');
int bodyLen = (int)g.snake.size() - 1;
for (size_t i = 1; i < g.snake.size(); i++) {
int seg = (int)i - 1;
int zone = (bodyLen <= 0) ? 0 : (seg * 4 / bodyLen);
if (zone > 3) zone = 3;
g.grid[g.snake[i].y * g.boardWidth + g.snake[i].x] = (char)('a' + zone);
}
g.grid[g.snake.front().y * g.boardWidth + g.snake.front().x] = 'H';
g.grid[g.apple.y * g.boardWidth + g.apple.x] = '@';
std::string &buf = g.renderBuf;
buf.clear();
buf += "\033[1;1H";
int vbw = g.boardWidth * 2 + 4;
std::string hpad(g.offsetX, ' ');
char scoreCStr[32];
snprintf(scoreCStr, sizeof(scoreCStr), "Score: %d", g.score);
int scoreVisLen = (int)strlen(scoreCStr);
for (int r = 0; r < g.offsetY; r++) buf += ERASE_LINE "\n";
{
int pad = std::max(0, (g.termWidth - scoreVisLen) / 2);
for (int i = 0; i < pad; i++) buf += ' ';
if (g.scoreFlashTimer > 0) {
float ratio = (float)g.scoreFlashTimer / FLASH_DURATION;
if (ratio > 0.75f) buf += BOLD BRIGHT_WHITE;
else if (ratio > 0.5f) buf += BOLD BRIGHT_GREEN;
else if (ratio > 0.25f) buf += BOLD GREEN;
else buf += YELLOW;
} else {
buf += BOLD YELLOW;
}
buf += scoreCStr;
buf += RESET;
}
buf += ERASE_LINE "\n";
buf += hpad; buf += CYAN;
for (int i = 0; i < vbw; i++) buf += '#';
buf += RESET ERASE_LINE "\n";
for (int y = 0; y < g.boardHeight; y++) {
buf += hpad;
buf += CYAN "##" RESET;
int base = y * g.boardWidth;
for (int x = 0; x < g.boardWidth; x++) {
char c = g.grid[base + x];
switch (c) {
case 'H':
switch (headPhase) {
case 0: buf += BOLD BRIGHT_GREEN "OO" RESET; break;
case 1: buf += BOLD BRIGHT_CYAN "OO" RESET; break;
case 2: buf += BOLD BRIGHT_WHITE "OO" RESET; break;
}
break;
case 'a': buf += BOLD BRIGHT_GREEN "oo" RESET; break;
case 'b': buf += BRIGHT_GREEN "oo" RESET; break;
case 'c': buf += GREEN "oo" RESET; break;
case 'd': buf += DIM GREEN "oo" RESET; break;
case '@':
if (appleFlashing) {
if (appleFlashBright) buf += BOLD BRIGHT_WHITE "@@" RESET;
else buf += BOLD YELLOW "@@" RESET;
} else if (appleVisible) {
switch (sparklePhase) {
case 0: buf += BOLD RED "@@" RESET; break;
case 1: buf += BOLD YELLOW "**" RESET; break;
case 2: buf += BOLD BRIGHT_WHITE "##" RESET; break;
}
} else {
buf += DIM RED "@@" RESET;
}
break;
default: buf += " "; break;
}
}
buf += CYAN "##" RESET ERASE_LINE "\n";
}
buf += hpad; buf += CYAN;
for (int i = 0; i < vbw; i++) buf += '#';
buf += RESET ERASE_LINE "\n";
{
const char* t = "Move: WASD/HJKL/Arrows | P: Pause | R: Restart | Q: Menu";
int pad = std::max(0, (g.termWidth - (int)strlen(t)) / 2);
for (int i = 0; i < pad; i++) buf += ' ';
buf += CYAN; buf += t; buf += RESET;
}
buf += ERASE_LINE "\n";
buf += ERASE_BELOW;
if (g.paused) {
const char* pm = " PAUSED -- Press P to resume ";
int ml = (int)strlen(pm);
int cr = g.offsetY + 2 + g.boardHeight / 2;
int cc = g.offsetX + 3 + std::max(0, (g.boardWidth * 2 - ml) / 2);
if (cc < 1) cc = 1;
char pos[32];
snprintf(pos, sizeof(pos), "\033[%d;%dH", cr, cc);
buf += pos;
buf += BOLD YELLOW REVERSE;
buf += pm;
buf += RESET;
}
write(STDOUT_FILENO, buf.c_str(), buf.size());
}
// ─── Centering Helpers ──────────────────────────────────────
static std::string centerText(const std::string &s, int tw) {
int p = std::max(0, (tw - (int)s.size()) / 2);
return std::string(p, ' ') + s;
}
static std::string centerColorText(const std::string &s, int vl, int tw) {
int p = std::max(0, (tw - vl) / 2);
return std::string(p, ' ') + s;
}
static void flushInput() {
char d; fd_set fds; struct timeval tv;
while (true) {
FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds); tv = {0, 0};
if (select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv) <= 0) break;
read(STDIN_FILENO, &d, 1);
}
}
// ─── Start Menu ─────────────────────────────────────────────
AppState showStartMenu() {
flushInput();
clearScreen();
int sel = 0;
const int NOPTS = 3;
std::string buf;
buf.reserve(4096);
unsigned long frame = 0;
while (true) {
if (g_interrupted) return STATE_EXIT;
long long fs = nowMicros();
int tw, th; getTerminalSize(tw, th);
if (tw < MIN_TERM_W || th < MIN_TERM_H) return STATE_TOO_SMALL;
{
char c = 0;
while (true) {
fd_set fds; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds);
struct timeval tv = {0, 0};
if (select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv) <= 0) break;
if (read(STDIN_FILENO, &c, 1) != 1) break;
if (c == 'q' || c == 'Q') return STATE_EXIT;
if (c == '1') { soundMenuSelect(); return STATE_PLAYING; }
if (c == '2') { soundMenuSelect(); return STATE_LEADERBOARD; }
if (c == '\r' || c == '\n' || c == ' ') {
soundMenuSelect();
switch (sel) {
case 0: return STATE_PLAYING;
case 1: return STATE_LEADERBOARD;
case 2: return STATE_EXIT;
}
}
if (c == '\033') {
char seq[2] = {0, 0};
fd_set f2; struct timeval t2;
FD_ZERO(&f2); FD_SET(STDIN_FILENO, &f2); t2 = {0, 5000};
if (select(STDIN_FILENO + 1, &f2, nullptr, nullptr, &t2) > 0)
read(STDIN_FILENO, &seq[0], 1);
FD_ZERO(&f2); FD_SET(STDIN_FILENO, &f2); t2 = {0, 5000};
if (select(STDIN_FILENO + 1, &f2, nullptr, nullptr, &t2) > 0)
read(STDIN_FILENO, &seq[1], 1);
if (seq[0] == '[') {
int prev = sel;
if (seq[1] == 'A') sel = (sel - 1 + NOPTS) % NOPTS;
else if (seq[1] == 'B') sel = (sel + 1) % NOPTS;
if (sel != prev) soundMenuMove();
}
continue;
}
{
int prev = sel;
switch (c) {
case 'w': case 'W': case 'k': case 'K':
sel = (sel - 1 + NOPTS) % NOPTS; break;
case 's': case 'S': case 'j': case 'J':
sel = (sel + 1) % NOPTS; break;
}
if (sel != prev) soundMenuMove();
}
}
}
frame++;
int breathPhase = (frame / 20) % 3;
const char* breathAttr;
switch (breathPhase) {
case 0: breathAttr = DIM; break;
case 1: breathAttr = ""; break;
default: breathAttr = BOLD; break;
}
buf.clear();
buf += "\033[1;1H";
int menuH = 13;
int topPad = std::max(1, (th - menuH) / 2);
for (int i = 0; i < topPad; i++) buf += ERASE_LINE "\n";
std::string bline = "========================================";
int blVis = (int)bline.size();
std::string blCol = std::string(CYAN) + bline + RESET;
buf += centerColorText(blCol, blVis, tw) + ERASE_LINE "\n";
std::string titleText = "V S N A K E";
int titleVis = (int)titleText.size();
std::string titleCol = std::string(breathAttr) + BRIGHT_GREEN + titleText + RESET;
buf += centerColorText(titleCol, titleVis, tw) + ERASE_LINE "\n";
buf += centerColorText(blCol, blVis, tw) + ERASE_LINE "\n";
buf += ERASE_LINE "\n";
int decoPhase = (frame / 8) % 3;
std::string snakeHead;
switch (decoPhase) {
case 0: snakeHead = std::string(BOLD) + BRIGHT_GREEN + "O>" + RESET; break;
case 1: snakeHead = std::string(BOLD) + BRIGHT_CYAN + "O>" + RESET; break;
case 2: snakeHead = std::string(BOLD) + BRIGHT_WHITE + "O>" + RESET; break;
}
std::string deco = std::string(DIM) + GREEN + "~" + RESET
+ BRIGHT_GREEN + "o" + RESET
+ GREEN + "o" + RESET
+ BRIGHT_GREEN + "o" + RESET
+ GREEN + "o" + RESET
+ snakeHead;
buf += centerColorText(deco, 9, tw) + ERASE_LINE "\n";
buf += ERASE_LINE "\n";
const char* labels[] = {"Start Game", "Leaderboard", "Quit"};
const char* keys[] = {"1", "2", "Q"};
for (int i = 0; i < NOPTS; i++) {
char plain[48];
snprintf(plain, sizeof(plain), " %c [%s] %-14s",
(i == sel) ? '>' : ' ', keys[i], labels[i]);
int plen = (int)strlen(plain);
if (i == sel) {
std::string col = std::string(BOLD) + YELLOW + REVERSE + plain + RESET;
buf += centerColorText(col, plen, tw);
} else {
std::string col = std::string(CYAN) + "[" + keys[i] + "]" + RESET
+ " " + labels[i];
int vlen = 1 + (int)strlen(keys[i]) + 1 + 2 + (int)strlen(labels[i]);
buf += centerColorText(col, vlen, tw);
}
buf += ERASE_LINE "\n";
}
buf += ERASE_LINE "\n";
std::string footer = "Navigate: Arrows/WS Select: Enter/Space";
buf += centerColorText(std::string(DIM) + footer + RESET,
(int)footer.size(), tw);
buf += ERASE_LINE "\n";
buf += ERASE_BELOW;
write(STDOUT_FILENO, buf.c_str(), buf.size());
long long el = nowMicros() - fs;
long long sl = RENDER_TICK_US - el;
if (sl > 0) usleep(static_cast<useconds_t>(sl));
}
}
// ─── Leaderboard Screen ────────────────────────────────────
AppState showLeaderboardScreen() {
clearScreen();
auto scores = loadScores();
int tw, th; getTerminalSize(tw, th);
std::string border = std::string(CYAN) + "=====================================" + RESET;
std::string title = std::string(BOLD) + YELLOW + "L E A D E R B O A R D" + RESET;
std::string div = std::string(CYAN) + "-------------------------------------" + RESET;
std::string buf;
buf += "\n\n";
buf += centerColorText(border, 37, tw) + "\n";
buf += centerColorText(title, 21, tw) + "\n";
buf += centerColorText(border, 37, tw) + "\n\n";
int n = std::min((int)scores.size(), 10);
if (n == 0) {
buf += centerText("(no saved scores)", tw) + "\n";
} else {
for (int i = 0; i < n; i++) {
std::string rank = std::to_string(i + 1);
if (i < 9) rank = " " + rank;
std::string plain = rank + ". " + scores[i].timestamp
+ " | " + std::to_string(scores[i].score);
std::string col = std::string(CYAN) + rank + "." + RESET + " "
+ scores[i].timestamp + " "
+ CYAN + "|" + RESET + " "
+ YELLOW + std::to_string(scores[i].score) + RESET;
buf += centerColorText(col, (int)plain.size(), tw) + "\n";
}
}
buf += "\n";
buf += centerColorText(div, 37, tw) + "\n\n";
buf += centerColorText(std::string(BOLD) + GREEN + "Press [R] to Return to Menu" + RESET, 27, tw) + "\n";
buf += centerColorText(std::string(BOLD) + RED + "Press [Q] to Quit" + RESET, 17, tw) + "\n";
write(STDOUT_FILENO, buf.c_str(), buf.size());
flushInput();
while (true) {
if (g_interrupted) return STATE_EXIT;
fd_set fds; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds);
struct timeval tv = {0, 50000};
if (select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv) > 0) {
char c;
if (read(STDIN_FILENO, &c, 1) == 1) {
if (c == 'r' || c == 'R') return STATE_MENU;
if (c == 'q' || c == 'Q') return STATE_EXIT;
}
}
}
}
// ─── Post-Game Screens ──────────────────────────────────────
static AppState waitForMenuOrExit() {
flushInput();
while (true) {
if (g_interrupted) return STATE_EXIT;
fd_set fds; FD_ZERO(&fds); FD_SET(STDIN_FILENO, &fds);
struct timeval tv = {0, 50000};
if (select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv) > 0) {
char c;
if (read(STDIN_FILENO, &c, 1) == 1) {
if (c == 'r' || c == 'R') return STATE_MENU;
if (c == 'q' || c == 'Q') return STATE_EXIT;
}
}
}
}
void showEndScreen(int score, bool won) {
clearScreen();
saveScore(score);
auto scores = loadScores();
int tw, th; getTerminalSize(tw, th);
std::string titleText = won ? "Y O U W I N !" : "G A M E O V E R";
std::string titleCol = won
? (std::string(BOLD) + BRIGHT_GREEN + titleText + RESET)
: (std::string(BOLD) + RED + titleText + RESET);
std::string border = std::string(CYAN) + "=============================" + RESET;
std::string scoreLine = std::string(BOLD) + YELLOW + "Final Score: " + RESET
+ BRIGHT_WHITE + std::to_string(score) + RESET;
std::string scoreVis = "Final Score: " + std::to_string(score);
std::string div = std::string(CYAN) + "-----------------------------" + RESET;
std::string buf;
buf += "\n\n";
buf += centerColorText(border, 29, tw) + "\n";
buf += centerColorText(titleCol, (int)titleText.size(), tw) + "\n";
buf += centerColorText(border, 29, tw) + "\n\n";
buf += centerColorText(scoreLine, (int)scoreVis.size(), tw) + "\n\n";
buf += centerColorText(std::string(BOLD) + CYAN + "Top Scores:" + RESET, 11, tw) + "\n";
buf += centerColorText(div, 29, tw) + "\n";
int n = std::min((int)scores.size(), 10);
for (int i = 0; i < n; i++) {
std::string rank = std::to_string(i + 1);
if (i < 9) rank = " " + rank;
std::string plain = rank + ". " + scores[i].timestamp
+ " | " + std::to_string(scores[i].score);
std::string col = std::string(CYAN) + rank + "." + RESET + " "
+ scores[i].timestamp + " "
+ CYAN + "|" + RESET + " "
+ YELLOW + std::to_string(scores[i].score) + RESET;
buf += centerColorText(col, (int)plain.size(), tw) + "\n";
}
if (scores.empty()) buf += centerText("(no scores yet)", tw) + "\n";
buf += centerColorText(div, 29, tw) + "\n\n";
buf += centerColorText(std::string(BOLD) + GREEN + "Press [R] to Return to Menu" + RESET, 27, tw) + "\n";
buf += centerColorText(std::string(BOLD) + RED + "Press [Q] to Quit" + RESET, 17, tw) + "\n";
write(STDOUT_FILENO, buf.c_str(), buf.size());