change in game_solve executable

This commit is contained in:
Mohamed Fatih committed 2022-03-07 14:40:43 +01:00
1 parent be34e56e5f
commit eac306feb1
1 file changed
+26 -15
+26 -15
View File
@@ -1,13 +1,18 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "game.h" #include "game.h"
#include "game_aux.h"
#include "game_ext.h" #include "game_ext.h"
#include "game_private.h" #include "game_private.h"
#include "game_tools.h" #include "game_tools.h"
void usage() { void usage(char* argv[]) {
fprintf(stderr, "problem with paramametre\n"); printf("Usage : %s -s <input> [<output>] => Find a solution of the game saved in file <input> and optionally save the solution to the file <output>.\n",
argv[0]);
printf(
" %s -c <input> [<output>] => Find the number of solution(s) of the game saved in the file <input> and optionally save this number in the file "
"<output>.\n",
argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@@ -15,47 +20,53 @@ int main(int argc, char* argv[]) {
// check if nb of param is correct // check if nb of param is correct
if (argc != 3 && argc != 4) { if (argc != 3 && argc != 4) {
// nb param is not correct // nb param is not correct
usage(); usage(argv);
} }
// get the name of the file and load it // get the name of the file and load it
char* file = argv[2];
char* option = argv[1]; char* option = argv[1];
char* file = argv[2];
// if option is -s // if option is -s
if (strcmp(option, "-s") == 0) { if (strcmp(option, "-s") == 0) {
game g1 = game_load(file); game g1 = game_load(file);
printf("Game successfully load.\n");
// check if there are any solutions // check if there are any solutions
bool solution = game_solve(g1); bool solution = game_solve(g1);
// if the game has not solutions EXIT // if the game has not solutions EXIT
if (solution == false) { if (solution == false) {
fprintf(stderr, "there is not solution"); fprintf(stderr, "There is no solution.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("A solution has been found :\n");
game_print(g1);
// IF THERE ARE 3 ARGUMENTS WE LOAD THE GAME // IF THERE ARE 3 ARGUMENTS WE LOAD THE GAME
if (argc == 4) { if (argc == 4) {
char* filename = argv[3]; char* filename = argv[3];
game_save(g1, filename); game_save(g1, filename);
printf("This game has been saved in the file named : %s.\n", filename);
} }
} }
// if the option is -c // if the option is -c
else if (strcmp(option, "-c") == 0) { else if (strcmp(option, "-c") == 0) {
// GET THE NB OF SOLUTIONS // GET THE NB OF SOLUTIONS
cgame g2 = game_load(file); cgame g2 = game_load(file);
uint nbsolutions = game_nb_solutions(g2); printf("Game successfully load.\n");
uint nbSolutions = game_nb_solutions(g2);
// IF THERE ARE 3 ARGUMENTs, I create a file with nb of solutions // IF THERE ARE 3 ARGUMENTs, I create a file with nb of solutions
printf("%u solution(s) has been found", nbSolutions);
if (argc == 4) { if (argc == 4) {
char* filename = argv[3]; char* filename = argv[3];
FILE* f = fopen(filename, "w"); FILE* f = fopen(filename, "w");
checkPointer((void*)f, "Couldn't open file\n"); checkPointer((void*)f, "Couldn't open file in game_solve executable\n");
test_output(fprintf(f, "%u\n", nbsolutions), 2, "Error in scan of file\n"); test_output(fprintf(f, "%u\n", nbSolutions), 2, "Error when printing in file in game_solve executable\n");
fclose(f); fclose(f);
printf(" this number has been saved in file named : %s", filename);
} }
printf("\n");
}
else {
usage(argv);
} }
// if argv[1] is neither -c nor -w
/*else {
printf("the first parametre must be '-s' if you want the solution of the game\n");
printf("or '-c' if you want the number of solutions\n");
exit(EXIT_FAILURE);
}*/
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }