From e4a470083ca909369a7d061528e3154f3916374c Mon Sep 17 00:00:00 2001 From: Diogo Medeiros Date: Tue, 2 Apr 2024 19:15:56 +0100 Subject: [PATCH] Update screen clearing functions --- src/hanabi/game.c | 12 ++++++++---- src/hanabi/interface.c | 13 ++++++++++--- src/hanabi/interface.h | 7 +++++-- src/hanabi/player.c | 2 +- src/lib/lab.c | 2 +- src/main.c | 10 ++++------ 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/hanabi/game.c b/src/hanabi/game.c index 540371e..b1c7224 100644 --- a/src/hanabi/game.c +++ b/src/hanabi/game.c @@ -51,7 +51,7 @@ void game_turn(game_t *const game) { if (game->player_first) { while (player_turn(game) == INVALID_ACTION) { - clear_screen(); + clear_menu_screen(); } enter_to_continue(); print_ui(game); @@ -65,7 +65,7 @@ void game_turn(game_t *const game) { print_ui(game); while (player_turn(game) == INVALID_ACTION) { - clear_screen(); + clear_menu_screen(); } enter_to_continue(); print_ui(game); @@ -104,7 +104,7 @@ void save_game(const game_t *const game, const char *const filename) { free(game_str); } -game_t* get_game_from_json(const cJSON *const game_json) { +game_t *get_game_from_json(const cJSON *const game_json) { // Create a new game game_t *game = malloc(sizeof(game_t)); // Get the properties of the game from the JSON object @@ -135,7 +135,11 @@ game_t *load_game(const char *const filename) { fseek(save, 0, SEEK_SET); char *game_str = malloc(size + 1); - fread(game_str, 1, size, save); + size_t read = fread(game_str, 1, size, save); + if (read != size) { + puts("Error reading save file."); + exit(1); + } game_str[size] = '\0'; fclose(save); diff --git a/src/hanabi/interface.c b/src/hanabi/interface.c index cca3187..e9ee27a 100644 --- a/src/hanabi/interface.c +++ b/src/hanabi/interface.c @@ -8,7 +8,14 @@ void enter_to_continue() { printfAt(120, 32, "Press enter to continue..."); while (getchar() != '\n') { } - clear_screen(); + clear_menu_screen(); +} + +void clear_screen() { + if (system("clear") != 0) { + puts("Error clearing the console."); + exit(1); + } } char *read_string(FILE *stream) { @@ -85,7 +92,7 @@ void print_game_start() { } void print_ui(const game_t *const game) { - system("clear"); + clear_screen(); print_hand(&game->bot_hand, 10, 5, true); print_hand(&game->player_hand, 10, 26, false); print_deck(&game->deck); @@ -213,7 +220,7 @@ void print_deck(const deck_t *deck) { } } -void clear_screen() { +void clear_menu_screen() { char spaces[SCREEN_WIDTH + 1]; // 80 spaces + null terminator memset(spaces, ' ', SCREEN_WIDTH); spaces[SCREEN_WIDTH] = '\0'; // Null terminate the string diff --git a/src/hanabi/interface.h b/src/hanabi/interface.h index bff7690..64c7da0 100644 --- a/src/hanabi/interface.h +++ b/src/hanabi/interface.h @@ -15,6 +15,9 @@ /// Prints a string and waits for the user to press enter. void enter_to_continue(); +/// Clear console screen. +void clear_screen(); + /// Read a character from a file stream. /// \param stream The file stream to read the character from. /// \return The character read from the file stream. @@ -37,8 +40,8 @@ void error_msg(const char *message); /// Print the game's logo to the screen. void print_game_start(); -/// Clear the screen. -void clear_screen(); +/// Clear the menu screen. +void clear_menu_screen(); /// Get the ANSI color for a given card color. /// \param color The color to get the ANSI color for. diff --git a/src/hanabi/player.c b/src/hanabi/player.c index 8576305..da9c1c5 100644 --- a/src/hanabi/player.c +++ b/src/hanabi/player.c @@ -27,7 +27,7 @@ int player_turn(game_t *const game) { } printfAt(120, 16, "\t Option: "); const int option = read_int(stdin); - clear_screen(); + clear_menu_screen(); switch (option) { case 1: diff --git a/src/lib/lab.c b/src/lib/lab.c index 2ef11f4..fbb875d 100644 --- a/src/lib/lab.c +++ b/src/lib/lab.c @@ -77,7 +77,7 @@ uint32_t generate_random_int(const uint32_t lower, const uint32_t upper) { const uint32_t num = lower + rand() % (upper - lower + 1); return num; #endif -#ifdef __APPLE__ || __MACH__ +#ifdef __APPLE__ // Generate a random number between lower and upper (inclusive) const uint32_t num = lower + arc4random_uniform(upper - lower + 1); return num; diff --git a/src/main.c b/src/main.c index 56a152a..92c68e2 100644 --- a/src/main.c +++ b/src/main.c @@ -23,7 +23,7 @@ int main(void) { puts("\n\t 4 - Exit the application"); printf("\n Option: "); const int option = read_int(stdin); - system("clear"); + clear_screen(); switch (option) { case 1: { play_game(); @@ -34,7 +34,7 @@ int main(void) { game_t *loaded_game = load_game("save.json"); if (loaded_game == NULL) { enter_to_continue(); - system("clear"); + clear_screen(); break; } game_loop(loaded_game); @@ -49,8 +49,6 @@ int main(void) { printf("%s", line); } fclose(rules); - enter_to_continue(); - system("clear"); break; } case 4: @@ -58,9 +56,9 @@ int main(void) { default: puts("\nThe option does not exist."); enter_to_continue(); - system("clear"); break; } + clear_screen(); } } @@ -76,7 +74,7 @@ void play_game() { printf("\n If you complete the fireworks show, you win the game."); printf("\n Good luck!\n"); enter_to_continue(); - system("clear"); + clear_screen(); printfAt(1, 1, "\n\n Let's start the game!\n"); printf("\n You will have to give your name to start the game.\n");