From 2d76a0fb32eabc71e9f5f4f9db0b983eb7f78b88 Mon Sep 17 00:00:00 2001 From: Thibault Perrier Date: Wed, 19 Jan 2022 10:11:01 +0100 Subject: [PATCH 1/5] cleaning game_test_thperrier --- game_test_thperrier.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/game_test_thperrier.c b/game_test_thperrier.c index 50e4749..58b76ed 100644 --- a/game_test_thperrier.c +++ b/game_test_thperrier.c @@ -53,10 +53,6 @@ bool test_game_is_black() { return false; } game_set_square(g, 0, 0, S_BLACKU); - if (!game_is_black(g, 0, 0)) { - game_delete(g); - return false; - } if (!((game_get_square(g, 0, 0) == S_BLACKU) && game_is_black(g, 0, 0))) { game_delete(g); return false; @@ -123,24 +119,33 @@ bool test_game_is_black() { /* ********** game_is_lightbulb ********** */ bool test_game_is_lightbulb() { game g = game_new_empty(); - game_set_square(g, 0, 0, S_LIGHTBULB); - if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_lightbulb(g, 0, 0)) { + if (game_is_lightbulb(g, 0, 0)) { game_delete(g); - return true; + return false; + } + game_set_square(g, 0, 0, S_LIGHTBULB); + if (!((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_lightbulb(g, 0, 0))) { + game_delete(g); + return false; } game_delete(g); - return false; + return true; } /* ********** game_is_blank ********** */ bool test_game_is_blank() { game g = game_new_empty(); + if (!game_is_blank(g, 0, 0)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK0); if (game_is_blank(g, 0, 0)) { game_delete(g); - return true; + return false; } game_delete(g); - return false; + return true; } /* ********** game_delete ********** */ From a10df5eabfdace7382fe17e46ba58bae712b46fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophyl=20Hoguet?= Date: Wed, 19 Jan 2022 10:12:53 +0100 Subject: [PATCH 2/5] patched game_text checkerrors --- game_text.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/game_text.c b/game_text.c index eb068b0..886c787 100644 --- a/game_text.c +++ b/game_text.c @@ -29,10 +29,13 @@ bool checkerrors(cgame g, char* stringError) { if (game_has_error(g, i, j)) { // if error append stringError with coordinate of the error, stringError[index_taberr] = i + CHAR0ASCII; + index_taberr++; stringError[index_taberr] = ':'; + index_taberr++; stringError[index_taberr] = j + CHAR0ASCII; + index_taberr++; stringError[index_taberr] = ' '; - index_taberr = index_taberr + NBCHARERR; + index_taberr++; } } } @@ -96,7 +99,7 @@ int main(int argc, char* argv[]) { while (!game_is_over(g)) { // print errors if (checkerrors(g, stringError)) - printf("Il y a un problème case(s): %s .\n", stringError); + printf("Il y a un problème case(s): %s\n", stringError); printf("Entrez une commande (h pour afficher l'aide): "); // scan the first command without args if (scanf(" %c", &c)) { From c66ad4e22c1365c0cefa09d34d091fa8f8576c2a Mon Sep 17 00:00:00 2001 From: Thibault Perrier Date: Wed, 19 Jan 2022 11:31:00 +0100 Subject: [PATCH 3/5] edit game_is_black + game_get_black_number --- game_test_thperrier.c | 119 ++++++++++++------------------------------ 1 file changed, 32 insertions(+), 87 deletions(-) diff --git a/game_test_thperrier.c b/game_test_thperrier.c index 58b76ed..553f736 100644 --- a/game_test_thperrier.c +++ b/game_test_thperrier.c @@ -15,31 +15,19 @@ bool test_dummy() { /* ********** game_get_black_number ********** */ bool test_game_get_black_number() { + square tab_square[] = {S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4}; game g = game_new_empty(); - game_set_square(g, 0, 0, S_BLACK0); - if (game_get_black_number(g, 0, 0) != 0) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK1); - if (game_get_black_number(g, 0, 0) != 1) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK2); - if (game_get_black_number(g, 0, 0) != 2) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK3); - if (game_get_black_number(g, 0, 0) != 3) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK4); - if (game_get_black_number(g, 0, 0) != 4) { - game_delete(g); - return false; + for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int j = 0; j < DEFAULT_SIZE; j++) { + // check for each case given in tab_square if the function return the expected result + for (int tab_index = 0; tab_index < (sizeof(tab_square) / sizeof(tab_square[0])); tab_index++) { + game_set_square(g, i, j, tab_square[tab_index]); + if (game_get_black_number(g, i, j) != tab_index) { + game_delete(g); + return false; + } + } + } } game_delete(g); return true; @@ -47,71 +35,28 @@ bool test_game_get_black_number() { /* ********** game_is_black ********** */ bool test_game_is_black() { + square tab_square[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU}; game g = game_new_empty(); - if (game_is_black(g, 0, 0)) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACKU); - if (!((game_get_square(g, 0, 0) == S_BLACKU) && game_is_black(g, 0, 0))) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK0); - if (!((game_get_square(g, 0, 0) == S_BLACK0) && game_is_black(g, 0, 0))) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK1); - if (!((game_get_square(g, 0, 0) == S_BLACK1) && game_is_black(g, 0, 0))) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK2); - if (!((game_get_square(g, 0, 0) == S_BLACK2) && game_is_black(g, 0, 0))) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK3); - if (!((game_get_square(g, 0, 0) == S_BLACK3) && game_is_black(g, 0, 0))) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK4); - if (!((game_get_square(g, 0, 0) == S_BLACK4) && game_is_black(g, 0, 0))) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_MARK); - if ((game_get_square(g, 0, 0) == S_MARK) && game_is_black(g, 0, 0)) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_MARK); - if ((game_get_square(g, 0, 0) == S_MARK) && game_is_black(g, 0, 0)) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_LIGHTBULB); - if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_black(g, 0, 0)) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_LIGHTBULB); - if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_black(g, 0, 0)) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, F_ERROR); - if ((game_get_square(g, 0, 0) == F_ERROR) && game_is_black(g, 0, 0)) { - game_delete(g); - return false; - } - game_set_square(g, 0, 0, F_LIGHTED); - if ((game_get_square(g, 0, 0) == F_LIGHTED) && game_is_black(g, 0, 0)) { - game_delete(g); - return false; + for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int j = 0; j < DEFAULT_SIZE; j++) { + // check for each case given in tab_square if the function return the expected result + for (int tab_index = 0; tab_index < (sizeof(tab_square) / sizeof(tab_square[0])); tab_index++) { + game_set_square(g, i, j, tab_square[tab_index]); + if (tab_square[tab_index] <= S_MARK) { + if (game_is_black(g, i, j)) { + game_delete(g); + return false; + } + } else { + if (!((game_get_square(g, i, j) == tab_square[tab_index]) && game_is_black(g, i, j))) { + game_delete(g); + return false; + } + } + } + } } + game_delete(g); return true; } From 8798d680a3185893c5d4e832f4272a9e1b4b9c2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophyl=20Hoguet?= Date: Wed, 19 Jan 2022 11:48:02 +0100 Subject: [PATCH 4/5] cleaning --- game.c | 12 +++++++++--- game_ext.c | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/game.c b/game.c index b16ab64..5aee299 100644 --- a/game.c +++ b/game.c @@ -51,6 +51,7 @@ game game_copy(cgame g1) { g2->tab_cell[i][j] = g1->tab_cell[i][j]; } } + // delete first allocated hist (by game new) of g2 history_delete_entire_history(g2->hist); g2->hist = history_copy(g1->hist); return g2; @@ -70,6 +71,7 @@ bool game_equal(cgame g1, cgame g2) { } void game_delete(game g) { + // free each tab of tabcell for (int i = 0; i < g->height; i++) { free(g->tab_cell[i]); g->tab_cell[i] = NULL; @@ -133,7 +135,7 @@ bool game_is_black(cgame g, uint i, uint j) { int game_get_black_number(cgame g, uint i, uint j) { if (game_get_state(g, i, j) == S_BLACKU) return -1; - return game_get_state(g, i, j) - 8; + return game_get_state(g, i, j) - S_BLACK; } bool game_is_marked(cgame g, uint i, uint j) { @@ -149,16 +151,19 @@ bool game_has_error(cgame g, uint i, uint j) { } bool game_check_move(cgame g, uint i, uint j, square s) { - return !(i >= g->height || j >= g->width || (s != S_BLANK && s != S_MARK && s != S_LIGHTBULB) || game_is_black(g, i, j)); + return (i < g->height && j < g->width && (s == S_BLANK || s == S_MARK || s == S_LIGHTBULB) && !game_is_black(g, i, j)); } void game_play_move(game g, uint i, uint j, square s) { + // shouldn't happen because we initalized in game new if (g->hist == NULL) { g->hist = history_create_empty(); g->hist = history_insert_first(g->hist, game_get_state(g, i, j), i, j); } else { - g->hist = history_next(history_insert_after(g->hist, g->hist, game_get_state(g, i, j), i, j)); + // to avoid redo after play g->hist = history_delete_all_after(g->hist, g->hist); + // get the last element of + g->hist = history_next(history_insert_after(g->hist, g->hist, game_get_state(g, i, j), i, j)); } game_set_square(g, i, j, s); game_update_flags(g); @@ -269,5 +274,6 @@ void game_restart(game g) { } game_update_flags(g); history_delete_entire_history(g->hist); + // insert f_error on first cell of histo. initalized to avoid bug on first move undo g->hist = history_insert_first(history_create_empty(), F_ERROR, 0, 0); } diff --git a/game_ext.c b/game_ext.c index 8f8e486..8d7eeb2 100644 --- a/game_ext.c +++ b/game_ext.c @@ -26,11 +26,13 @@ game game_new_empty_ext(uint nb_rows, uint nb_cols, bool wrapping) { g->width = nb_cols; g->hist = history_create_empty(); // alloc the tab with variables set before + // alloc first tab g->tab_cell = (square**)malloc(sizeof(square*) * g->height); if (g->tab_cell == NULL) { fprintf(stderr, "not enough memory\n"); exit(EXIT_FAILURE); } + // alloc each tab of tab_cell for (int i = 0; i < g->height; i++) { g->tab_cell[i] = (square*)calloc(g->width, sizeof(square)); if (g->tab_cell[i] == NULL) { @@ -38,6 +40,7 @@ game game_new_empty_ext(uint nb_rows, uint nb_cols, bool wrapping) { exit(EXIT_FAILURE); } } + // insert f_error on first cell of histo. initalized to avoid bug on first move undo g->hist = history_insert_first(g->hist, F_ERROR, 0, 0); return g; } From 1111782a04c876327ff9040cd7f961545843f594 Mon Sep 17 00:00:00 2001 From: Thibault Perrier Date: Wed, 19 Jan 2022 11:49:21 +0100 Subject: [PATCH 5/5] starting editing game_is_lightbulb --- game_test_thperrier.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/game_test_thperrier.c b/game_test_thperrier.c index 553f736..8f302c1 100644 --- a/game_test_thperrier.c +++ b/game_test_thperrier.c @@ -60,19 +60,28 @@ bool test_game_is_black() { game_delete(g); return true; } - +// changer l'intérieur de la boucle /* ********** game_is_lightbulb ********** */ bool test_game_is_lightbulb() { + square tab_square[] = {S_BLANK, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU}; game g = game_new_empty(); - if (game_is_lightbulb(g, 0, 0)) { - game_delete(g); - return false; - } game_set_square(g, 0, 0, S_LIGHTBULB); if (!((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_lightbulb(g, 0, 0))) { game_delete(g); return false; } + for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int j = 0; j < DEFAULT_SIZE; j++) { + // check for each case given in tab_square if the function return the expected result + for (int tab_index = 0; tab_index < (sizeof(tab_square) / sizeof(tab_square[0])); tab_index++) { + game_set_square(g, i, j, tab_square[tab_index]); + if (game_get_black_number(g, i, j) != tab_index) { + game_delete(g); + return false; + } + } + } + } game_delete(g); return true; }