Small snake game in C
Commit bc2264fa3d69
tarball3 files changed+92-38
@@ -207,3 +207,16 @@ uint8_t make_folder(char *path) {
207207
208208
return 1;209209
}210
+211
+int32_t string_to_num(char* string, int32_t min, int32_t max, int32_t number) {212
+ 213
+ char *endptr;214
+ int32_t new_num = strtol(string, &endptr, 10);215
+216
+ if (string != endptr) {217
+ if (new_num >= min && new_num <= max)218
+ return new_num;219
+ } 220
+221
+ return number;222
+}@@ -34,10 +34,10 @@ int main(int argc, char *args[]) {
3434
speed game_speed = AVERAGE;3535
selected game_size = {WIDTH, HEIGHT};3636
37
+ score leader_board[SCORE_NUMBER] = {};3738
3839
char *env;39
- char file_path[PATH_LEN];40
-40
+ char *folder_path;4141
4242
if ((env = getenv("XDG_DATA_HOME")) == NULL) {4343
if ((env = getenv("HOME")) == NULL) {@@ -45,69 +45,89 @@ int main(int argc, char *args[]) {
4545
return 1;4646
}4747
48
- char local[] = ".local";49
- char share[] = "share";50
-51
- 5248
uint8_t error = 0;5349
54
- snprintf(file_path, sizeof(file_path), "%s/%s", env, local);55
- error += make_folder(file_path);50
+ char local[] = "/.local";51
+ char share[] = "/share";52
+53
+ folder_path = malloc(strlen(env) + strlen(local) + 54
+ strlen(share) + strlen(SETTINGS_FOLDER) + 2);55
+56
+ sprintf(folder_path, "%s/.local", env);57
+ error += make_folder(folder_path);5658
57
- snprintf(file_path, sizeof(file_path), "%s/%s/%s", env, local, share);58
- error += make_folder(file_path);59
+ strcat(folder_path, "/share");60
+ error += make_folder(folder_path);5961
6062
if (error <= 1) {6163
printf("Failed to create Folder");6264
return 1;63
- }64
-65
- snprintf(file_path, sizeof(file_path), "%s/%s/%s/%s", env, local, share, SETTING_FILE);66
- 65
+ } 6766
} else {68
- snprintf(file_path, sizeof(file_path), "%s/%s", env, SETTING_FILE);67
+ folder_path = malloc(strlen(env) + strlen(SETTINGS_FOLDER) + 2);68
+ sprintf(folder_path, "%s", env);6969
}7070
71
+ sprintf(folder_path + strlen(folder_path), "/%s", SETTINGS_FOLDER);72
+ if (!make_folder(folder_path)) {73
+ printf("Failed to create Folder");74
+ return 1;75
+ }76
+77
+ char *settings_path = malloc(strlen(folder_path) + strlen(SETTINGS_FILE) + 2); 78
+ sprintf(settings_path, "%s/%s", folder_path, SETTINGS_FILE);79
+80
+7181
{72
- FILE* setting_file = fopen(file_path, "r");82
+ FILE* setting_file = fopen(settings_path, "r");7383
7484
if (setting_file != NULL) {7585
7686
char setting_input[STRING_LEN]; 77
- char *endptr;7887
7988
fgets(setting_input, sizeof(setting_input), setting_file);89
+ game_speed = string_to_num(setting_input, 0, SPEED_L - 1, game_speed);8090
81
- int32_t number = strtol(setting_input, &endptr, 10);91
+ fgets(setting_input, sizeof(setting_input), setting_file);92
+ game_size.x = string_to_num(setting_input, MIN_SIZE, MAX_SIZE, game_size.x);8293
83
- if (setting_input != endptr) {84
- if (number >= 0 && number < SPEED_L)85
- game_speed = number;86
- }94
+ fgets(setting_input, sizeof(setting_input), setting_file);95
+ game_size.y = string_to_num(setting_input, MIN_SIZE, MAX_SIZE, game_size.y);8796
97
+ fclose(setting_file);98
+ }99
+ }88100
89
- fgets(setting_input, sizeof(setting_input), setting_file);101
+ char *score_path = malloc(strlen(folder_path) + strlen(SCORE_FILE) + 2);102
+ sprintf(score_path, "%s/%s", folder_path, SCORE_FILE);90103
91
- number = strtol(setting_input, &endptr, 10);104
+ free(folder_path);92105
93
- if (setting_input != endptr) {94
- if (number >= MIN_SIZE && number <= MAX_SIZE)95
- game_size.x = number;96
- }106
+ {107
+ FILE* score_file = fopen(score_path, "r");97108
109
+ if (score_file != NULL) {98110
99
- fgets(setting_input, sizeof(setting_input), setting_file);111
+ char score_input[STRING_LEN];100112
101
- number = strtol(setting_input, &endptr, 10);113
+ uint8_t i;114
+ for (i = 0; i < SCORE_NUMBER; i++) {115
+ fgets(score_input, SCORE_LEN, score_file);116
+ if (score_input[0] != '\0') {117
+ uint8_t j;118
+ for(j = 0; score_input[j] != '\0'; j++) {119
+ if (score_input[j] == '\n')120
+ score_input[j] = ' ';121
+ }122
+ strcpy(leader_board[i].name, score_input);123
+ }102124
103
- if (setting_input != endptr) {104
- if (number >= MIN_SIZE && number <= MAX_SIZE)105
- game_size.y = number;125
+ fgets(score_input, sizeof(score_input), score_file);126
+ leader_board[i].points = string_to_num(score_input, 0, UINT16_MAX, leader_board[i].points);106127
}107128
108
- fclose(setting_file);109129
}110
- } 130
+ }111131
112132
/* Setting up terminal with termios */113133
@@ -459,10 +479,21 @@ end_of_program:
459479
460480
461481
{462
- FILE* setting_file = fopen(file_path, "w");482
+ FILE* setting_file = fopen(settings_path, "w");463483
464484
fprintf(setting_file, "%d\n%d\n%d", game_speed, game_size.x, game_size.y);465485
}486
+487
+ {488
+ uint8_t i;489
+490
+ for (i = 0; i < SCORE_NUMBER; i++) {491
+ printf("%-10s %05d\n", leader_board[i].name, leader_board[i].points);492
+ }493
+ }494
+495
+ free(settings_path);496
+ free(score_path);466497
467498
return 0;468499
}@@ -34,14 +34,18 @@
3434
#define MAX_SIZE 1003535
3636
#define STRING_LEN 2037
-#define PATH_LEN 20037
+38
+#define SCORE_LEN 1039
+#define SCORE_NUMBER 103840
3941
#define SPEED 50 /* in ms */4042
4143
#define MAX_LENGTH 10004244
#define STARTING_LENGTH 24345
44
-#define SETTING_FILE "snake_c.settings"46
+#define SETTINGS_FILE "settings"47
+#define SCORE_FILE "score"48
+#define SETTINGS_FOLDER "snake_c"4549
4650
typedef enum { NEW_GAME, SETTINGS, EXIT, MAIN_MENU } main_menu;4751
@@ -74,6 +78,10 @@ typedef struct snake {
7478
uint16_t length;7579
} snake;7680
81
+typedef struct score {82
+ char name[SCORE_LEN];83
+ uint16_t points;84
+} score;7785
7886
uint8_t update_snake (selected game_size, snake *my_snake, direction shift);7987
void update_field(snake my_snake, selected game_size, @@ -97,4 +105,6 @@ direction read_direction(char *string, bool wasd_enabled);
97105
98106
uint8_t make_folder(char *path);99107
108
+int32_t string_to_num(char* string, int32_t min, int32_t max, int32_t number);109
+100110
#endif