Small snake game in C
Commit 2df7e6a62a0d
tarball1 file changed+88-3
@@ -35,6 +35,7 @@ int main(int argc, char *args[]) {
3535
selected game_size = {WIDTH, HEIGHT};3636
3737
score leader_board[SCORE_NUMBER] = {};38
+ uint16_t score = 0;3839
3940
char *env;4041
char *folder_path;@@ -125,7 +126,7 @@ int main(int argc, char *args[]) {
125126
fgets(score_input, sizeof(score_input), score_file);126127
leader_board[i].points = string_to_num(score_input, 0, UINT16_MAX, leader_board[i].points);127128
}128
-129
+ fclose(score_file);129130
}130131
}131132
@@ -357,7 +358,6 @@ int main(int argc, char *args[]) {
357358
358359
bool paused = false;359360
360
- uint16_t score = 0;361361
362362
if ((too_small = draw_board(game_size, board, current_size, true, false, score)))363363
paused = true;@@ -471,6 +471,89 @@ end_free:
471471
472472
end_of_program:473473
474
+475
+ {476
+477
+ uint8_t i, j;478
+479
+ for (i = 0; i < SCORE_NUMBER; i++) {480
+ if (score > leader_board[i].points)481
+ break;482
+ }483
+484
+ if (i < SCORE_NUMBER) {485
+486
+487
+488
+ for (j = SCORE_NUMBER - 1; j > i; j--) {489
+ leader_board[j] = leader_board[j -1]; 490
+ }491
+492
+ leader_board[i].points = score;493
+ leader_board[i].name[0] = '\0';494
+495
+ printf("\033[2J\033[H");496
+497
+ for (i = 0; i < SCORE_NUMBER; i++) {498
+ printf("%-10s %05d\n", leader_board[i].name, leader_board[i].points);499
+ }500
+501
+ for (i = SCORE_NUMBER - j; i > 0; i--)502
+ printf("\033[%c", UP_CODE);503
+ printf("\r_");504
+ fflush(stdout);505
+506
+ uint8_t number_charcters = 0;507
+508
+ while(true) {509
+ char string[STRING_LEN] = {};510
+ ssize_t length_string = read(STDIN_FILENO, string, sizeof(string) - 1);511
+512
+ if (length_string > 0) {513
+ 514
+ if (string[0] == '\n'){515
+ leader_board[i].name[number_charcters] = '\0';516
+ break; 517
+ }518
+ 519
+ if (string[0] == 127 && number_charcters > 0) {520
+ printf("\033[%c \033[%c\033[%c_", LEFT_CODE, LEFT_CODE, LEFT_CODE);521
+ leader_board[i].name[--number_charcters] = '\0';522
+ }523
+524
+ if (isalnum(string[0]) && number_charcters < SCORE_LEN - 1) {525
+ printf("\033[%c%c_", LEFT_CODE, string[0]);526
+ leader_board[i].name[number_charcters++] = string[0];527
+ }528
+ }529
+ fflush(stdout);530
+ nanosleep(&milispeed, NULL);531
+ }532
+ } else {533
+ 534
+ printf("\033[2J\033[H");535
+536
+ for (i = 0; i < SCORE_NUMBER; i++) {537
+ printf("%-10s %05d\n", leader_board[i].name, leader_board[i].points);538
+ }539
+540
+541
+542
+ while(true) {543
+ char string[STRING_LEN] = {};544
+ ssize_t length_string = read(STDIN_FILENO, string, sizeof(string) - 1);545
+546
+ if (length_string > 0) {547
+ if (string[0] == '\n')548
+ break;549
+ }550
+ }551
+ }552
+}553
+554
+555
+556
+474557
tcsetattr(STDIN_FILENO, TCSANOW, &old);475558
printf("\033[?25h\n\n");476559
@@ -485,10 +568,12 @@ end_of_program:
485568
}486569
487570
{571
+572
+ FILE* score_file = fopen(score_path, "w");488573
uint8_t i;489574
490575
for (i = 0; i < SCORE_NUMBER; i++) {491
- printf("%-10s %05d\n", leader_board[i].name, leader_board[i].points);576
+ fprintf(score_file, "%s\n%i\n", leader_board[i].name, leader_board[i].points);492577
}493578
}