jag

snake_c

public

Small snake game in C

Star0
Watch0

Commit 7a93b7b4eede

tarball

Jaglekon <58274093+Jaglekon@users.noreply.github.com>2026-08-30 20:25 UTC

9 files changed+96-29
settings file it's own
Modified file: .gitignore+1 additions
@@ -1,2 +1,3 @@
11 snake
22 build
3+result
Modified file: CMakeLists.txt+1 additions
@@ -25,6 +25,7 @@ add_executable(snake
2525 source/logic.c
2626 source/draw.c
2727 source/parameters.c
28+ source/settings_file/settings_file.c
2829 )
2930
3031 target_link_libraries(snake PRIVATE
Modified file: flake.nix+1 additions
@@ -32,6 +32,7 @@
3232 draw.c \
3333 logic.c \
3434 parameters.c \
35+ settings_file/settings_file.c \
3536 -I${jag-parse-lib}/include \
3637 -L${jag-parse-lib}/lib \
3738 -ljag_parse \
Deleted file: result−1 deletions
@@ -1 +0,0 @@
1-/nix/store/l5qbga6hbp0qvaakc6vvs11fdr34fi3h-snake-1.0.0
Deleted file: source/file_edit/file_edit.c−13 deletions
@@ -1,13 +0,0 @@
1-#include "snake.h"
2-
3-
4-uint8_t make_folder(char *path) {
5-
6- errno = 0;
7- if (mkdir(path, 0755) && errno != EEXIST) {
8- return 0;
9- }
10-
11- return 1;
12-}
13-
Modified file: source/main.c+26 additions−15 deletions
@@ -3,12 +3,14 @@
33
44 int main(int argc, char *args[]) {
55
6+
67 /* command argument handling */
78 if (!parse_parameters(argc, args))
89 return 0;
910
1011 printf("\033[?1049h");
1112
13+
1214 /* game settings */
1315
1416 speed game_speed = AVERAGE;
@@ -16,6 +18,7 @@ int main(int argc, char *args[]) {
1618
1719 score leader_board[SCORE_NUMBER] = {};
1820
21+
1922 /* Handling Files to store settings & highscores */
2023
2124
@@ -33,36 +36,44 @@ int main(int argc, char *args[]) {
3336 return 1;
3437 }
3538
39+
40+ /* Settings file */
41+
3642 char *settings_path = malloc(strlen(folder_path) + strlen(SETTINGS_FILE) + 2);
3743 sprintf(settings_path, "%s/%s", folder_path, SETTINGS_FILE);
3844
3945
40- /* Reading the files */
41-
4246 {
43- FILE* setting_file = fopen(settings_path, "r");
44-
45- if (setting_file != NULL) {
46-
47- char setting_input[STRING_LEN];
4847
49- fgets(setting_input, sizeof(setting_input), setting_file);
50- game_speed = string_to_num(setting_input, 0, SPEED_L - 1, game_speed);
48+ setting_arr settings;
5149
52- fgets(setting_input, sizeof(setting_input), setting_file);
53- game_size.x = string_to_num(setting_input, MIN_SIZE, MAX_SIZE, game_size.x);
50+ if (!init_setting_arr(&settings, 3)) {
51+ printf("alloc issue\n");
52+ return 1;
53+ }
5454
55- fgets(setting_input, sizeof(setting_input), setting_file);
56- game_size.y = string_to_num(setting_input, MIN_SIZE, MAX_SIZE, game_size.y);
55+ settings.settings[0] = game_speed;
56+ settings.settings[1] = game_size.x;
57+ settings.settings[2] = game_size.y;
5758
58- fclose(setting_file);
59+ if (!read_settings(settings_path, settings)) {
60+ printf("Issue reading files\n");
61+ return 1;
5962 }
63+
64+ game_speed = settings.settings[0];
65+ game_size.x = settings.settings[1];
66+ game_size.y = settings.settings[2];
67+
68+ free_setting_arr(settings);
6069 }
6170
71+
72+ /* Highscore file */
73+
6274 char *score_path = malloc(strlen(folder_path) + strlen(SCORE_FILE) + 2);
6375 sprintf(score_path, "%s/%s", folder_path, SCORE_FILE);
6476
65-
6677 {
6778 FILE* score_file = fopen(score_path, "r");
6879
Added file: source/settings_file/settings_file.c+43 additions
@@ -0,0 +1,43 @@
1+#include "settings_file.h"
2+
3+uint8_t read_settings(char *settings_path, setting_arr set_arr) {
4+
5+ FILE* setting_file = fopen(settings_path, "r");
6+
7+ if (setting_file == NULL)
8+ return 0;
9+
10+ char setting_input[SET_LEN];
11+
12+ size_t i;
13+ for (i = 0; i < set_arr.length; i++) {
14+
15+ fgets(setting_input, sizeof(setting_input), setting_file);
16+
17+ char *endptr;
18+ int64_t new_num = strtoll(setting_input, &endptr, 10);
19+
20+ if (setting_input != endptr)
21+ set_arr.settings[i] = new_num;
22+
23+ }
24+
25+ fclose(setting_file);
26+
27+ return 1;
28+}
29+
30+
31+uint8_t init_setting_arr(setting_arr *set_arr, size_t length) {
32+ set_arr->length = length;
33+
34+ if (!(set_arr->settings = calloc(length, sizeof(int64_t))))
35+ return 0;
36+
37+ return 1;
38+}
39+
40+void free_setting_arr(setting_arr set_arr) {
41+ free(set_arr.settings);
42+}
43+
Added file: source/settings_file/settings_file.h+23 additions
@@ -0,0 +1,23 @@
1+#ifndef SETTINGS_FILE_H
2+#define SETTINGS_FILE_H
3+
4+#include <stdio.h>
5+#include <stdint.h>
6+#include <stdlib.h>
7+
8+#define SET_LEN 10
9+
10+typedef struct {
11+ int64_t *settings;
12+ size_t length;
13+} setting_arr;
14+
15+
16+uint8_t read_settings(char *settings_file, setting_arr set_arr);
17+
18+
19+uint8_t init_setting_arr(setting_arr *set_arr, size_t length);
20+
21+void free_setting_arr(setting_arr set_arr);
22+
23+#endif
Modified file: source/snake.h+1 additions
@@ -27,6 +27,7 @@
2727 #include <jag_parse.h>
2828 #include <jag_write.h>
2929
30+#include "settings_file/settings_file.h"
3031
3132 #define RIGHT_CODE 'C'
3233 #define LEFT_CODE 'D'