fin du nb solution avec tests et sans fuite memoire non de dieu c'est bon

This commit is contained in:
valentin carmona committed 2022-03-08 20:53:17 +01:00
1 parent af562625d0
commit 579df2e2c7
4 files changed
+59 -11

No files matched your search

+14 -1
View File
@@ -249,8 +249,21 @@ square ext_hard_10x10[] = {
S_BLANK, S_BLANK, S_BLACK0, S_BLANK, S_BLANK, S_BLANK, S_BLACKU, S_BLANK, S_BLANK, S_BLANK /* row 10 */
};
square ext_1sol_1x1[] = {
S_BLANK /* row 0 */
};
square ext_2sol_2x1[] = {
S_BLANK, S_BLANK /* row 0 */
};
square ext_2sol_2x2[] = {
S_BLANK, S_BLACK1, /* row 0 */
S_BLANK, S_BLANK /* row 1 */
};
square ext_4sol_3x3[] = {
S_BLANK, S_BLANK, S_BLANK, /* row 0 */
S_BLANK, S_BLACK2, S_BLANK, /* row 1 */
S_BLANK, S_BLANK, S_BLANK /* row 3 */
};
};
+3
View File
@@ -36,6 +36,9 @@ extern char default_str[];
extern char ext_5x3w_str[];
extern char other_str[];
extern square ext_hard_10x10[];
extern square ext_1sol_1x1[];
extern square ext_2sol_2x1[];
extern square ext_2sol_2x2[];
extern square ext_4sol_3x3[];
#endif // __GAME_EXAMPLES_H__
+22 -2
View File
@@ -1243,12 +1243,32 @@ bool test_game_nb_solutions(bool whoami, char** name) {
*name = (char*)__func__;
return false;
}
bool test1, test2, test3, test4;
game game_1sol_1x1 = game_new_ext(1, 1, ext_1sol_1x1, false);
if (game_nb_solutions(game_1sol_1x1) == 1) {
game_delete(game_1sol_1x1);
test1 = true;
}
game game_2sol_2x1 = game_new_ext(2, 1, ext_2sol_2x1, false);
if (game_nb_solutions(game_2sol_2x1) == 2) {
game_delete(game_2sol_2x1);
test2 = true;
}
game game_2sol_2x2 = game_new_ext(2, 2, ext_2sol_2x2, false);
if (game_nb_solutions(game_2sol_2x2) == 2) {
game_delete(game_2sol_2x2);
test3 = true;
}
game game_4sol_3x3 = game_new_ext(3, 3, ext_4sol_3x3, false);
if (game_nb_solutions(game_4sol_3x3) != 4) {
if (game_nb_solutions(game_4sol_3x3) == 4) {
game_delete(game_4sol_3x3);
test4 = true;
}
if (test1 && test2 && test3 && test4) {
return true;
} else {
return false;
}
return true;
}
/* ********** USAGE ********** */
+20 -8
View File
@@ -425,7 +425,7 @@ bool game_solve(game g) {
return false;
}
uint game_nb_solutions_aux(game g, game* t_games) {
uint game_nb_solutions_aux(game g, game* t_games, uint index, uint ind_max) {
/*sauvegarde de la game de base*/
game game_save = game_copy(g);
/*application du game solve*/
@@ -434,11 +434,16 @@ uint game_nb_solutions_aux(game g, game* t_games) {
}
/*copy de la solution dans le tableau*/
t_games[0] = game_copy(g);
/*allocation de une place en plus dans le tableau t_game pour pouvoir ensuite utiliser en recursif la case d'apres*/
t_games = (game*)malloc(sizeof(game));
t_games[index] = game_copy(g);
index++;
/*inittialisation des différentes variables utilisé par la suite dans la fonction*/
if (index == ind_max) {
/*allocation de une place en plus dans le tableau t_game pour pouvoir ensuite utiliser en recursif la case d'apres*/
t_games = (game*)realloc(t_games, sizeof(game) * ind_max);
ind_max = ind_max * 2;
}
/*initialisation des différentes variables utilisé par la suite dans la fonction*/
uint res = 1; // resultat renvoyé par la fonction
uint** tab = (uint**)malloc(sizeof(uint*) * (g->height * g->width)); // tableau stockant l'emplacement des lightbulbs
for (uint r = 0; r < (g->height * g->width); r++) {
@@ -461,17 +466,21 @@ uint game_nb_solutions_aux(game g, game* t_games) {
for (uint s = 0; s < size; s++) {
game game_test = game_copy(game_save);
game_set_square(game_test, tab[s][0], tab[s][1], S_MARK);
res = res + game_nb_solutions_aux(game_test, &t_games[1]);
res = res + game_nb_solutions_aux(game_test, t_games, index, ind_max);
game_delete(game_test);
}
game_delete(game_save);
free(tab);
return res;
}
uint game_nb_solutions(cgame g) {
if (g == NULL) {
return 0;
}
game* t_games = (game*)malloc(sizeof(game));
game g_tmp = game_copy(g);
uint nb = game_nb_solutions_aux(g_tmp, t_games);
uint nb = game_nb_solutions_aux(g_tmp, t_games, 0, 2);
uint res = nb;
for (uint n = 0; n < nb; n++) {
for (uint i = n + 1; i < nb; i++) {
@@ -482,11 +491,14 @@ uint game_nb_solutions(cgame g) {
} else {
game_print(t_games[n]);
}
game_delete(g1);
game_delete(g2);
}
}
for (uint i = 0; i < nb; i++) {
game_print(t_games[i]);
}
game_delete(g_tmp);
free(t_games);
return (res);
}