game_nb_solutions finished
This commit is contained in:
1 parent
8a5862e1c7
commit
52ecb029e8
3 files changed
+68
-77
No files matched your search
+4
-3
@@ -254,12 +254,13 @@ square ext_1sol_1x1[] = {
|
||||
};
|
||||
|
||||
square ext_2sol_2x1[] = {
|
||||
S_BLANK, S_BLANK /* row 0 */
|
||||
S_BLANK, /* row 0 */
|
||||
S_BLANK /* row 1*/
|
||||
};
|
||||
|
||||
square ext_2sol_2x2[] = {
|
||||
S_BLANK, S_BLACK1, /* row 0 */
|
||||
S_BLANK, S_BLANK /* row 1 */
|
||||
S_BLANK, S_BLANK, /* row 0 */
|
||||
S_BLANK, S_BLANK /* row 1 */
|
||||
};
|
||||
|
||||
square ext_4sol_3x3[] = {
|
||||
|
||||
+22
-21
@@ -1243,32 +1243,33 @@ bool test_game_nb_solutions(bool whoami, char** name) {
|
||||
*name = (char*)__func__;
|
||||
return false;
|
||||
}
|
||||
bool test1, test2, test3, test4;
|
||||
bool test1, test2, test3, test4, test5;
|
||||
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;
|
||||
}
|
||||
test1 = (game_nb_solutions(game_1sol_1x1) == 1);
|
||||
game_delete(game_1sol_1x1);
|
||||
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;
|
||||
}
|
||||
test2 = (game_nb_solutions(game_2sol_2x1) == 2);
|
||||
game_delete(game_2sol_2x1);
|
||||
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;
|
||||
}
|
||||
test3 = (game_nb_solutions(game_2sol_2x2) == 2);
|
||||
game_delete(game_2sol_2x2);
|
||||
game game_4sol_3x3 = game_new_ext(3, 3, ext_4sol_3x3, false);
|
||||
if (game_nb_solutions(game_4sol_3x3) == 4) {
|
||||
game_delete(game_4sol_3x3);
|
||||
test4 = true;
|
||||
}
|
||||
if (test1 && test2 && test3 && test4) {
|
||||
test4 = (game_nb_solutions(game_4sol_3x3) == 4);
|
||||
game_delete(game_4sol_3x3);
|
||||
game gNoSol = game_new_empty_ext(3, 3, false);
|
||||
game_set_square(gNoSol, 2, 2, S_BLACK4);
|
||||
test5 = (game_nb_solutions(gNoSol) == 0);
|
||||
game_delete(gNoSol);
|
||||
if (test1 && test2 && test3 && test4 && test5)
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
printf("test%u => %s\n", 1, test1 ? "SUCCESS" : "FAILURE");
|
||||
printf("test%u => %s\n", 2, test2 ? "SUCCESS" : "FAILURE");
|
||||
printf("test%u => %s\n", 3, test3 ? "SUCCESS" : "FAILURE");
|
||||
printf("test%u => %s\n", 4, test4 ? "SUCCESS" : "FAILURE");
|
||||
printf("test%u => %s\n", 5, test5 ? "SUCCESS" : "FAILURE");
|
||||
// printf("test%u => %s\n", 6, test6 ? "SUCCESS" : "FAILURE");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ********** USAGE ********** */
|
||||
|
||||
+42
-53
@@ -425,53 +425,35 @@ bool game_solve(game g) {
|
||||
return false;
|
||||
}
|
||||
|
||||
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*/
|
||||
if (game_solve(g) == false) {
|
||||
return 0;
|
||||
void delete_all_marks(game g) {
|
||||
for (uint i = 0; i < game_nb_rows(g); i++) {
|
||||
for (uint j = 0; j < game_nb_cols(g); j++) {
|
||||
if (game_is_marked(g, i, j))
|
||||
game_play_move(g, i, j, S_BLANK);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*copy de la solution dans le tableau*/
|
||||
t_games[*index] = game_copy(g);
|
||||
*index = (*index) + 1;
|
||||
|
||||
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*/
|
||||
fprintf(stderr, "Too much solutions\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/*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++) {
|
||||
tab[r] = (uint*)malloc(sizeof(uint) * 2);
|
||||
}
|
||||
uint size = 0; // nombre de lightbulb dans tab
|
||||
|
||||
/*listage est stock des emplacements des lightbulb dans tab*/
|
||||
for (uint i = 0; i < g->height; i++) {
|
||||
for (uint j = 0; j < g->width; j++) {
|
||||
if (game_get_state(g, i, j) == S_LIGHTBULB) {
|
||||
tab[size][0] = i;
|
||||
tab[size][1] = j;
|
||||
size++;
|
||||
void game_nb_solutions_aux(game g, game* t_games, uint* index, uint* ind_max) {
|
||||
game g_copy = game_copy(g);
|
||||
if (game_solve(g)) {
|
||||
if (*index == *ind_max) {
|
||||
(*ind_max) = (*ind_max) * 2;
|
||||
t_games = realloc(t_games, sizeof(game) * (*ind_max));
|
||||
}
|
||||
t_games[*index] = game_copy(g);
|
||||
(*index)++;
|
||||
for (uint i = 0; i < game_nb_rows(g); i++) {
|
||||
for (uint j = 0; j < game_nb_cols(g); j++) {
|
||||
if (game_is_lightbulb(g, i, j)) {
|
||||
game_play_move(g_copy, i, j, S_MARK);
|
||||
game_nb_solutions_aux(g_copy, t_games, index, ind_max);
|
||||
game_undo(g_copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*marquage dans la game de base des emplacements des lightbulbs 1 a 1 et appel recursif de sorte a tester toutes les possibilitées*/
|
||||
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, index, ind_max);
|
||||
game_delete(game_test);
|
||||
}
|
||||
game_delete(game_save);
|
||||
free(tab);
|
||||
return res;
|
||||
game_delete(g_copy);
|
||||
}
|
||||
|
||||
uint game_nb_solutions(cgame g) {
|
||||
@@ -479,20 +461,27 @@ uint game_nb_solutions(cgame g) {
|
||||
return 0;
|
||||
}
|
||||
uint index = 0;
|
||||
uint ind_max = 2048;
|
||||
game t_games[ind_max];
|
||||
uint max_tab = 8;
|
||||
uint res = 0;
|
||||
game* t_games = malloc(sizeof(game) * max_tab);
|
||||
game g_tmp = game_copy(g);
|
||||
uint nb = game_nb_solutions_aux(g_tmp, t_games, &index, &ind_max);
|
||||
game_nb_solutions_aux(g_tmp, t_games, &index, &max_tab);
|
||||
game_delete(g_tmp);
|
||||
uint res = nb;
|
||||
for (uint n = 0; n < nb; n++) {
|
||||
for (uint i = n + 1; i < nb; i++) {
|
||||
game g1 = t_games[n];
|
||||
game g2 = t_games[i];
|
||||
if (game_equal(g1, g2)) {
|
||||
res--;
|
||||
for (uint i = 0; i < index; i++) {
|
||||
delete_all_marks(t_games[i]);
|
||||
}
|
||||
for (uint i = 0; i < index; i++) {
|
||||
bool toadd = true;
|
||||
for (uint j = i + 1; j < index; j++) {
|
||||
if (game_equal(t_games[i], t_games[j])) {
|
||||
toadd = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (toadd)
|
||||
res++;
|
||||
game_delete(t_games[i]);
|
||||
}
|
||||
return (res);
|
||||
free(t_games);
|
||||
return res;
|
||||
}
|
||||
Reference in new issue
Block a user