diff --git a/CMakeLists.txt b/CMakeLists.txt index 09c7f15..617489c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,10 +19,10 @@ add_executable(game_test_nnunes game_test_nnunes.c) add_executable(history_test history_test.c) #les dépendances sont calculées automatiquement -target_link_libraries(game_text game) -target_link_libraries(game_test_thoguet game) -target_link_libraries(game_test_thperrier game) -target_link_libraries(game_test_nnunes game) +target_link_libraries(game_text game history) +target_link_libraries(game_test_thoguet game history) +target_link_libraries(game_test_thperrier game history) +target_link_libraries(game_test_nnunes game history) target_link_libraries(history_test game history) add_library (game game.c game_aux.c) diff --git a/game.c b/game.c index 440283c..f65b0be 100644 --- a/game.c +++ b/game.c @@ -2,54 +2,64 @@ #include #include +int max(int a, int b) { + if (a > b) + return a; + return b; +} + game game_new(square* squares) { game g = game_new_empty(); - for (int i = 0; i < DEFAULT_SIZE; i++) { - for (int j = 0; j < DEFAULT_SIZE; j++) { - g->tab_cell[i][j] = squares[DEFAULT_SIZE * i + j]; + for (int i = 0; i < g->height; i++) { + for (int j = 0; j < g->width; j++) { + g->tab_cell[i][j] = squares[g->width * i + j]; } } return g; } game game_new_empty(void) { + // alloc struct game g = (game)malloc(sizeof(struct game_s)); if (g == NULL) { fprintf(stderr, "not enough memory\n"); exit(EXIT_FAILURE); } - g->tab_cell = (square**)malloc(sizeof(square*) * DEFAULT_SIZE); - if (g->tab_cell == NULL) { - fprintf(stderr, "not enough memory\n"); - exit(EXIT_FAILURE); - } - for (int i = 0; i < DEFAULT_SIZE; i++) { - g->tab_cell[i] = (square*)malloc(sizeof(square) * DEFAULT_SIZE); - if (g->tab_cell[i] == NULL) { - fprintf(stderr, "not enough memory\n"); - exit(EXIT_FAILURE); - } - for (int j = 0; j < DEFAULT_SIZE; j++) { - g->tab_cell[i][j] = S_BLANK; - } - } + // set variable of struct g->wrapped = false; g->height = DEFAULT_SIZE; g->width = DEFAULT_SIZE; g->hist = NULL; + // alloc the tab with variables set before + g->tab_cell = (square**)malloc(sizeof(square*) * g->height); + if (g->tab_cell == NULL) { + fprintf(stderr, "not enough memory\n"); + exit(EXIT_FAILURE); + } + for (int i = 0; i < g->height; i++) { + g->tab_cell[i] = (square*)malloc(sizeof(square) * g->width); + if (g->tab_cell[i] == NULL) { + fprintf(stderr, "not enough memory\n"); + exit(EXIT_FAILURE); + } + for (int j = 0; j < g->width; j++) { + g->tab_cell[i][j] = S_BLANK; + } + } return g; } - +// TODO game game_copy(cgame g1) { game g2 = game_new_empty(); - for (int i = 0; i < DEFAULT_SIZE; i++) { - for (int j = 0; j < DEFAULT_SIZE; j++) { + for (int i = 0; i < g2->height; i++) { + for (int j = 0; j < g2->width; j++) { g2->tab_cell[i][j] = g1->tab_cell[i][j]; } } return g2; } +// TODO bool game_equal(cgame g1, cgame g2) { for (int i = 0; i < DEFAULT_SIZE; i++) { for (int j = 0; j < DEFAULT_SIZE; j++) { @@ -62,7 +72,7 @@ bool game_equal(cgame g1, cgame g2) { } void game_delete(game g) { - for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int i = 0; i < g->height; i++) { free(g->tab_cell[i]); if (g->tab_cell[i] != NULL) g->tab_cell[i] = NULL; @@ -145,9 +155,7 @@ bool game_has_error(cgame g, uint i, uint j) { } bool game_check_move(cgame g, uint i, uint j, square s) { - if (i >= DEFAULT_SIZE || j >= DEFAULT_SIZE || (s != S_BLANK && s != S_MARK && s != S_LIGHTBULB) || game_is_black(g, i, j)) - return false; - return true; + 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) { @@ -157,8 +165,8 @@ void game_play_move(game g, uint i, uint j, square s) { void delete_flags(game g) { // delete prev flags - for (uint i = 0; i < DEFAULT_SIZE; i++) { - for (uint j = 0; j < DEFAULT_SIZE; j++) { + for (uint i = 0; i < g->height; i++) { + for (uint j = 0; j < g->width; j++) { game_set_square(g, i, j, game_get_state(g, i, j)); } } @@ -176,17 +184,17 @@ void addF_LIGHTED(game g, uint i, uint j) { void game_update_flags(game g) { delete_flags(g); // update flags lighted and lightbulb error - for (int i = 0; i < DEFAULT_SIZE; i++) { - for (int j = 0; j < DEFAULT_SIZE; j++) { + for (int i = 0; i < g->height; i++) { + for (int j = 0; j < g->width; j++) { if (game_get_state(g, i, j) == S_LIGHTBULB) { // add FLIGHTED to the current lightbulb addF_LIGHTED(g, i, j); // add FLIGHTED on the 4 directions, when a wall is hit stop this direction, when another lightbulb is hit put the initial lighbulb on error int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1}; - for (int x = 1; x < DEFAULT_SIZE; x++) { + for (int x = 1; x < max(g->width, g->height); x++) { for (uint y = 0; y < 7; y = y + 2) { - if (tab[y] != tab[y + 1] && j + x * tab[y + 1] >= 0 && j + x * tab[y + 1] < DEFAULT_SIZE && i + x * tab[y] >= 0 && - i + x * tab[y] < DEFAULT_SIZE) { + if (tab[y] != tab[y + 1] && j + x * tab[y + 1] >= 0 && j + x * tab[y + 1] < g->width && i + x * tab[y] >= 0 && + i + x * tab[y] < g->height) { if (game_is_black(g, i + x * tab[y], j + x * tab[y + 1])) { tab[y] = 0; tab[y + 1] = 0; @@ -204,14 +212,14 @@ void game_update_flags(game g) { } } // update flags wall (need to wait all FLIGHTED being updated) - for (int i = 0; i < DEFAULT_SIZE; i++) { - for (int j = 0; j < DEFAULT_SIZE; j++) { + for (int i = 0; i < g->height; i++) { + for (int j = 0; j < g->width; j++) { if (game_is_black(g, i, j) && game_get_black_number(g, i, j) >= 0) { int lb = 0; int notempty = 0; int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1}; for (uint y = 0; y < 7; y = y + 2) { - if (j + tab[y + 1] >= 0 && j + tab[y + 1] < DEFAULT_SIZE && i + tab[y] >= 0 && i + tab[y] < DEFAULT_SIZE) { + if (j + tab[y + 1] >= 0 && j + tab[y + 1] < g->width && i + tab[y] >= 0 && i + tab[y] < g->height) { if (game_is_lightbulb(g, i + tab[y], j + tab[y + 1])) lb++; else if (!game_is_blank(g, i + tab[y], j + tab[y + 1]) || game_is_lighted(g, i + tab[y], j + tab[y + 1])) @@ -233,8 +241,8 @@ void game_update_flags(game g) { } bool game_is_over(cgame g) { - for (uint i = 0; i < DEFAULT_SIZE; i++) { - for (uint j = 0; j < DEFAULT_SIZE; j++) { + for (uint i = 0; i < g->height; i++) { + for (uint j = 0; j < g->width; j++) { if ((game_get_state(g, i, j) == S_BLANK && game_get_flags(g, i, j) == S_BLANK) || game_get_flags(g, i, j) == F_ERROR || game_get_flags(g, i, j) == (F_LIGHTED | F_ERROR)) return false; @@ -244,8 +252,8 @@ bool game_is_over(cgame g) { } void game_restart(game g) { - for (uint i = 0; i < DEFAULT_SIZE; i++) { - for (uint j = 0; j < DEFAULT_SIZE; j++) { + for (uint i = 0; i < g->height; i++) { + for (uint j = 0; j < g->width; j++) { if (game_get_state(g, i, j) == S_MARK || game_get_state(g, i, j) == S_LIGHTBULB) game_set_square(g, i, j, S_BLANK); } diff --git a/game.h b/game.h index 53cf9f6..fb82e34 100644 --- a/game.h +++ b/game.h @@ -8,8 +8,6 @@ #ifndef __GAME_H__ #define __GAME_H__ #include -#include "history.h" - /** * @brief Standard unsigned integer type. **/ @@ -59,11 +57,12 @@ typedef enum { /** square mask used in square enum */ #define A_MASK 0xFF +#include "history.h" struct game_s { square** tab_cell; bool wrapped; - uint height; - uint width; + uint height; // i in general + uint width; // j in general history hist; }; diff --git a/game_ext.c b/game_ext.c index a7dcde0..bb63417 100644 --- a/game_ext.c +++ b/game_ext.c @@ -3,11 +3,11 @@ #include game game_new_ext(uint nb_rows, uint nb_cols, square* squares, bool wrapping) { - game g = (game)malloc(sizeof(struct game_s)); + return; } game game_new_empty_ext(uint nb_rows, uint nb_cols, bool wrapping) { - return; + game g = (game)malloc(sizeof(struct game_s)); } uint game_nb_rows(cgame g) { diff --git a/game_test_thoguet.c b/game_test_thoguet.c index fc80d6c..47b40bc 100644 --- a/game_test_thoguet.c +++ b/game_test_thoguet.c @@ -80,8 +80,6 @@ bool test_game_new(void) { /* ******** game_print ******** */ -// est ce qu'il faut faire avec un tab invalide ? - bool test_game_print(void) { square tab[DEFAULT_SIZE * DEFAULT_SIZE]; square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU}; @@ -205,7 +203,7 @@ int checklightbulb(game g, uint i, uint j, bool wall) { break; } } - for (int i2 = i + 1; i2 < DEFAULT_SIZE; i2++) { + for (int i2 = i + 1; i2 < g->height; i2++) { flags = game_get_square(g, i2, j); if (flags == (S_LIGHTBULB | F_LIGHTED) || flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { return -1; @@ -223,7 +221,7 @@ int checklightbulb(game g, uint i, uint j, bool wall) { break; } } - for (int j2 = j + 1; j2 < DEFAULT_SIZE; j2++) { + for (int j2 = j + 1; j2 < g->width; j2++) { flags = game_get_square(g, i, j2); if (flags == (S_LIGHTBULB | F_LIGHTED) || flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { return -1; @@ -246,8 +244,8 @@ bool check_update(game g) { uint notempty; bool res; bool wall; - for (uint i = 0; i < DEFAULT_SIZE; i++) { - for (uint j = 0; j < DEFAULT_SIZE; j++) { + for (uint i = 0; i < g->height; i++) { + for (uint j = 0; j < g->width; j++) { if (game_is_lightbulb(g, i, j)) { square flags = game_get_flags(g, i, j); if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR) { @@ -270,7 +268,7 @@ bool check_update(game g) { } } wall = false; - for (int k = i + 1; k < DEFAULT_SIZE; k++) { + for (int k = i + 1; k < g->height; k++) { if (/*test if current case is a wall*/ game_is_black(g, k, j)) { wall = true; } else { @@ -298,7 +296,7 @@ bool check_update(game g) { } } wall = false; - for (int k = j + 1; k < DEFAULT_SIZE; k++) { + for (int k = j + 1; k < g->width; k++) { if (/*test if current case is a wall*/ game_is_black(g, i, k)) { wall = true; } else { @@ -324,7 +322,7 @@ bool check_update(game g) { } else { notempty++; } - if (i < DEFAULT_SIZE - 1) { + if (i < g->height - 1) { choice[size] = game_get_square(g, i + 1, j); size++; } else { @@ -336,7 +334,7 @@ bool check_update(game g) { } else { notempty++; } - if (j < DEFAULT_SIZE - 1) { + if (j < g->width - 1) { choice[size] = game_get_square(g, i, j + 1); size++; } else { @@ -376,19 +374,19 @@ bool check_update(game g) { // a > 2 = temps d'exec > 3 min et expo bool brutforce(game g, int a, bool* deldup) { // char c; - bool* delDupNext = (bool*)calloc(DEFAULT_SIZE * DEFAULT_SIZE, sizeof(bool)); + bool* delDupNext = (bool*)calloc(g->height * g->width, sizeof(bool)); if (delDupNext == NULL) { fprintf(stderr, "Not enought memory\n"); exit(EXIT_FAILURE); } square list[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU, S_BLANK}; - for (uint i = 0; i < DEFAULT_SIZE; i++) { - for (uint j = 0; j < DEFAULT_SIZE; j++) { + for (uint i = 0; i < g->height; i++) { + for (uint j = 0; j < g->width; j++) { // printf("state: %d i:%d j:%d\n",game_get_state(g,i,j),i,j); - if (deldup == NULL || !(deldup[i * DEFAULT_SIZE + j])) { + if (deldup == NULL || !(deldup[i * g->width + j])) { if (game_get_state(g, i, j) == S_BLANK) { if (delDupNext != NULL && a > 0) - delDupNext[i * DEFAULT_SIZE + j] = true; + delDupNext[i * g->width + j] = true; for (uint k = 0; k < 9; k++) { game_set_square(g, i, j, list[k]); // if (k == 0){ diff --git a/game_test_thperrier.c b/game_test_thperrier.c index bc77498..f6f7339 100644 --- a/game_test_thperrier.c +++ b/game_test_thperrier.c @@ -171,8 +171,8 @@ bool test_game_equal() { bool test_game_new_empty() { game g = game_new_empty(); assert(g); - for (int i = 0; i < DEFAULT_SIZE; i++) { - for (int j = 0; j < DEFAULT_SIZE; j++) { + for (int i = 0; i < g->height; i++) { + for (int j = 0; j < g->width; j++) { if (game_get_square(g, i, j) != S_BLANK) { game_delete(g); return false; @@ -186,7 +186,7 @@ bool test_game_new_empty() { /* ********** game_copy ********** */ bool test_game_copy() { game g1 = game_new_empty(); - for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int i = 0; i < g1->height; i++) { game_set_square(g1, i, 0, S_LIGHTBULB); game_set_square(g1, i, 1, S_MARK); game_set_square(g1, i, 2, S_BLACKU); @@ -202,8 +202,8 @@ bool test_game_copy() { game_delete(g2); return false; } - for (int i = 0; i < DEFAULT_SIZE; i++) { - for (int j = 0; j < DEFAULT_SIZE; j++) { + for (int i = 0; i < g2->height; i++) { + for (int j = 0; j < g2->width; j++) { game_set_square(g2, i, j, S_BLACK3); } } diff --git a/game_text.c b/game_text.c index 3648dc8..b798dcf 100644 --- a/game_text.c +++ b/game_text.c @@ -10,8 +10,8 @@ bool checkerr(cgame g, char taberr[]) { } uint err = 0; taberr[0] = '\0'; - for (int i = 0; i < DEFAULT_SIZE; i++) { - for (int j = 0; j < DEFAULT_SIZE; j++) { + for (int i = 0; i < g->height; i++) { + for (int j = 0; j < g->width; j++) { // check each case if (game_has_error(g, i, j)) { // if error append taberr with coo+, @@ -54,7 +54,7 @@ int main(void) { // test if game is over uint i; uint j; - char taberr[DEFAULT_SIZE * DEFAULT_SIZE * 4]; + char taberr[g->height * g->width * 4]; char c; // print game game_print(g); diff --git a/history.h b/history.h index 5f1864c..e88331e 100644 --- a/history.h +++ b/history.h @@ -1,6 +1,9 @@ #ifndef __HISTORY_H__ #define __HISTORY_H__ #include + +typedef struct s_history* history; + #include "game.h" struct s_history { @@ -11,8 +14,6 @@ struct s_history { struct s_history* prev; }; -typedef struct s_history* history; - /** * @brief fprintf not enought memory + exit **/