Skip to content

Commit

Permalink
Update screen clearing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Necas209 committed Apr 2, 2024
1 parent bc8aa59 commit b2efa72
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
12 changes: 8 additions & 4 deletions src/hanabi/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
13 changes: 10 additions & 3 deletions src/hanabi/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/hanabi/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/hanabi/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/lib/lab.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -49,18 +49,16 @@ int main(void) {
printf("%s", line);
}
fclose(rules);
enter_to_continue();
system("clear");
break;
}
case 4:
exit(0);
default:
puts("\nThe option does not exist.");
enter_to_continue();
system("clear");
break;
}
clear_screen();
}
}

Expand Down

0 comments on commit b2efa72

Please sign in to comment.