Merge branch 'main' of gitlab.emi.u-bordeaux.fr:pt2/teams-s4/tm07/lightup-07c

This commit is contained in:
valentin carmona committed 2022-03-06 15:51:22 +01:00
commit 696e71f7fb
4 files changed
+27 -20

No files matched your search

+9 -2
View File
@@ -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
View File
@@ -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 -4
View File
@@ -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() {
@@ -13,7 +12,6 @@ void usage() {
}
int main(int argc, char* argv[]) {
// check if nb of param is correct
if (argc != 3 && argc != 4) {
// nb param is not correct
@@ -48,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);
}
}
+6 -13
View File
@@ -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");