Merge branch 'main' of gitlab.emi.u-bordeaux.fr:pt2/teams-s4/tm07/lightup-07c
This commit is contained in:
commit
aa3869427d
4 files changed
+71
-22
No files matched your search
+9
-2
@@ -7,9 +7,16 @@ int max(int a, int b) {
|
||||
return b;
|
||||
}
|
||||
|
||||
void checkPointer(void* p) {
|
||||
void checkPointer(void* p, char* errMessage) {
|
||||
if (!p) {
|
||||
fprintf(stderr, "Error on given pointer.\n");
|
||||
fprintf(stderr, "%s", errMessage);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void test_output(int i, int excepted, char* message) {
|
||||
if (i != excepted) {
|
||||
fprintf(stderr, message);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
+10
-1
@@ -26,6 +26,15 @@ int max(int a, int b);
|
||||
*
|
||||
* @param p any pointer
|
||||
*/
|
||||
void checkPointer(void* p);
|
||||
void checkPointer(void* p, char* errMessage);
|
||||
|
||||
/**
|
||||
* @brief test if i and excepted are the same else exit the prog with the message
|
||||
*
|
||||
* @param i
|
||||
* @param excepted
|
||||
* @param message to print in stderr
|
||||
*/
|
||||
void test_output(int i, int excepted, char* message);
|
||||
|
||||
#endif
|
||||
+2
-3
@@ -4,7 +4,6 @@
|
||||
#include "game.h"
|
||||
#include "game_ext.h"
|
||||
#include "game_private.h"
|
||||
#include "game_tools.c"
|
||||
#include "game_tools.h"
|
||||
|
||||
void usage() {
|
||||
@@ -47,8 +46,8 @@ int main(int argc, char* argv[]) {
|
||||
if (argc == 4) {
|
||||
char* filename = argv[3];
|
||||
FILE* f = fopen(filename, "w");
|
||||
checkPointer((void*)f);
|
||||
test_output(fprintf(f, "%u\n", nbsolutions), 2, "Error in scan of file");
|
||||
checkPointer((void*)f, "Couldn't open file\n");
|
||||
test_output(fprintf(f, "%u\n", nbsolutions), 2, "Error in scan of file\n");
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
+50
-16
@@ -6,25 +6,18 @@
|
||||
|
||||
#define NB_CHAR_HEADER_WITHTOUT_DIMENSIONS 4
|
||||
|
||||
void test_output(int i, int excepted, char* message) {
|
||||
if (i != excepted) {
|
||||
fprintf(stderr, message);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
game game_load(char* filename) {
|
||||
checkPointer((void*)filename);
|
||||
checkPointer((void*)filename, "NULL pointer filename on game_load\n");
|
||||
// ouvrons le fichier
|
||||
FILE* f = fopen(filename, "r");
|
||||
checkPointer((void*)f);
|
||||
checkPointer((void*)f, "Couldn't open file\n");
|
||||
// récuperer les valeurs dont on a beson pour créer le jeu initail
|
||||
uint nb_rows, nb_cols;
|
||||
int wrapping;
|
||||
test_output(fscanf(f, "%u %u %d\n", &nb_rows, &nb_cols, &wrapping), 3, "Error in scan of file");
|
||||
// créer le jeu initail
|
||||
game g1 = game_new_empty_ext(nb_rows, nb_cols, wrapping);
|
||||
checkPointer((void*)g1);
|
||||
checkPointer((void*)g1, "Couldn't create game\n");
|
||||
// récuperer les coups à jouer
|
||||
for (uint i = 0; i < nb_rows; i++) {
|
||||
for (uint j = 0; j < nb_cols; j++) {
|
||||
@@ -77,11 +70,11 @@ char get_char_from_state(cgame g, uint i, uint j) {
|
||||
}
|
||||
|
||||
void game_save(cgame g, char* filename) {
|
||||
checkPointer((void*)filename);
|
||||
checkPointer((void*)g);
|
||||
checkPointer((void*)filename, "NULL pointer filename on game_save\n");
|
||||
checkPointer((void*)g, "NULL pointer game g on game_save\n");
|
||||
// try create or open file named filename
|
||||
FILE* f = fopen(filename, "w");
|
||||
checkPointer((void*)f);
|
||||
checkPointer((void*)f, "Couldn't open file in game_save\n");
|
||||
// try to print the first line (game_nb_rows(g) / 10 + 1 = nb char of a number)
|
||||
test_output(fprintf(f, "%d %d %d\n", game_nb_rows(g), game_nb_cols(g), game_is_wrapping(g)),
|
||||
NB_CHAR_HEADER_WITHTOUT_DIMENSIONS + game_nb_rows(g) / 10 + 1 + game_nb_cols(g) / 10 + 1, "Couldn't print in file.\n");
|
||||
@@ -124,8 +117,49 @@ bool game_solve(game g) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
uint game_nb_solutions(cgame g) {
|
||||
if (g == NULL)
|
||||
|
||||
uint game_nb_solutions_aux(game g, game* games) {
|
||||
game game_save = game_copy(g);
|
||||
uint res = 1;
|
||||
if (game_solve(g) == false) {
|
||||
return 0;
|
||||
return 2;
|
||||
}
|
||||
games = (game*)calloc(sizeof(game));
|
||||
games[0] = game_copy(g);
|
||||
uint** tab = (uint**)malloc(sizeof(uint*) * (g->height * g->width));
|
||||
for (uint r = 0; r < (g->height * g->width); r++) {
|
||||
tab[r] = (uint*)malloc(sizeof(uint) * 2);
|
||||
}
|
||||
uint size = 0;
|
||||
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (uint s = 0; s < size; s++) {
|
||||
game game_test = game_copy(game_save);
|
||||
res = res + game_nb_solutions_aux(game_test, &games[1]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
uint game_nb_solutions(cgame g) {
|
||||
game* games = (game*)malloc(0);
|
||||
game g_tmp = game_copy(g);
|
||||
uint nb = game_nb_solutions_aux(g_tmp, games);
|
||||
uint res = nb;
|
||||
for (uint n = 0; n < nb; n++) {
|
||||
for (uint i = n; i < nb; i++) {
|
||||
game g1 = g_tmp[n];
|
||||
game g2 = g_tmp[i];
|
||||
if (game_equal(g1, g2)) {
|
||||
res--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (res);
|
||||
}
|
||||
Reference in new issue
Block a user