From f61621a9270c1180f56e8d536d5aab179622446f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophyl?= Date: Fri, 10 Dec 2021 02:14:55 +0100 Subject: [PATCH] opti + memory leak fixed --- game_aux.c | 43 ++++++++----------------------------------- game_text.c | 39 +++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 55 deletions(-) diff --git a/game_aux.c b/game_aux.c index cb2c983..91c2ba8 100644 --- a/game_aux.c +++ b/game_aux.c @@ -2,39 +2,13 @@ #include #include "game.h" -char charPrintCell(square sqState) { - if (sqState == S_BLANK) { - return ' '; - } - if ((sqState == S_LIGHTBULB) || - (sqState == (S_LIGHTBULB | F_ERROR)) | (sqState == (S_LIGHTBULB | F_LIGHTED)) | (sqState == (S_LIGHTBULB | F_ERROR | F_LIGHTED))) { - return '*'; - } - if ((sqState == S_MARK) || (sqState == (S_MARK | F_ERROR)) | (sqState == (S_MARK | F_LIGHTED))) { - return '-'; - } - if (sqState == S_BLACK0 || sqState == (S_BLACK0 | F_ERROR)) { - return '0'; - } - if (sqState == S_BLACK1 || sqState == (S_BLACK1 | F_ERROR)) { - return '1'; - } - if (sqState == S_BLACK2 || sqState == (S_BLACK2 | F_ERROR)) { - return '2'; - } - if (sqState == S_BLACK3 || sqState == (S_BLACK3 | F_ERROR)) { - return '3'; - } - if (sqState == S_BLACK4 || sqState == (S_BLACK4 | F_ERROR)) { - return '4'; - } - if (sqState == S_BLACKU || sqState == (S_BLACKU | F_ERROR)) { - return 'w'; - } - if (sqState == F_LIGHTED) { - return '.'; - } - return -1; +char charPrintCell(cgame g, uint i, uint j) { + square sqState = game_get_state(g, i, j); + square sqFlags = game_get_flags(g, i, j); + char tab[] = {' ', '*', '-', '?', '?', '?', '?', '?', '0', '1', '2', '3', '4', 'w', '?', '?', '.'}; + if (!sqState) + return tab[sqFlags]; + return tab[sqState]; } void game_print(cgame g) { @@ -52,7 +26,7 @@ void game_print(cgame g) { for (i = 0; i < DEFAULT_SIZE; i++) { printf("%u |", i); for (uint j = 0; j < DEFAULT_SIZE; j++) { - printf("%c", charPrintCell(game_get_square(g, i, j))); + printf("%c", charPrintCell(g, i, j)); } printf("|\n"); } @@ -88,6 +62,5 @@ game game_default_solution(void) { game_set_square(g, 5, 0, S_LIGHTBULB); game_set_square(g, 5, 5, S_LIGHTBULB); game_set_square(g, 6, 1, S_LIGHTBULB); - game_update_flags(g); return g; } \ No newline at end of file diff --git a/game_text.c b/game_text.c index 57adcb1..3648dc8 100644 --- a/game_text.c +++ b/game_text.c @@ -4,7 +4,7 @@ #include "game.h" #include "game_aux.h" -bool checkerr(game g, char taberr[]) { +bool checkerr(cgame g, char taberr[]) { if (taberr == NULL || g == NULL) { return -1; } @@ -74,36 +74,35 @@ int main(void) { game_print(g); } else if (c == 'q') { printf("Solution: \n"); - game_print(game_default_solution()); + game sol = game_default_solution(); + game_update_flags(sol); + game_print(sol); + game_delete(sol); + game_delete(g); printf("shame\n"); return EXIT_SUCCESS; } else if (c == 'l' || c == 'm' || c == 'b') { if (scanf(" %u %u", &i, &j) == 2) { - if (i < DEFAULT_SIZE && j < DEFAULT_SIZE && !game_is_black(g, i, j)) { - if (c == 'l') { - game_set_square(g, i, j, S_LIGHTBULB); - game_update_flags(g); - game_print(g); - } else if (c == 'm') { - game_set_square(g, i, j, S_MARK); - game_update_flags(g); - game_print(g); - } else if (c == 'b') { - game_set_square(g, i, j, S_BLANK); - game_update_flags(g); - game_print(g); - } - } else { + if (c == 'l' && game_check_move(g, i, j, S_LIGHTBULB)) { + game_play_move(g, i, j, S_LIGHTBULB); + game_print(g); + } else if (c == 'm' && game_check_move(g, i, j, S_MARK)) { + game_play_move(g, i, j, S_MARK); + game_print(g); + } else if (c == 'b' && game_check_move(g, i, j, S_BLANK)) { + game_play_move(g, i, j, S_BLANK); + game_print(g); + } else printf("Erreur: (,) invalide.\n"); - } - } else { - printf("Commande inconnue.\n"); } } else { printf("Commande inconnue.\n"); } + } else { + printf("Commande inconnue.\n"); } } printf("congratulation\n"); + game_delete(g); return EXIT_SUCCESS; } \ No newline at end of file