diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..1c5cae1 --- /dev/null +++ b/.clang-format @@ -0,0 +1,148 @@ +--- +Language: Cpp +# BasedOnStyle: Chromium +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: true +BinPackParameters: false +BraceWrapping: + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^' + Priority: 2 + - Regex: '^<.*\.h>' + Priority: 1 + - Regex: '^<.*' + Priority: 2 + - Regex: '.*' + Priority: 3 +IncludeIsMainRegex: '([-_](test|unittest))?$' +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 4 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Never +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Left +RawStringFormats: + - Language: Cpp + Delimiters: + - cc + - CC + - cpp + - Cpp + - CPP + - 'c++' + - 'C++' + CanonicalDelimiter: '' + BasedOnStyle: google + - Language: TextProto + Delimiters: + - pb + - PB + - proto + - PROTO + EnclosingFunctions: + - EqualsProto + - EquivToProto + - PARSE_PARTIAL_TEXT_PROTO + - PARSE_TEST_PROTO + - PARSE_TEXT_PROTO + - ParseTextOrDie + - ParseTextProtoOrDie + CanonicalDelimiter: '' + BasedOnStyle: google +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Auto +TabWidth: 4 +UseTab: ForIndentation +... + diff --git a/game.c b/game.c index c771ce4..9e8b10d 100644 --- a/game.c +++ b/game.c @@ -1,146 +1,108 @@ -#include -#include #include "game.h" +#include +#include -struct game_s{ - square ** tab_cell; +struct game_s { + square** tab_cell; }; -game game_new(square *squares){ - game g = (game)malloc(sizeof(game)); - if (g == NULL){ - fprintf(stderr, "not enough memory\n"); - exit (EXIT_FAILURE); - } - g->tab_cell = (square**)malloc(sizeof(square**)*DEFAULT_SIZE); - if (g->tab_cell == NULL){ - fprintf(stderr, "not enough memory\n"); - exit (EXIT_FAILURE); - } - for (int i = 0; i < DEFAULT_SIZE; i++){ - g->tab_cell[i] = (square*)malloc(sizeof(square*)*DEFAULT_SIZE); - if (g->tab_cell[i] == NULL){ - fprintf(stderr, "not enough memory\n"); - exit (EXIT_FAILURE); - } - } - return g; +game game_new(square* squares) { + game g = (game)malloc(sizeof(game)); + if (g == NULL) { + fprintf(stderr, "not enough memory\n"); + exit(EXIT_FAILURE); + } + g->tab_cell = (square**)malloc(sizeof(square**) * DEFAULT_SIZE); + if (g->tab_cell == NULL) { + fprintf(stderr, "not enough memory\n"); + exit(EXIT_FAILURE); + } + for (int i = 0; i < DEFAULT_SIZE; i++) { + g->tab_cell[i] = (square*)malloc(sizeof(square*) * DEFAULT_SIZE); + if (g->tab_cell[i] == NULL) { + fprintf(stderr, "not enough memory\n"); + exit(EXIT_FAILURE); + } + } + return g; } - -game game_new_empty(void){ - return; +game game_new_empty(void) { + return; } - -game game_copy(cgame g){ - return; +game game_copy(cgame g) { + return; } - -bool game_equal(cgame g1, cgame g2){ - return; +bool game_equal(cgame g1, cgame g2) { + return; } - -void game_delete(game g){ - return; +void game_delete(game g) { + return; } - - -void game_set_square(game g, uint i, uint j, square s){ - return; +void game_set_square(game g, uint i, uint j, square s) { + return; } - - -square game_get_square(cgame g, uint i, uint j){ - return; +square game_get_square(cgame g, uint i, uint j) { + return; } - - -square game_get_state(cgame g, uint i, uint j){ - return; +square game_get_state(cgame g, uint i, uint j) { + return; } - - -square game_get_flags(cgame g, uint i, uint j){ - return; +square game_get_flags(cgame g, uint i, uint j) { + return; } - - -bool game_is_blank(cgame g, uint i, uint j){ - return; +bool game_is_blank(cgame g, uint i, uint j) { + return; } - - -bool game_is_lightbulb(cgame g, uint i, uint j){ - return; +bool game_is_lightbulb(cgame g, uint i, uint j) { + return; } - - -bool game_is_black(cgame g, uint i, uint j){ - return; +bool game_is_black(cgame g, uint i, uint j) { + return; } - - -int game_get_black_number(cgame g, uint i, uint j){ - return; +int game_get_black_number(cgame g, uint i, uint j) { + return; } - - -bool game_is_marked(cgame g, uint i, uint j){ - return; +bool game_is_marked(cgame g, uint i, uint j) { + return; } - - -bool game_is_lighted(cgame g, uint i, uint j){ - return; +bool game_is_lighted(cgame g, uint i, uint j) { + return; } - - -bool game_has_error(cgame g, uint i, uint j){ - return; +bool game_has_error(cgame g, uint i, uint j) { + return; } - - - -bool game_check_move(cgame g, uint i, uint j, square s){ - return; +bool game_check_move(cgame g, uint i, uint j, square s) { + return; } - - -void game_play_move(game g, uint i, uint j, square s){ - return; +void game_play_move(game g, uint i, uint j, square s) { + return; } - - -void game_update_flags(game g){ - return; +void game_update_flags(game g) { + return; } - - -bool game_is_over(cgame g){ - return; +bool game_is_over(cgame g) { + return; } - - -void game_restart(game g){ - return; +void game_restart(game g) { + return; } - diff --git a/game_aux.c b/game_aux.c index 4c69b60..9168c56 100644 --- a/game_aux.c +++ b/game_aux.c @@ -2,91 +2,95 @@ #include #include "game.h" -char charPrintCell(square sqState){ - if (sqState == S_BLANK){ +char charPrintCell(square sqState) { + if (sqState == S_BLANK) { return ' '; } - if ((sqState == S_LIGHTBULB) || (sqState == (S_LIGHTBULB|F_ERROR)) | (sqState == (S_LIGHTBULB|F_LIGHTED)) | (sqState == (S_LIGHTBULB|F_ERROR|F_LIGHTED))){ + if ((sqState == S_LIGHTBULB) || + (sqState == (S_LIGHTBULB | F_ERROR)) | + (sqState == (S_LIGHTBULB | F_LIGHTED)) | + (sqState == (S_LIGHTBULB | F_ERROR | F_LIGHTED))) { return '*'; } - if ((sqState == S_MARK) || (sqState == (S_MARK|F_ERROR)) | (sqState == (S_MARK|F_LIGHTED))){ + if ((sqState == S_MARK) || + (sqState == (S_MARK | F_ERROR)) | (sqState == (S_MARK | F_LIGHTED))) { return '-'; } - if (sqState == S_BLACK0 || sqState == (S_BLACK0|F_ERROR)){ + if (sqState == S_BLACK0 || sqState == (S_BLACK0 | F_ERROR)) { return '0'; } - if (sqState == S_BLACK1 || sqState == (S_BLACK1|F_ERROR)){ + if (sqState == S_BLACK1 || sqState == (S_BLACK1 | F_ERROR)) { return '1'; } - if (sqState == S_BLACK2 || sqState == (S_BLACK2|F_ERROR)){ + if (sqState == S_BLACK2 || sqState == (S_BLACK2 | F_ERROR)) { return '2'; } - if (sqState == S_BLACK3 || sqState == (S_BLACK3|F_ERROR)){ + if (sqState == S_BLACK3 || sqState == (S_BLACK3 | F_ERROR)) { return '3'; } - if (sqState == S_BLACK4 || sqState == (S_BLACK4|F_ERROR)){ + if (sqState == S_BLACK4 || sqState == (S_BLACK4 | F_ERROR)) { return '4'; } - if (sqState == S_BLACKU || sqState == (S_BLACKU|F_ERROR)){ + if (sqState == S_BLACKU || sqState == (S_BLACKU | F_ERROR)) { return 'w'; } - if (sqState == F_LIGHTED){ + if (sqState == F_LIGHTED) { return '.'; } return -1; } -void game_print(cgame g){ - if (g != NULL){ +void game_print(cgame g) { + if (g != NULL) { uint i; printf(" 0"); - for (i = 1; i < DEFAULT_SIZE; i++){ - printf("%u",i); + for (i = 1; i < DEFAULT_SIZE; i++) { + printf("%u", i); } printf("\n -"); - for (i = 1; i < DEFAULT_SIZE; i++){ + for (i = 1; i < DEFAULT_SIZE; i++) { printf("-"); } printf("\n"); - for (i = 0; i < DEFAULT_SIZE; i++){ - printf("%u |",i); - for (uint j = 0; j < DEFAULT_SIZE; j++){ - printf("%c",charPrintCell(game_get_square(g,i,j))); + for (i = 0; i < DEFAULT_SIZE; i++) { + printf("%u |", i); + for (uint j = 0; j < DEFAULT_SIZE; j++) { + printf("%c", charPrintCell(game_get_square(g, i, j))); } printf("|\n"); } printf(" -"); - for (i = 1; i < DEFAULT_SIZE; i++){ + for (i = 1; i < DEFAULT_SIZE; i++) { printf("-"); } printf("\n"); } } -game game_default(void){ +game game_default(void) { game g = game_new_empty(); - game_set_square(g,0,2,S_BLACK1); - game_set_square(g,1,2,S_BLACK2); - game_set_square(g,2,5,S_BLACKU); - game_set_square(g,2,6,S_BLACK2); - game_set_square(g,4,0,S_BLACK1); - game_set_square(g,4,1,S_BLACKU); - game_set_square(g,5,4,S_BLACK2); - game_set_square(g,6,4,S_BLACKU); + game_set_square(g, 0, 2, S_BLACK1); + game_set_square(g, 1, 2, S_BLACK2); + game_set_square(g, 2, 5, S_BLACKU); + game_set_square(g, 2, 6, S_BLACK2); + game_set_square(g, 4, 0, S_BLACK1); + game_set_square(g, 4, 1, S_BLACKU); + game_set_square(g, 5, 4, S_BLACK2); + game_set_square(g, 6, 4, S_BLACKU); return g; } -game game_default_solution(void){ - game g = game_default(); - game_set_square(g,0,0,S_LIGHTBULB); - game_set_square(g,1,1,S_LIGHTBULB); - game_set_square(g,0,3,S_LIGHTBULB); - game_set_square(g,1,6,S_LIGHTBULB); - game_set_square(g,2,2,S_LIGHTBULB); - game_set_square(g,3,6,S_LIGHTBULB); - game_set_square(g,4,4,S_LIGHTBULB); - game_set_square(g,5,0,S_LIGHTBULB); - game_set_square(g,5,5,S_LIGHTBULB); - game_set_square(g,6,1,S_LIGHTBULB); - game_update_flags(g); - return g; +game game_default_solution(void) { + game g = game_default(); + game_set_square(g, 0, 0, S_LIGHTBULB); + game_set_square(g, 1, 1, S_LIGHTBULB); + game_set_square(g, 0, 3, S_LIGHTBULB); + game_set_square(g, 1, 6, S_LIGHTBULB); + game_set_square(g, 2, 2, S_LIGHTBULB); + game_set_square(g, 3, 6, S_LIGHTBULB); + game_set_square(g, 4, 4, S_LIGHTBULB); + game_set_square(g, 5, 0, S_LIGHTBULB); + game_set_square(g, 5, 5, S_LIGHTBULB); + game_set_square(g, 6, 1, S_LIGHTBULB); + game_update_flags(g); + return g; } \ No newline at end of file diff --git a/game_test_nnunes.c b/game_test_nnunes.c index 4cfb420..7cbe96b 100644 --- a/game_test_nnunes.c +++ b/game_test_nnunes.c @@ -1,398 +1,393 @@ +#include +#include #include #include -#include #include -#include #include "game.h" #include "game_aux.h" -bool test_dummy(void){ - return true; +bool test_dummy(void) { + return true; } -void usage(int argc, char *argv[]){ - fprintf(stderr, "Usage: %s [<...>]\n", argv[0]); - exit(EXIT_FAILURE); +void usage(int argc, char* argv[]) { + fprintf(stderr, "Usage: %s [<...>]\n", argv[0]); + exit(EXIT_FAILURE); } /* ************** game_get_square ************** */ -bool test_game_get_square(void){ - game g = game_new_empty(); - game_set_square(g, 0, 0, S_LIGHTBULB); - if (game_get_square(g, 0, 0) != S_LIGHTBULB){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLANK); - if (game_get_square(g, 0, 0) != S_BLANK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_MARK); - if (game_get_square(g, 0, 0) != S_MARK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK); - if (game_get_square(g, 0, 0) != S_BLACK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK0); - if (game_get_square(g, 0, 0) != S_BLACK0){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK1); - if (game_get_square(g, 0, 0) != S_BLACK1){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK2); - if (game_get_square(g, 0, 0) != S_BLACK2){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK3); - if (game_get_square(g, 0, 0) != S_BLACK3){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK4); - if (game_get_square(g, 0, 0) != S_BLACK4){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACKU); - if (game_get_square(g, 0, 0) != S_BLACKU){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_LIGHTBULB|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_LIGHTBULB|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLANK|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLANK|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_MARK|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_MARK|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLACK|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK0|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLACK0|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK1|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLACK1|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK2|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLACK2|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK3|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLACK3|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK4|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLACK4|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACKU|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLACKU|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_LIGHTBULB|F_LIGHTED)); - if (game_get_square(g, 0, 0) != (S_LIGHTBULB|F_LIGHTED)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLANK|F_LIGHTED)); - if (game_get_square(g, 0, 0) != (S_BLANK|F_LIGHTED)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_MARK|F_LIGHTED)); - if (game_get_square(g, 0, 0) != (S_MARK|F_LIGHTED)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_LIGHTBULB|F_LIGHTED|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_LIGHTBULB|F_LIGHTED|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLANK|F_LIGHTED|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_BLANK|F_LIGHTED|F_ERROR)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_MARK|F_LIGHTED|F_ERROR)); - if (game_get_square(g, 0, 0) != (S_MARK|F_LIGHTED|F_ERROR)){ - game_delete(g); - return false; - } - game_delete(g); - return true; +bool test_game_get_square(void) { + game g = game_new_empty(); + game_set_square(g, 0, 0, S_LIGHTBULB); + if (game_get_square(g, 0, 0) != S_LIGHTBULB) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLANK); + if (game_get_square(g, 0, 0) != S_BLANK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_MARK); + if (game_get_square(g, 0, 0) != S_MARK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK); + if (game_get_square(g, 0, 0) != S_BLACK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK0); + if (game_get_square(g, 0, 0) != S_BLACK0) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK1); + if (game_get_square(g, 0, 0) != S_BLACK1) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK2); + if (game_get_square(g, 0, 0) != S_BLACK2) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK3); + if (game_get_square(g, 0, 0) != S_BLACK3) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK4); + if (game_get_square(g, 0, 0) != S_BLACK4) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACKU); + if (game_get_square(g, 0, 0) != S_BLACKU) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_LIGHTBULB | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_LIGHTBULB | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLANK | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLANK | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_MARK | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_MARK | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLACK | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK0 | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLACK0 | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK1 | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLACK1 | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK2 | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLACK2 | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK3 | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLACK3 | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK4 | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLACK4 | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACKU | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLACKU | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_LIGHTBULB | F_LIGHTED)); + if (game_get_square(g, 0, 0) != (S_LIGHTBULB | F_LIGHTED)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLANK | F_LIGHTED)); + if (game_get_square(g, 0, 0) != (S_BLANK | F_LIGHTED)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_MARK | F_LIGHTED)); + if (game_get_square(g, 0, 0) != (S_MARK | F_LIGHTED)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_LIGHTBULB | F_LIGHTED | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLANK | F_LIGHTED | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_BLANK | F_LIGHTED | F_ERROR)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_MARK | F_LIGHTED | F_ERROR)); + if (game_get_square(g, 0, 0) != (S_MARK | F_LIGHTED | F_ERROR)) { + game_delete(g); + return false; + } + game_delete(g); + return true; } /* ************** game_get_state ************** */ -bool test_game_get_state(void){ - game g = game_new_empty(); - game_set_square(g, 0, 0, S_LIGHTBULB); - if (game_get_state(g, 0, 0) != S_LIGHTBULB){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLANK); - if (game_get_state(g, 0, 0) != S_BLANK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_MARK); - if (game_get_state(g, 0, 0) != S_MARK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK); - if (game_get_state(g, 0, 0) != S_BLACK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK0); - if (game_get_state(g, 0, 0) != S_BLACK0){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK1); - if (game_get_state(g, 0, 0) != S_BLACK1){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK2); - if (game_get_state(g, 0, 0) != S_BLACK2){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK3); - if (game_get_state(g, 0, 0) != S_BLACK3){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK4); - if (game_get_state(g, 0, 0) != S_BLACK4){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACKU); - if (game_get_state(g, 0, 0) != S_BLACKU){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_LIGHTBULB|F_ERROR)); - if (game_get_state(g, 0, 0) != S_LIGHTBULB){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLANK|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLANK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_MARK|F_ERROR)); - if (game_get_state(g, 0, 0) != S_MARK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLACK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK0|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLACK0){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK1|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLACK1){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK2|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLACK2){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK3|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLACK3){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACK4|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLACK4){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLACKU|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLACKU){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_LIGHTBULB|F_LIGHTED)); - if (game_get_state(g, 0, 0) != S_LIGHTBULB){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLANK|F_LIGHTED)); - if (game_get_state(g, 0, 0) != S_BLANK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_MARK|F_LIGHTED)); - if (game_get_state(g, 0, 0) != S_MARK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_LIGHTBULB|F_LIGHTED|F_ERROR)); - if (game_get_state(g, 0, 0) != S_LIGHTBULB){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_BLANK|F_LIGHTED|F_ERROR)); - if (game_get_state(g, 0, 0) != S_BLANK){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, (S_MARK|F_LIGHTED|F_ERROR)); - if (game_get_state(g, 0, 0) != S_MARK){ - game_delete(g); - return false; - } - game_delete(g); - return true; +bool test_game_get_state(void) { + game g = game_new_empty(); + game_set_square(g, 0, 0, S_LIGHTBULB); + if (game_get_state(g, 0, 0) != S_LIGHTBULB) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLANK); + if (game_get_state(g, 0, 0) != S_BLANK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_MARK); + if (game_get_state(g, 0, 0) != S_MARK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK); + if (game_get_state(g, 0, 0) != S_BLACK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK0); + if (game_get_state(g, 0, 0) != S_BLACK0) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK1); + if (game_get_state(g, 0, 0) != S_BLACK1) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK2); + if (game_get_state(g, 0, 0) != S_BLACK2) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK3); + if (game_get_state(g, 0, 0) != S_BLACK3) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK4); + if (game_get_state(g, 0, 0) != S_BLACK4) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACKU); + if (game_get_state(g, 0, 0) != S_BLACKU) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_LIGHTBULB | F_ERROR)); + if (game_get_state(g, 0, 0) != S_LIGHTBULB) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLANK | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLANK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_MARK | F_ERROR)); + if (game_get_state(g, 0, 0) != S_MARK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLACK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK0 | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLACK0) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK1 | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLACK1) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK2 | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLACK2) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK3 | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLACK3) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACK4 | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLACK4) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLACKU | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLACKU) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_LIGHTBULB | F_LIGHTED)); + if (game_get_state(g, 0, 0) != S_LIGHTBULB) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLANK | F_LIGHTED)); + if (game_get_state(g, 0, 0) != S_BLANK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_MARK | F_LIGHTED)); + if (game_get_state(g, 0, 0) != S_MARK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_LIGHTBULB | F_LIGHTED | F_ERROR)); + if (game_get_state(g, 0, 0) != S_LIGHTBULB) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_BLANK | F_LIGHTED | F_ERROR)); + if (game_get_state(g, 0, 0) != S_BLANK) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, (S_MARK | F_LIGHTED | F_ERROR)); + if (game_get_state(g, 0, 0) != S_MARK) { + game_delete(g); + return false; + } + game_delete(g); + return true; } /* ************** game_get_flags ************** */ -bool test_game_get_flags(void){ - game g = game_new_empty(); - game_set_square(g, 0, 0, S_BLANK|F_LIGHTED); - if (game_get_flags(g, 0, 0) != F_LIGHTED){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLANK|F_ERROR); - if (game_get_flags(g, 0, 0) != F_ERROR){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLANK|F_ERROR|F_LIGHTED); - if (game_get_flags(g, 0, 0) != (F_ERROR|F_LIGHTED)){ - game_delete(g); - return false; - } - game_delete(g); - return true; +bool test_game_get_flags(void) { + game g = game_new_empty(); + game_set_square(g, 0, 0, S_BLANK | F_LIGHTED); + if (game_get_flags(g, 0, 0) != F_LIGHTED) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLANK | F_ERROR); + if (game_get_flags(g, 0, 0) != F_ERROR) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLANK | F_ERROR | F_LIGHTED); + if (game_get_flags(g, 0, 0) != (F_ERROR | F_LIGHTED)) { + game_delete(g); + return false; + } + game_delete(g); + return true; } /* ************** game_is_marked ************** */ -bool test_game_is_marked(void){ - game g = game_new_empty(); - game_set_square(g, 0, 0, S_MARK); - if ((game_get_square(g, 0, 0) == S_MARK) && game_is_marked(g, 0, 0)){ - game_delete(g); - return true; - } - game_delete(g); - return false; +bool test_game_is_marked(void) { + game g = game_new_empty(); + game_set_square(g, 0, 0, S_MARK); + if ((game_get_square(g, 0, 0) == S_MARK) && game_is_marked(g, 0, 0)) { + game_delete(g); + return true; + } + game_delete(g); + return false; } /* ************** game_is_lighted ************** */ -bool test_game_is_lighted(void){ - game g = game_new_empty(); - game_set_square(g, 0, 0, S_BLANK|F_LIGHTED); - if ((game_get_square(g, 0, 0) == F_LIGHTED) && game_is_lighted(g, 0, 0)){ - game_delete(g); - return true; - } - game_set_square(g, 0, 0, S_BLANK|F_LIGHTED|F_ERROR); - if((game_get_flags(g, 0, 0) == (F_LIGHTED|F_ERROR)) && game_is_lighted(g, 0, 0)){ - game_delete(g); - return true; - } - game_delete(g); - return false; +bool test_game_is_lighted(void) { + game g = game_new_empty(); + game_set_square(g, 0, 0, S_BLANK | F_LIGHTED); + if ((game_get_square(g, 0, 0) == F_LIGHTED) && game_is_lighted(g, 0, 0)) { + game_delete(g); + return true; + } + game_set_square(g, 0, 0, S_BLANK | F_LIGHTED | F_ERROR); + if ((game_get_flags(g, 0, 0) == (F_LIGHTED | F_ERROR)) && + game_is_lighted(g, 0, 0)) { + game_delete(g); + return true; + } + game_delete(g); + return false; } /* ************** game_has_error ************** */ -bool test_game_has_error(void){ - game g = game_new_empty(); - game_set_square(g, 0, 0, S_BLANK|F_ERROR); - if((game_get_flags(g, 0, 0) == F_ERROR) && game_has_error(g, 0, 0)){ - game_delete(g); - return true; - } - game_set_square(g, 0, 0, S_BLANK|F_LIGHTED|F_ERROR); - if((game_get_flags(g, 0, 0) == (F_LIGHTED|F_ERROR)) && game_has_error(g, 0, 0)){ - game_delete(g); - return true; - } - game_delete(g); - return false; +bool test_game_has_error(void) { + game g = game_new_empty(); + game_set_square(g, 0, 0, S_BLANK | F_ERROR); + if ((game_get_flags(g, 0, 0) == F_ERROR) && game_has_error(g, 0, 0)) { + game_delete(g); + return true; + } + game_set_square(g, 0, 0, S_BLANK | F_LIGHTED | F_ERROR); + if ((game_get_flags(g, 0, 0) == (F_LIGHTED | F_ERROR)) && + game_has_error(g, 0, 0)) { + game_delete(g); + return true; + } + game_delete(g); + return false; } -int main(int argc, char * argv[]){ - if (argc != 2){ - usage(argc, argv); - } - fprintf(stderr, "=> Start test \"%s\"\n", argv[1]); - bool success = false; - if (strcmp("dummy", argv[1]) == 0) - success = test_dummy(); - else if (strcmp("game_get_square", argv[1]) == 0){ - success = test_game_get_square(); - } - else if (strcmp("game_get_state", argv[1]) == 0){ - success = test_game_get_state(); - } - else if (strcmp("game_get_flags", argv[1]) == 0){ - success = test_game_get_flags(); - } - else if (strcmp("game_is_marked", argv[1]) == 0){ - success = test_game_is_marked(); - } - else if (strcmp("game_is_lighted", argv[1]) == 0){ - success = test_game_is_lighted(); - } - else if (strcmp("game_has_error", argv[1]) == 0){ - success = test_game_has_error(); - } - else { - fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]); - exit(EXIT_FAILURE); - } - if (success){ - fprintf(stderr, "Test \"%s\" finished: SUCCESS\n", argv[1]); - return EXIT_SUCCESS; - } - else { - fprintf(stderr, "Test \"%s\" finished: FAILURE\n", argv[1]); - return EXIT_FAILURE; - } +int main(int argc, char* argv[]) { + if (argc != 2) { + usage(argc, argv); + } + fprintf(stderr, "=> Start test \"%s\"\n", argv[1]); + bool success = false; + if (strcmp("dummy", argv[1]) == 0) + success = test_dummy(); + else if (strcmp("game_get_square", argv[1]) == 0) { + success = test_game_get_square(); + } else if (strcmp("game_get_state", argv[1]) == 0) { + success = test_game_get_state(); + } else if (strcmp("game_get_flags", argv[1]) == 0) { + success = test_game_get_flags(); + } else if (strcmp("game_is_marked", argv[1]) == 0) { + success = test_game_is_marked(); + } else if (strcmp("game_is_lighted", argv[1]) == 0) { + success = test_game_is_lighted(); + } else if (strcmp("game_has_error", argv[1]) == 0) { + success = test_game_has_error(); + } else { + fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]); + exit(EXIT_FAILURE); + } + if (success) { + fprintf(stderr, "Test \"%s\" finished: SUCCESS\n", argv[1]); + return EXIT_SUCCESS; + } else { + fprintf(stderr, "Test \"%s\" finished: FAILURE\n", argv[1]); + return EXIT_FAILURE; + } } \ No newline at end of file diff --git a/game_test_thoguet.c b/game_test_thoguet.c index 24bd142..90eb189 100644 --- a/game_test_thoguet.c +++ b/game_test_thoguet.c @@ -1,27 +1,27 @@ +#include +#include #include #include -#include #include -#include #include "game.h" #include "game_aux.h" typedef unsigned int uint; /* ********** DUMMY ********** */ -bool test_dummy(void){ +bool test_dummy(void) { return EXIT_SUCCESS; } /* ******* game_is_over ******* */ -bool test_game_is_over(void){ +bool test_game_is_over(void) { game g = game_default_solution(); game_update_flags(g); assert(game_is_over(g)); - game_set_square(g,0,0,(S_LIGHTBULB|F_LIGHTED|F_ERROR)); + game_set_square(g, 0, 0, (S_LIGHTBULB | F_LIGHTED | F_ERROR)); assert(!game_is_over(g)); game_update_flags(g); - game_set_square(g,0,1,S_BLANK); + game_set_square(g, 0, 1, S_BLANK); assert(!game_is_over(g)); game_delete(g); g = game_default(); @@ -33,21 +33,21 @@ bool test_game_is_over(void){ /* ******* game_restart ******* */ -bool test_game_restart(void){ +bool test_game_restart(void) { game g = game_default(); game g1 = game_default_solution(); - game_set_square(g,1,5,S_BLACK4); - game_set_square(g1,1,5,S_BLACK4); - game_set_square(g,3,2,S_BLACK3); - game_set_square(g1,3,2,S_BLACK3); - game_set_square(g,0,1,S_LIGHTBULB); - game_set_square(g1,0,1,S_LIGHTBULB); - game_play_move(g,2,0,S_MARK); - game_play_move(g1,2,0,S_MARK); - assert(!game_equal(g,g1)); + game_set_square(g, 1, 5, S_BLACK4); + game_set_square(g1, 1, 5, S_BLACK4); + game_set_square(g, 3, 2, S_BLACK3); + game_set_square(g1, 3, 2, S_BLACK3); + game_set_square(g, 0, 1, S_LIGHTBULB); + game_set_square(g1, 0, 1, S_LIGHTBULB); + game_play_move(g, 2, 0, S_MARK); + game_play_move(g1, 2, 0, S_MARK); + assert(!game_equal(g, g1)); game_restart(g1); game_restart(g); - assert(game_equal(g,g1)); + assert(game_equal(g, g1)); game_delete(g); game_delete(g1); return EXIT_SUCCESS; @@ -55,16 +55,18 @@ bool test_game_restart(void){ /* ********* game_new ********* */ -bool test_game_new(void){ - square tab[DEFAULT_SIZE*DEFAULT_SIZE]; +bool test_game_new(void) { + square tab[DEFAULT_SIZE * DEFAULT_SIZE]; game g; - square list[] = {S_BLANK,S_LIGHTBULB,S_MARK,S_BLACK,S_BLACK0,S_BLACK1,S_BLACK2,S_BLACK3,S_BLACK4,S_BLACKU,F_ERROR}; - for (uint i = 0; i < sizeof(list)/sizeof(list[0]) ; i ++){ + square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK, + S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, + S_BLACK4, S_BLACKU, F_ERROR}; + for (uint i = 0; i < sizeof(list) / sizeof(list[0]); i++) { g = NULL; - for (uint j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){ + for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) { tab[j] = list[i]; } - printf("%d\n",i); + printf("%d\n", i); g = game_new(tab); assert(g); game_delete(g); @@ -76,15 +78,16 @@ bool test_game_new(void){ // est ce qu'il faut faire avec un tab invalide ? -bool test_game_print(void){ - square tab[DEFAULT_SIZE*DEFAULT_SIZE]; - square list[] = {S_BLANK,S_LIGHTBULB,S_MARK,S_BLACK0,S_BLACK1,S_BLACK2,S_BLACK3,S_BLACK4,S_BLACKU}; +bool test_game_print(void) { + square tab[DEFAULT_SIZE * DEFAULT_SIZE]; + square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, + S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU}; game g; - for (uint i = 0; i < sizeof(list)/sizeof(list[0]) ; i ++){ - for (uint j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){ + for (uint i = 0; i < sizeof(list) / sizeof(list[0]); i++) { + for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) { tab[j] = list[i]; } - printf("%d\n",i); + printf("%d\n", i); g = game_new(tab); game_print(g); game_delete(g); @@ -94,16 +97,28 @@ bool test_game_print(void){ /* ******* game_get_square ******* */ -bool test_game_get_square(void){ +bool test_game_get_square(void) { game g; - square tab[DEFAULT_SIZE*DEFAULT_SIZE]; - square list[] = {S_BLANK,S_LIGHTBULB,S_MARK,S_BLACK,S_BLACK0,S_BLACK1,S_BLACK2,S_BLACK3,S_BLACK4,S_BLACKU,F_ERROR}; - for (uint i = 0; i < sizeof(list)/sizeof(list[0]) ; i ++){ - for (uint j = 0; j < DEFAULT_SIZE*DEFAULT_SIZE; j++){ + square tab[DEFAULT_SIZE * DEFAULT_SIZE]; + square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK, + S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, + S_BLACK4, S_BLACKU, F_ERROR}; + for (uint i = 0; i < sizeof(list) / sizeof(list[0]); i++) { + for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) { tab[j] = list[i]; g = game_new(tab); - printf("[%d, %d] %d %d\n",j/(DEFAULT_SIZE),j-(j/(DEFAULT_SIZE))-(j/(DEFAULT_SIZE)*(DEFAULT_SIZE-1)),game_get_square(g,j/(DEFAULT_SIZE),j-(j/(DEFAULT_SIZE))-(j/(DEFAULT_SIZE)*(DEFAULT_SIZE-1))),tab[j]); - assert(game_get_square(g,j/(DEFAULT_SIZE),j-(j/(DEFAULT_SIZE))-(j/(DEFAULT_SIZE)*(DEFAULT_SIZE-1))) == tab[j]); + printf( + "[%d, %d] %d %d\n", j / (DEFAULT_SIZE), + j - (j / (DEFAULT_SIZE)) - + (j / (DEFAULT_SIZE) * (DEFAULT_SIZE - 1)), + game_get_square(g, j / (DEFAULT_SIZE), + j - (j / (DEFAULT_SIZE)) - + (j / (DEFAULT_SIZE) * (DEFAULT_SIZE - 1))), + tab[j]); + assert(game_get_square(g, j / (DEFAULT_SIZE), + j - (j / (DEFAULT_SIZE)) - + (j / (DEFAULT_SIZE) * + (DEFAULT_SIZE - 1))) == tab[j]); game_delete(g); } } @@ -112,23 +127,27 @@ bool test_game_get_square(void){ /* ******* game_default ******* */ -bool test_game_default(void){ +bool test_game_default(void) { game g = game_default(); - //a verif aps sur + // a verif aps sur assert(g); - square tab[49] = - { - /*X0 X1 X2 X3 X4 X5 X6*/ - /*Y 0*/ S_BLANK, S_BLANK, S_BLACK1, S_BLANK, S_BLANK, S_BLANK, S_BLANK, - /*Y 1*/ S_BLANK, S_BLANK, S_BLACK2, S_BLANK, S_BLANK, S_BLANK, S_BLANK, - /*Y 2*/ S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLACKU, S_BLACK2, - /*Y 3*/ S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, - /*Y 4*/ S_BLACK1, S_BLACKU, S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLANK, - /*Y 5*/ S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLACK2, S_BLANK, S_BLANK, - /*Y 6*/ S_BLANK, S_BLANK, S_BLANK, S_BLANK, S_BLACKU, S_BLANK, S_BLANK - }; + square tab[49] = {/*X0 X1 X2 X3 X4 X5 X6*/ + /*Y 0*/ S_BLANK, S_BLANK, S_BLACK1, S_BLANK, + S_BLANK, S_BLANK, S_BLANK, + /*Y 1*/ S_BLANK, S_BLANK, S_BLACK2, S_BLANK, + S_BLANK, S_BLANK, S_BLANK, + /*Y 2*/ S_BLANK, S_BLANK, S_BLANK, S_BLANK, + S_BLANK, S_BLACKU, S_BLACK2, + /*Y 3*/ S_BLANK, S_BLANK, S_BLANK, S_BLANK, + S_BLANK, S_BLANK, S_BLANK, + /*Y 4*/ S_BLACK1, S_BLACKU, S_BLANK, S_BLANK, + S_BLANK, S_BLANK, S_BLANK, + /*Y 5*/ S_BLANK, S_BLANK, S_BLANK, S_BLANK, + S_BLACK2, S_BLANK, S_BLANK, + /*Y 6*/ S_BLANK, S_BLANK, S_BLANK, S_BLANK, + S_BLACKU, S_BLANK, S_BLANK}; game g1 = game_new(tab); - assert(game_equal(g,g1)); + assert(game_equal(g, g1)); game_delete(g); game_delete(g1); return EXIT_SUCCESS; @@ -136,23 +155,23 @@ bool test_game_default(void){ /* *** game_default_solution *** */ -bool test_game_default_solution(void){ +bool test_game_default_solution(void) { game g = game_default_solution(); game g1 = game_default(); assert(g); - //solution - game_set_square(g1,0,0,S_LIGHTBULB); - game_set_square(g1,1,1,S_LIGHTBULB); - game_set_square(g1,0,3,S_LIGHTBULB); - game_set_square(g1,1,6,S_LIGHTBULB); - game_set_square(g1,2,2,S_LIGHTBULB); - game_set_square(g1,3,6,S_LIGHTBULB); - game_set_square(g1,4,4,S_LIGHTBULB); - game_set_square(g1,5,0,S_LIGHTBULB); - game_set_square(g1,5,5,S_LIGHTBULB); - game_set_square(g1,6,1,S_LIGHTBULB); + // solution + game_set_square(g1, 0, 0, S_LIGHTBULB); + game_set_square(g1, 1, 1, S_LIGHTBULB); + game_set_square(g1, 0, 3, S_LIGHTBULB); + game_set_square(g1, 1, 6, S_LIGHTBULB); + game_set_square(g1, 2, 2, S_LIGHTBULB); + game_set_square(g1, 3, 6, S_LIGHTBULB); + game_set_square(g1, 4, 4, S_LIGHTBULB); + game_set_square(g1, 5, 0, S_LIGHTBULB); + game_set_square(g1, 5, 5, S_LIGHTBULB); + game_set_square(g1, 6, 1, S_LIGHTBULB); game_update_flags(g1); - assert(game_equal(g,g1)); + assert(game_equal(g, g1)); game_delete(g); game_delete(g1); return EXIT_SUCCESS; @@ -160,207 +179,214 @@ bool test_game_default_solution(void){ /* **** game_update_flags **** */ -int checklightbulb(game g, uint i, uint j, bool wall){ - square flags = game_get_flags(g,i,j); - //square state = game_get_state(g,i,j); - //test if the current case is a lightbulb and if it has been well updated if true we can break the loop because every others cases wont be lighted by the bulb we are testing - if (game_is_lightbulb(g,i,j)){ - if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR){ - //printf("156\n"); +int checklightbulb(game g, uint i, uint j, bool wall) { + square flags = game_get_flags(g, i, j); + // square state = game_get_state(g,i,j); + // test if the current case is a lightbulb and if it has been well updated + // if true we can break the loop because every others cases wont be lighted + // by the bulb we are testing + if (game_is_lightbulb(g, i, j)) { + if (/*test if this lightbulb is well updated*/ flags == S_BLANK || + flags == F_ERROR) { + // printf("156\n"); return 0; } return -1; } - //if no wall(s) before test if the light of the lightbulb has been well updated - if (!wall){ - - //test if the current case is not lighted - if (flags != F_LIGHTED){ - //printf("170\n"); - return 0; + // if no wall(s) before test if the light of the lightbulb has been well + // updated + if (!wall) { + // test if the current case is not lighted + if (flags != F_LIGHTED) { + // printf("170\n"); + return 0; } - - } - else{ - if (/*test if the current case is lighted*/ flags == F_LIGHTED){ - //printf("i:%d j:%d\n",i,j); - for (int i2 = i-1 ; i2 >= 0; i2--){ - flags = game_get_square(g,i2,j); - //printf("i2:%d i2state:%d i2flags:%d\n",i2,state,flags); - if (flags == (S_LIGHTBULB|F_LIGHTED) || flags == (S_LIGHTBULB|F_LIGHTED|F_ERROR)){ + + } else { + if (/*test if the current case is lighted*/ flags == F_LIGHTED) { + // printf("i:%d j:%d\n",i,j); + for (int i2 = i - 1; i2 >= 0; i2--) { + flags = game_get_square(g, i2, j); + // printf("i2:%d i2state:%d i2flags:%d\n",i2,state,flags); + if (flags == (S_LIGHTBULB | F_LIGHTED) || + flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { return -1; } - if (flags != F_LIGHTED && flags != (F_LIGHTED|S_MARK)){ + if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) { break; } } - for (int i2 = i+1 ; i2 < DEFAULT_SIZE; i2++){ - flags = game_get_square(g,i2,j); - if (flags == (S_LIGHTBULB|F_LIGHTED) || flags == (S_LIGHTBULB|F_LIGHTED|F_ERROR)){ + for (int i2 = i + 1; i2 < DEFAULT_SIZE; i2++) { + flags = game_get_square(g, i2, j); + if (flags == (S_LIGHTBULB | F_LIGHTED) || + flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { return -1; } - if (flags != F_LIGHTED && flags != (F_LIGHTED|S_MARK)){ + if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) { break; } } - for (int j2 = j-1 ; j2 >= 0; j2--){ - flags = game_get_square(g,i,j2); - if (flags == (S_LIGHTBULB|F_LIGHTED) || flags == (S_LIGHTBULB|F_LIGHTED|F_ERROR)){ + for (int j2 = j - 1; j2 >= 0; j2--) { + flags = game_get_square(g, i, j2); + if (flags == (S_LIGHTBULB | F_LIGHTED) || + flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { return -1; } - if (flags != F_LIGHTED && flags != (F_LIGHTED|S_MARK)){ + if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) { break; } } - for (int j2 = j+1 ; j2 < DEFAULT_SIZE; j2++){ - flags = game_get_square(g,i,j2); - if (flags == (S_LIGHTBULB|F_LIGHTED) || flags == (S_LIGHTBULB|F_LIGHTED|F_ERROR)){ + for (int j2 = j + 1; j2 < DEFAULT_SIZE; j2++) { + flags = game_get_square(g, i, j2); + if (flags == (S_LIGHTBULB | F_LIGHTED) || + flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { return -1; } - if (flags != F_LIGHTED && flags != (F_LIGHTED|S_MARK)){ + if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) { break; } } - //printf("220\n"); + // printf("220\n"); return 0; } } return 1; } -bool check_update(game g){ +bool check_update(game g) { uint size; square choice[4]; uint lb; uint notempty; bool res; bool wall; - for (uint i = 0; i < DEFAULT_SIZE; i++){ - for (uint j = 0; j< DEFAULT_SIZE; j++){ - if (game_is_lightbulb(g,i,j)){ - square flags = game_get_flags(g,i,j); - if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR){ - //printf("196\n"); + for (uint i = 0; i < DEFAULT_SIZE; i++) { + for (uint j = 0; j < DEFAULT_SIZE; j++) { + if (game_is_lightbulb(g, i, j)) { + square flags = game_get_flags(g, i, j); + if (/*test if this lightbulb is well updated*/ flags == + S_BLANK || + flags == F_ERROR) { + // printf("196\n"); return false; } - //loop checking every case before i + // loop checking every case before i wall = false; - for (int k = i-1; k > 0; k--){ - if (/*test if current case is a wall*/ game_is_black(g,k,j)){ + for (int k = i - 1; k > 0; k--) { + if (/*test if current case is a wall*/ game_is_black(g, k, + j)) { wall = true; - } - else { - int tmp = checklightbulb(g,k,j,wall); - if (tmp == -1){ + } else { + int tmp = checklightbulb(g, k, j, wall); + if (tmp == -1) { break; } - if (!tmp){ + if (!tmp) { return tmp; } } } wall = false; - for (int k = i+1; k < DEFAULT_SIZE; k++){ - if (/*test if current case is a wall*/ game_is_black(g,k,j)){ + for (int k = i + 1; k < DEFAULT_SIZE; k++) { + if (/*test if current case is a wall*/ game_is_black(g, k, + j)) { wall = true; - } - else { - int tmp = checklightbulb(g,k,j,wall); - if (tmp == -1){ + } else { + int tmp = checklightbulb(g, k, j, wall); + if (tmp == -1) { break; } - if (!tmp){ + if (!tmp) { return tmp; } } } wall = false; - for (int k = j-1; k > 0; k--){ - if (/*test if current case is a wall*/ game_is_black(g,i,k)){ + for (int k = j - 1; k > 0; k--) { + if (/*test if current case is a wall*/ game_is_black(g, i, + k)) { wall = true; - } - else { - int tmp = checklightbulb(g,i,k,wall); - if (tmp == -1){ + } else { + int tmp = checklightbulb(g, i, k, wall); + if (tmp == -1) { break; } - if (!tmp){ + if (!tmp) { return tmp; } } } wall = false; - for (int k = j+1; k < DEFAULT_SIZE; k++){ - if (/*test if current case is a wall*/ game_is_black(g,i,k)){ + for (int k = j + 1; k < DEFAULT_SIZE; k++) { + if (/*test if current case is a wall*/ game_is_black(g, i, + k)) { wall = true; - } - else { - int tmp = checklightbulb(g,i,k,wall); - if (tmp == -1){ + } else { + int tmp = checklightbulb(g, i, k, wall); + if (tmp == -1) { break; } - if (!tmp){ + if (!tmp) { return tmp; } } } - } - else if (/*test if current case is a wall*/game_is_black(g,i,j)){ - if (game_get_black_number(g,i,j) != -1){ - //lookup for lightbulb and emptycells + } else if (/*test if current case is a wall*/ game_is_black(g, i, + j)) { + if (game_get_black_number(g, i, j) != -1) { + // lookup for lightbulb and emptycells size = 0; lb = 0; notempty = 0; res = true; - if (i > 0){ - choice[size] = game_get_square(g,i-1,j); + if (i > 0) { + choice[size] = game_get_square(g, i - 1, j); size++; - } - else{ + } else { notempty++; } - if (i < DEFAULT_SIZE-1){ - choice[size] = game_get_square(g,i+1,j); + if (i < DEFAULT_SIZE - 1) { + choice[size] = game_get_square(g, i + 1, j); size++; - } - else{ + } else { notempty++; } - if (j > 0){ - choice[size] = game_get_square(g,i,j-1); + if (j > 0) { + choice[size] = game_get_square(g, i, j - 1); size++; - } - else{ + } else { notempty++; } - if (j < DEFAULT_SIZE-1){ - choice[size] = game_get_square(g,i,j+1); + if (j < DEFAULT_SIZE - 1) { + choice[size] = game_get_square(g, i, j + 1); size++; - } - else{ + } else { notempty++; } - for (int l = 0 ; l < size ; l++){ - if (choice[l] == (S_LIGHTBULB|F_LIGHTED) || choice[l] == (S_LIGHTBULB|F_LIGHTED|F_ERROR)){ + for (int l = 0; l < size; l++) { + if (choice[l] == (S_LIGHTBULB | F_LIGHTED) || + choice[l] == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) { lb++; - } - else if (choice[l] != S_BLANK){ + } else if (choice[l] != S_BLANK) { notempty++; } } - if (lb > game_get_black_number(g,i,j)){ + if (lb > game_get_black_number(g, i, j)) { res = false; } - //look if there is enough empty cell around the wall - else if (notempty > abs(game_get_black_number(g,i,j)-4)){ + // look if there is enough empty cell around the wall + else if (notempty > + abs(game_get_black_number(g, i, j) - 4)) { res = false; } - if (game_get_flags(g,i,j) == F_ERROR){ + if (game_get_flags(g, i, j) == F_ERROR) { res = !res; } - if (!res){ - //printf("386\n"); - //printf("lb:%d notempty:%d i:%d j:%d state : %d, flags: %d\n",lb,notempty,i,j,game_get_state(g,i,j),game_get_flags(g,i,j)); + if (!res) { + // printf("386\n"); + // printf("lb:%d notempty:%d i:%d j:%d state : %d, + // flags: + // %d\n",lb,notempty,i,j,game_get_state(g,i,j),game_get_flags(g,i,j)); return res; } } @@ -371,34 +397,36 @@ bool check_update(game g){ } // a > 2 = temps d'exec > 3 min et expo -bool brutforce(game g,int a, bool * deldup){ +bool brutforce(game g, int a, bool* deldup) { // char c; - bool * delDupNext = (bool *) calloc(DEFAULT_SIZE*DEFAULT_SIZE,sizeof(bool)); - square list[] = {S_LIGHTBULB,S_MARK,S_BLACK0,S_BLACK1,S_BLACK2,S_BLACK3,S_BLACK4,S_BLACKU,S_BLANK}; - for (uint i = 0; i < DEFAULT_SIZE; i++){ - for (uint j = 0 ;j < DEFAULT_SIZE; j++){ - // printf("state: %d i:%d j:%d\n",game_get_state(g,i,j),i,j); - if (deldup == NULL || !(deldup[i*DEFAULT_SIZE+j])){ - if (game_get_state(g,i,j) == S_BLANK){ + bool* delDupNext = (bool*)calloc(DEFAULT_SIZE * DEFAULT_SIZE, sizeof(bool)); + square list[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, + S_BLACK3, S_BLACK4, S_BLACKU, S_BLANK}; + for (uint i = 0; i < DEFAULT_SIZE; i++) { + for (uint j = 0; j < DEFAULT_SIZE; j++) { + // printf("state: %d i:%d j:%d\n",game_get_state(g,i,j),i,j); + if (deldup == NULL || !(deldup[i * DEFAULT_SIZE + j])) { + if (game_get_state(g, i, j) == S_BLANK) { if (delDupNext != NULL && a > 0) - delDupNext[i*DEFAULT_SIZE+j] = true; - for (uint k = 0 ; k < 9; k++){ - game_set_square(g,i,j,list[k]); + delDupNext[i * DEFAULT_SIZE + j] = true; + for (uint k = 0; k < 9; k++) { + game_set_square(g, i, j, list[k]); // if (k == 0){ // game_print(g); // scanf("%c",&c); // } - if (a > 0){ - if (!brutforce(g,a-1,delDupNext)){ - //printf("341\n"); + if (a > 0) { + if (!brutforce(g, a - 1, delDupNext)) { + // printf("341\n"); free(delDupNext); return false; } } game_update_flags(g); - if (!check_update(g)){ + if (!check_update(g)) { game_print(g); - //printf("i:%d j:%d state : %d, flags: %d\n",i,j,game_get_state(g,i,j),game_get_flags(g,i,j)); + // printf("i:%d j:%d state : %d, flags: + // %d\n",i,j,game_get_state(g,i,j),game_get_flags(g,i,j)); free(delDupNext); return false; } @@ -411,9 +439,9 @@ bool brutforce(game g,int a, bool * deldup){ return true; } -bool test_game_update_flags(void){ +bool test_game_update_flags(void) { game g = game_default(); - if (!brutforce(g,1,NULL)){ + if (!brutforce(g, 1, NULL)) { game_delete(g); return EXIT_FAILURE; } @@ -423,72 +451,70 @@ bool test_game_update_flags(void){ /* ****** game_play_move ****** */ -bool test_game_play_move(void){ +bool test_game_play_move(void) { game g = game_new_empty(); game g1 = game_new_empty(); - game_play_move(g,0,0,S_LIGHTBULB); - assert(!game_equal(g,g1)); - game_set_square(g1,0,0,S_LIGHTBULB); - assert(!game_equal(g,g1)); + game_play_move(g, 0, 0, S_LIGHTBULB); + assert(!game_equal(g, g1)); + game_set_square(g1, 0, 0, S_LIGHTBULB); + assert(!game_equal(g, g1)); game_update_flags(g1); - assert(game_equal(g,g1)); - game_play_move(g,3,0,S_MARK); - assert(!game_equal(g,g1)); - game_set_square(g1,3,0,S_MARK); - assert(!game_equal(g,g1)); + assert(game_equal(g, g1)); + game_play_move(g, 3, 0, S_MARK); + assert(!game_equal(g, g1)); + game_set_square(g1, 3, 0, S_MARK); + assert(!game_equal(g, g1)); game_update_flags(g1); - assert(game_equal(g,g1)); - game_play_move(g,0,3,S_BLANK); - assert(game_equal(g,g1)); - game_set_square(g1,0,3,S_BLANK); - assert(!game_equal(g,g1)); + assert(game_equal(g, g1)); + game_play_move(g, 0, 3, S_BLANK); + assert(game_equal(g, g1)); + game_set_square(g1, 0, 3, S_BLANK); + assert(!game_equal(g, g1)); game_update_flags(g1); - assert(game_equal(g,g1)); + assert(game_equal(g, g1)); game_delete(g); game_delete(g1); return EXIT_SUCCESS; } - /* ***** game_check_move ***** */ -bool test_game_check_move(void){ +bool test_game_check_move(void) { game g = game_new_empty(); - assert(!game_check_move(g,10,10,S_BLANK)); - assert(!game_check_move(g,10,10,S_BLACK0)); - assert(!game_check_move(g,0,0,S_BLACK0)); - assert(!game_check_move(g,0,0,S_BLACK1)); - assert(!game_check_move(g,0,0,S_BLACK2)); - assert(!game_check_move(g,0,0,S_BLACK3)); - assert(!game_check_move(g,0,0,S_BLACK4)); - assert(!game_check_move(g,0,0,S_BLACKU)); - assert(!game_check_move(g,0,0,F_LIGHTED)); - assert(!game_check_move(g,0,0,F_ERROR)); - for (uint i = 0 ; i < S_BLACKU-S_BLACK0 ; i ++){ - game_set_square(g,0,0,S_BLACK0+i); - assert(!game_check_move(g,0,0,S_BLANK)); - assert(!game_check_move(g,0,0,S_MARK)); - assert(!game_check_move(g,0,0,S_LIGHTBULB)); + assert(!game_check_move(g, 10, 10, S_BLANK)); + assert(!game_check_move(g, 10, 10, S_BLACK0)); + assert(!game_check_move(g, 0, 0, S_BLACK0)); + assert(!game_check_move(g, 0, 0, S_BLACK1)); + assert(!game_check_move(g, 0, 0, S_BLACK2)); + assert(!game_check_move(g, 0, 0, S_BLACK3)); + assert(!game_check_move(g, 0, 0, S_BLACK4)); + assert(!game_check_move(g, 0, 0, S_BLACKU)); + assert(!game_check_move(g, 0, 0, F_LIGHTED)); + assert(!game_check_move(g, 0, 0, F_ERROR)); + for (uint i = 0; i < S_BLACKU - S_BLACK0; i++) { + game_set_square(g, 0, 0, S_BLACK0 + i); + assert(!game_check_move(g, 0, 0, S_BLANK)); + assert(!game_check_move(g, 0, 0, S_MARK)); + assert(!game_check_move(g, 0, 0, S_LIGHTBULB)); } - assert(game_check_move(g,1,0,S_BLANK)); - assert(game_check_move(g,1,0,S_MARK)); - assert(game_check_move(g,1,0,S_LIGHTBULB)); + assert(game_check_move(g, 1, 0, S_BLANK)); + assert(game_check_move(g, 1, 0, S_MARK)); + assert(game_check_move(g, 1, 0, S_LIGHTBULB)); game_delete(g); return EXIT_SUCCESS; } /* ********** USAGE ********** */ -int usage(int argc, char *argv[]) -{ - fprintf(stderr, "Usage: %s [<...>]\n", argv[0]); - exit(EXIT_FAILURE); +int usage(int argc, char* argv[]) { + fprintf(stderr, "Usage: %s [<...>]\n", argv[0]); + exit(EXIT_FAILURE); } /* ********** MAIN ********** */ -int main(int argc, char * argv[]){ - if (argc == 1){ +int main(int argc, char* argv[]) { + if (argc == 1) { usage(argc, argv); } fprintf(stderr, "=> Start test \"%s\"\n", argv[1]); @@ -516,16 +542,15 @@ int main(int argc, char * argv[]){ else if (strcmp("game_check_move", argv[1]) == 0) ok = test_game_check_move(); else { - fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]); - exit(EXIT_FAILURE); + fprintf(stderr, "Error: test \"%s\" not found!\n", argv[1]); + exit(EXIT_FAILURE); } // print test result - if (!ok){ - fprintf(stderr, "Test \"%s\" finished: SUCCESS\n", argv[1]); - return EXIT_SUCCESS; - } - else { - fprintf(stderr, "Test \"%s\" finished: FAILURE\n", argv[1]); - return EXIT_FAILURE; + if (!ok) { + fprintf(stderr, "Test \"%s\" finished: SUCCESS\n", argv[1]); + return EXIT_SUCCESS; + } else { + fprintf(stderr, "Test \"%s\" finished: FAILURE\n", argv[1]); + return EXIT_FAILURE; } } \ No newline at end of file diff --git a/game_test_thperrier.c b/game_test_thperrier.c index b397d03..86dbe81 100644 --- a/game_test_thperrier.c +++ b/game_test_thperrier.c @@ -1,272 +1,261 @@ -#include -#include -#include -#include #include +#include +#include +#include +#include #include "game.h" #include "game_aux.h" - /* ********** DUMMY ********** */ -bool test_dummy(){ - return true; +bool test_dummy() { + return true; } /* ********** game_get_black_number ********** */ -bool test_game_get_black_number(){ - game g = game_new_empty(); - game_set_square(g, 0, 0, S_BLACK0); - if (game_get_black_number(g, 0, 0) != 0){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK1); - if (game_get_black_number(g, 0, 0) != 1){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK2); - if (game_get_black_number(g, 0, 0) != 2){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK3); - if (game_get_black_number(g, 0, 0) != 3){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK4); - if (game_get_black_number(g, 0, 0) != 4){ - game_delete(g); - return false; - } - game_delete(g); - return true; +bool test_game_get_black_number() { + game g = game_new_empty(); + game_set_square(g, 0, 0, S_BLACK0); + if (game_get_black_number(g, 0, 0) != 0) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK1); + if (game_get_black_number(g, 0, 0) != 1) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK2); + if (game_get_black_number(g, 0, 0) != 2) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK3); + if (game_get_black_number(g, 0, 0) != 3) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK4); + if (game_get_black_number(g, 0, 0) != 4) { + game_delete(g); + return false; + } + game_delete(g); + return true; } /* ********** game_is_black ********** */ -bool test_game_is_black(){ - game g = game_new_empty(); - if (game_is_black(g, 0, 0)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACKU); - if (!game_is_black(g, 0, 0)){ - game_delete(g); - return false; - } - if (!((game_get_square(g, 0, 0) == S_BLACKU) && game_is_black(g, 0, 0))){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK0); - if (!((game_get_square(g, 0, 0) == S_BLACK0) && game_is_black(g, 0, 0))){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK1); - if (!((game_get_square(g, 0, 0) == S_BLACK1) && game_is_black(g, 0, 0))){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK2); - if (!((game_get_square(g, 0, 0) == S_BLACK2) && game_is_black(g, 0, 0))){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK3); - if (!((game_get_square(g, 0, 0) == S_BLACK3) && game_is_black(g, 0, 0))){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_BLACK4); - if (!((game_get_square(g, 0, 0) == S_BLACK4) && game_is_black(g, 0, 0))){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_MARK); - if ((game_get_square(g, 0, 0) == S_MARK) && game_is_black(g, 0, 0)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_MARK); - if ((game_get_square(g, 0, 0) == S_MARK) && game_is_black(g, 0, 0)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_LIGHTBULB); - if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_black(g, 0, 0)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, S_LIGHTBULB); - if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_black(g, 0, 0)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, F_ERROR); - if ((game_get_square(g, 0, 0) == F_ERROR) && game_is_black(g, 0, 0)){ - game_delete(g); - return false; - } - game_set_square(g, 0, 0, F_LIGHTED); - if ((game_get_square(g, 0, 0) == F_LIGHTED) && game_is_black(g, 0, 0)){ - game_delete(g); - return false; - } - game_delete(g); - return true; +bool test_game_is_black() { + game g = game_new_empty(); + if (game_is_black(g, 0, 0)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACKU); + if (!game_is_black(g, 0, 0)) { + game_delete(g); + return false; + } + if (!((game_get_square(g, 0, 0) == S_BLACKU) && game_is_black(g, 0, 0))) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK0); + if (!((game_get_square(g, 0, 0) == S_BLACK0) && game_is_black(g, 0, 0))) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK1); + if (!((game_get_square(g, 0, 0) == S_BLACK1) && game_is_black(g, 0, 0))) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK2); + if (!((game_get_square(g, 0, 0) == S_BLACK2) && game_is_black(g, 0, 0))) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK3); + if (!((game_get_square(g, 0, 0) == S_BLACK3) && game_is_black(g, 0, 0))) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_BLACK4); + if (!((game_get_square(g, 0, 0) == S_BLACK4) && game_is_black(g, 0, 0))) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_MARK); + if ((game_get_square(g, 0, 0) == S_MARK) && game_is_black(g, 0, 0)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_MARK); + if ((game_get_square(g, 0, 0) == S_MARK) && game_is_black(g, 0, 0)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_LIGHTBULB); + if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_black(g, 0, 0)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, S_LIGHTBULB); + if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_black(g, 0, 0)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, F_ERROR); + if ((game_get_square(g, 0, 0) == F_ERROR) && game_is_black(g, 0, 0)) { + game_delete(g); + return false; + } + game_set_square(g, 0, 0, F_LIGHTED); + if ((game_get_square(g, 0, 0) == F_LIGHTED) && game_is_black(g, 0, 0)) { + game_delete(g); + return false; + } + game_delete(g); + return true; } /* ********** game_is_lightbulb ********** */ -bool test_game_is_lightbulb(){ - game g = game_new_empty(); - game_set_square(g, 0, 0, S_LIGHTBULB); - if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_lightbulb(g, 0, 0)){ - game_delete(g); - return true; - } - game_delete(g); - return false; +bool test_game_is_lightbulb() { + game g = game_new_empty(); + game_set_square(g, 0, 0, S_LIGHTBULB); + if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && + game_is_lightbulb(g, 0, 0)) { + game_delete(g); + return true; + } + game_delete(g); + return false; } /* ********** game_is_blank ********** */ -bool test_game_is_blank(){ - game g = game_new_empty(); - if (game_is_blank(g, 0, 0)){ - game_delete(g); - return true; - } - game_delete(g); - return false; +bool test_game_is_blank() { + game g = game_new_empty(); + if (game_is_blank(g, 0, 0)) { + game_delete(g); + return true; + } + game_delete(g); + return false; } /* ********** game_delete ********** */ -bool test_game_delete(){ - game g1 = game_new_empty(); - game_delete(g1); - return true; +bool test_game_delete() { + game g1 = game_new_empty(); + game_delete(g1); + return true; } /* ********** game_equal ********** */ -bool test_game_equal(){ - bool equal = true; - game g1 = game_new_empty(); - game g2 = game_new_empty(); - for (int i = 0; i < DEFAULT_SIZE; i++){ - for (int j = 0; j < DEFAULT_SIZE; j++){ - if (game_get_square(g1, i, j) != game_get_square(g2, i, j)){ - equal = false; - } - } - } - equal = (equal == game_equal(g1, g2)); - game_delete(g1); - game_delete(g2); - return equal; +bool test_game_equal() { + bool equal = true; + game g1 = game_new_empty(); + game g2 = game_new_empty(); + for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int j = 0; j < DEFAULT_SIZE; j++) { + if (game_get_square(g1, i, j) != game_get_square(g2, i, j)) { + equal = false; + } + } + } + equal = (equal == game_equal(g1, g2)); + game_delete(g1); + game_delete(g2); + return equal; } /* ********** game_new_empty ********** */ -bool test_game_new_empty(){ - game g = game_new_empty(); - assert(g); - for (int i = 0; i < DEFAULT_SIZE; i++){ - for (int j = 0; j < DEFAULT_SIZE; j++){ - if (game_get_square(g, i, j) != S_BLANK){ - game_delete(g); - return false; - } - } - } - game_delete(g); - return true; +bool test_game_new_empty() { + game g = game_new_empty(); + assert(g); + for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int j = 0; j < DEFAULT_SIZE; j++) { + if (game_get_square(g, i, j) != S_BLANK) { + game_delete(g); + return false; + } + } + } + game_delete(g); + return true; } /* ********** game_copy ********** */ -bool test_game_copy(){ - game g1 = game_new_empty(); - for (int i = 0; i < DEFAULT_SIZE; i++){ - game_set_square(g1, i, 0, S_LIGHTBULB); - game_set_square(g1, i, 1, S_MARK); - game_set_square(g1, i, 2, S_BLACKU); - game_set_square(g1, i, 3, S_BLACK0); - game_set_square(g1, i, 4, S_BLACK1); - game_set_square(g1, 1, 2, F_LIGHTED); - game_set_square(g1, 1, 3, F_ERROR); - } - game g2 = game_copy(g1); - assert(g2); - if (!game_equal(g1, g2)){ - game_delete(g1); - game_delete(g2); - return false; - } - for (int i = 0; i < DEFAULT_SIZE; i++){ - for (int j = 0; j < DEFAULT_SIZE; j++){ - game_set_square(g2, i, j, S_BLACK3); - } - } - g2 = game_copy(g1); - assert(g2); - if (!game_equal(g1, g2)){ - game_delete(g1); - game_delete(g2); - return false; - } - game_delete(g1); - game_delete(g2); - return true; +bool test_game_copy() { + game g1 = game_new_empty(); + for (int i = 0; i < DEFAULT_SIZE; i++) { + game_set_square(g1, i, 0, S_LIGHTBULB); + game_set_square(g1, i, 1, S_MARK); + game_set_square(g1, i, 2, S_BLACKU); + game_set_square(g1, i, 3, S_BLACK0); + game_set_square(g1, i, 4, S_BLACK1); + game_set_square(g1, 1, 2, F_LIGHTED); + game_set_square(g1, 1, 3, F_ERROR); + } + game g2 = game_copy(g1); + assert(g2); + if (!game_equal(g1, g2)) { + game_delete(g1); + game_delete(g2); + return false; + } + for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int j = 0; j < DEFAULT_SIZE; j++) { + game_set_square(g2, i, j, S_BLACK3); + } + } + g2 = game_copy(g1); + assert(g2); + if (!game_equal(g1, g2)) { + game_delete(g1); + game_delete(g2); + return false; + } + game_delete(g1); + game_delete(g2); + return true; } /* ********** usage ********** */ -void usage(int argc, char *argv[]){ - fprintf(stderr, "Usage: %s \n", argv[0]); - exit(EXIT_FAILURE); +void usage(int argc, char* argv[]) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); } /* ********** main ********** */ -int main (int argc, char *argv[]){ - bool success = false; - if (argc != 2){ - usage(argc, argv); - } - else if (strcmp("dummy", argv[1]) == 0){ - success = test_dummy(); - } - else if (strcmp("game_new_empty", argv[1]) == 0){ - success = test_game_new_empty(); - } - else if (strcmp("game_copy", argv[1]) == 0){ - success = test_game_copy(); - } - else if (strcmp("game_equal", argv[1]) == 0){ - success = test_game_equal(); - } - else if (strcmp("game_delete", argv[1]) == 0){ - success = test_game_delete(); - } - else if (strcmp("game_is_blank", argv[1]) == 0){ - success = test_game_is_blank(); - } - else if (strcmp("game_is_lightbulb", argv[1]) == 0){ - success = test_game_is_lightbulb(); - } - else if (strcmp("game_is_black", argv[1]) == 0){ - success = test_game_is_black(); - } - else if (strcmp("game_get_black_number", argv[1]) == 0){ - success = test_game_get_black_number(); - } - else{ - fprintf (stderr, "test \"%s\" is not a valid test name.\n", argv[1]); - return EXIT_FAILURE; - } - if (success){ - fprintf (stderr, "test \"%s\" finished successfully\n", argv[1]); - return EXIT_SUCCESS; - } - else { - fprintf (stderr, "test \"%s\" error\n", argv[1]); - return EXIT_FAILURE; - } - return EXIT_SUCCESS; +int main(int argc, char* argv[]) { + bool success = false; + if (argc != 2) { + usage(argc, argv); + } else if (strcmp("dummy", argv[1]) == 0) { + success = test_dummy(); + } else if (strcmp("game_new_empty", argv[1]) == 0) { + success = test_game_new_empty(); + } else if (strcmp("game_copy", argv[1]) == 0) { + success = test_game_copy(); + } else if (strcmp("game_equal", argv[1]) == 0) { + success = test_game_equal(); + } else if (strcmp("game_delete", argv[1]) == 0) { + success = test_game_delete(); + } else if (strcmp("game_is_blank", argv[1]) == 0) { + success = test_game_is_blank(); + } else if (strcmp("game_is_lightbulb", argv[1]) == 0) { + success = test_game_is_lightbulb(); + } else if (strcmp("game_is_black", argv[1]) == 0) { + success = test_game_is_black(); + } else if (strcmp("game_get_black_number", argv[1]) == 0) { + success = test_game_get_black_number(); + } else { + fprintf(stderr, "test \"%s\" is not a valid test name.\n", argv[1]); + return EXIT_FAILURE; + } + if (success) { + fprintf(stderr, "test \"%s\" finished successfully\n", argv[1]); + return EXIT_SUCCESS; + } else { + fprintf(stderr, "test \"%s\" error\n", argv[1]); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } \ No newline at end of file diff --git a/game_text.c b/game_text.c index cd42911..46b9d55 100644 --- a/game_text.c +++ b/game_text.c @@ -1,47 +1,51 @@ +#include #include #include -#include #include "game.h" #include "game_aux.h" -bool checkerr(game g, char taberr[]){ - if (taberr == NULL || g == NULL){ +bool checkerr(game g, char taberr[]) { + if (taberr == NULL || g == NULL) { return -1; } uint err = 0; taberr[0] = '\0'; - for (int i=0; i < DEFAULT_SIZE; i++){ - for (int j=0;j < DEFAULT_SIZE; j++){ - //check each case - if (game_has_error(g,i,j)){ + for (int i = 0; i < DEFAULT_SIZE; i++) { + for (int j = 0; j < DEFAULT_SIZE; j++) { + // check each case + if (game_has_error(g, i, j)) { // if error append taberr with coo+, - taberr[err] = i+48; + taberr[err] = i + 48; err++; taberr[err] = ':'; err++; - taberr[err] = j+48; + taberr[err] = j + 48; err++; taberr[err] = ' '; err++; } } } - // if taberr changes print - if (taberr[0] != '\0'){ + // if taberr changes print + if (taberr[0] != '\0') { taberr[err] = '\0'; return true; } return false; } -void printhelp(void){ +void printhelp(void) { printf("Liste des différentes commandes :\n"); printf("h : Affiche cette aide.\n"); printf("r : Réinitialise la partie.\n"); printf("q : Quitter la partie (abandon).\n"); - printf("l : Place une ampoule (*light bulb*) dans la case (,).\n"); + printf( + "l : Place une ampoule (*light bulb*) dans la case " + "(,).\n"); printf("m : Place une marque (*mark*) dans la case (,).\n"); - printf("b : Place placer une case vide (*blank*) à la position (,).\n"); + printf( + "b : Place placer une case vide (*blank*) à la position " + "(,).\n"); } int main(void) { @@ -50,52 +54,53 @@ int main(void) { // test if game is over uint i; uint j; - char taberr[DEFAULT_SIZE*DEFAULT_SIZE*4]; + char taberr[DEFAULT_SIZE * DEFAULT_SIZE * 4]; char c; // print game game_print(g); - while (!game_is_over(g)){ + while (!game_is_over(g)) { // print errors - if (checkerr(g,taberr)){ - printf("Il y a un problème case: %s\n",taberr); + if (checkerr(g, taberr)) { + printf("Il y a un problème case: %s\n", taberr); } printf("Entrez une commande (h pour afficher l'aide): "); - if (scanf(" %c",&c)){ - if (c == 'h'){ + if (scanf(" %c", &c)) { + if (c == 'h') { printhelp(); - }else if (c == 'r'){ + } else if (c == 'r') { game_restart(g); printf("Partie réinitialisé avec success !\n"); game_update_flags(g); game_print(g); - }else if (c == 'q'){ + } else if (c == 'q') { printf("Solution: \n"); game_print(game_default_solution()); printf("shame\n"); return EXIT_SUCCESS; - }else if (c == 'l' || c == 'm' || c == 'b'){ - if (scanf(" %u %u",&i,&j) == 2){ - if (i < DEFAULT_SIZE && j < DEFAULT_SIZE && !game_is_black(g,i,j)){ - if (c == 'l'){ - game_set_square(g,i,j,S_LIGHTBULB); + } else if (c == 'l' || c == 'm' || c == 'b') { + if (scanf(" %u %u", &i, &j) == 2) { + if (i < DEFAULT_SIZE && j < DEFAULT_SIZE && + !game_is_black(g, i, j)) { + if (c == 'l') { + game_set_square(g, i, j, S_LIGHTBULB); game_update_flags(g); game_print(g); - }else if (c == 'm'){ - game_set_square(g,i,j,S_MARK); + } else if (c == 'm') { + game_set_square(g, i, j, S_MARK); game_update_flags(g); game_print(g); - }else if (c == 'b') { - game_set_square(g,i,j,S_BLANK); + } else if (c == 'b') { + game_set_square(g, i, j, S_BLANK); game_update_flags(g); game_print(g); } - }else { + } else { printf("Erreur: (,) invalide.\n"); } - }else { + } else { printf("Commande inconnue.\n"); } - }else{ + } else { printf("Commande inconnue.\n"); } }