This commit is contained in:
Théophyl Hoguet committed 2021-12-09 20:42:04 +01:00
1 parent af17b34a9a
commit aac7da4685
8 files changed
+110 -207

No files matched your search

+1 -1
View File
@@ -45,7 +45,7 @@ BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true BreakStringLiterals: true
ColumnLimit: 80 ColumnLimit: 160
CommentPragmas: '^ IWYU pragma:' CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true ConstructorInitializerAllOnOneLineOrOnePerLine: true
+65 -124
View File
@@ -112,20 +112,15 @@ square game_get_flags(cgame g, uint i, uint j) {
} }
bool game_is_blank(cgame g, uint i, uint j) { bool game_is_blank(cgame g, uint i, uint j) {
return (g->tab_cell[i][j] == S_BLANK); return game_get_state(g, i, j) == S_BLANK;
} }
bool game_is_lightbulb(cgame g, uint i, uint j) { bool game_is_lightbulb(cgame g, uint i, uint j) {
return ((g->tab_cell[i][j] == S_LIGHTBULB) || return game_get_state(g, i, j) == S_LIGHTBULB;
(g->tab_cell[i][j] == (S_LIGHTBULB | F_LIGHTED)) ||
(g->tab_cell[i][j] == (S_LIGHTBULB | F_ERROR)) ||
(g->tab_cell[i][j] == (S_LIGHTBULB | F_LIGHTED | F_ERROR)));
} }
bool game_is_black(cgame g, uint i, uint j) { bool game_is_black(cgame g, uint i, uint j) {
if (game_get_state(g, i, j) >= 8 && game_get_state(g, i, j) <= 13) return game_get_state(g, i, j) >= S_BLACK0 && game_get_state(g, i, j) <= S_BLACKU;
return true;
return false;
} }
int game_get_black_number(cgame g, uint i, uint j) { int game_get_black_number(cgame g, uint i, uint j) {
@@ -135,33 +130,19 @@ int game_get_black_number(cgame g, uint i, uint j) {
} }
bool game_is_marked(cgame g, uint i, uint j) { bool game_is_marked(cgame g, uint i, uint j) {
return ((g->tab_cell[i][j] == S_MARK) || return game_get_state(g, i, j) == S_MARK;
(g->tab_cell[i][j] == (S_MARK | F_LIGHTED)) ||
(g->tab_cell[i][j] == (S_MARK | F_ERROR)) ||
(g->tab_cell[i][j] == (S_MARK | F_LIGHTED | F_ERROR)));
} }
bool game_is_lighted(cgame g, uint i, uint j) { bool game_is_lighted(cgame g, uint i, uint j) {
if (g->tab_cell[i][j] >= F_ERROR) { return game_get_flags(g, i, j) >= F_LIGHTED;
g->tab_cell[i][j] -= F_ERROR;
}
if (g->tab_cell[i][j] >= F_LIGHTED) {
return true;
}
return false;
} }
bool game_has_error(cgame g, uint i, uint j) { bool game_has_error(cgame g, uint i, uint j) {
if (g->tab_cell[i][j] >= F_ERROR) { return g->tab_cell[i][j] >= F_ERROR;
return true;
}
return false;
} }
bool game_check_move(cgame g, uint i, uint j, square s) { bool game_check_move(cgame g, uint i, uint j, square s) {
if (i >= DEFAULT_SIZE || j >= DEFAULT_SIZE || if (i >= DEFAULT_SIZE || j >= DEFAULT_SIZE || (s != S_BLANK && s != S_MARK && s != S_LIGHTBULB) || game_is_black(g, i, j))
(s != S_BLANK && s != S_MARK && s != S_LIGHTBULB) ||
game_is_black(g, i, j))
return false; return false;
return true; return true;
} }
@@ -171,115 +152,78 @@ void game_play_move(game g, uint i, uint j, square s) {
game_update_flags(g); game_update_flags(g);
} }
void game_update_flags(game g) { void delete_flags(game g) {
// delete prev flags // delete prev flags
for (uint i = 0; i < DEFAULT_SIZE; i++) { for (uint i = 0; i < DEFAULT_SIZE; i++) {
for (uint j = 0; j < DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE; j++) {
game_set_square(g, i, j, game_get_state(g, i, j)); game_set_square(g, i, j, game_get_state(g, i, j));
} }
} }
}
void addF_LIGHTED(game g, uint i, uint j) {
square flags = game_get_flags(g, i, j);
if (flags == S_BLANK)
game_set_square(g, i, j, game_get_state(g, i, j) | F_LIGHTED);
else if (flags == F_ERROR) {
game_set_square(g, i, j, game_get_state(g, i, j) | F_LIGHTED | F_ERROR);
}
}
void game_update_flags(game g) {
delete_flags(g);
// update flags lighted and lightbulb error // update flags lighted and lightbulb error
for (uint i = 0; i < DEFAULT_SIZE; i++) { for (int i = 0; i < DEFAULT_SIZE; i++) {
for (uint j = 0; j < DEFAULT_SIZE; j++) { for (int j = 0; j < DEFAULT_SIZE; j++) {
if (game_get_state(g, i, j) == S_LIGHTBULB) { if (game_get_state(g, i, j) == S_LIGHTBULB) {
if (game_get_flags(g, i, j) > 16) // add FLIGHTED to the current lightbulb
game_set_square(g, i, j, addF_LIGHTED(g, i, j);
(S_LIGHTBULB | F_LIGHTED | F_ERROR)); // add FLIGHTED on the 4 directions, when a wall is hit stop this direction, when another lightbulb is hit put the initial lighbulb on error
else int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1};
game_set_square(g, i, j, (S_LIGHTBULB | F_LIGHTED)); for (int x = 1; x < DEFAULT_SIZE; x++) {
for (uint i2 = i + 1; for (uint y = 0; y < 7; y = y + 2) {
i2 < DEFAULT_SIZE && !game_is_black(g, i2, j); i2++) { if (tab[y] != tab[y + 1] && j + x * tab[y + 1] >= 0 && j + x * tab[y + 1] < DEFAULT_SIZE && i + x * tab[y] >= 0 &&
if (game_get_state(g, i2, j) == S_LIGHTBULB) i + x * tab[y] < DEFAULT_SIZE) {
// update initial lightbulb on F_ERROR if (game_is_black(g, i + x * tab[y], j + x * tab[y + 1])) {
game_set_square(g, i, j, tab[y] = 0;
(S_LIGHTBULB | F_LIGHTED | F_ERROR)); tab[y + 1] = 0;
// update cases on FLIGHTED } else {
game_set_square(g, i2, j, // if we found another lightbulb on the line
(game_get_state(g, i2, j) | F_LIGHTED)); if (game_get_state(g, i + x * tab[y], j + x * tab[y + 1]) == S_LIGHTBULB)
} // update initial lightbulb on F_ERROR
for (int i2 = i - 1; i2 >= 0 && !game_is_black(g, i2, j); game_set_square(g, i, j, (S_LIGHTBULB | F_LIGHTED | F_ERROR));
i2--) { addF_LIGHTED(g, i + x * tab[y], j + x * tab[y + 1]);
if (game_get_state(g, i2, j) == S_LIGHTBULB) }
// update initial lightbulb on F_ERROR }
game_set_square(g, i, j, }
(S_LIGHTBULB | F_LIGHTED | F_ERROR));
game_set_square(g, i2, j,
(game_get_state(g, i2, j) | F_LIGHTED));
}
for (int j2 = j - 1; j2 >= 0 && !game_is_black(g, i, j2);
j2--) {
if (game_get_state(g, i, j2) == S_LIGHTBULB)
// update initial lightbulb on F_ERROR
game_set_square(g, i, j,
(S_LIGHTBULB | F_LIGHTED | F_ERROR));
game_set_square(g, i, j2,
(game_get_state(g, i, j2) | F_LIGHTED));
}
for (uint j2 = j + 1;
j2 < DEFAULT_SIZE && !game_is_black(g, i, j2); j2++) {
if (game_get_state(g, i, j2) == S_LIGHTBULB)
// update initial lightbulb on F_ERROR
game_set_square(g, i, j,
(S_LIGHTBULB | F_LIGHTED | F_ERROR));
game_set_square(g, i, j2,
(game_get_state(g, i, j2) | F_LIGHTED));
} }
} }
} }
} }
// update flags wall // update flags wall (need to wait all FLIGHTED being updated)
for (uint i = 0; i < DEFAULT_SIZE; i++) { for (int i = 0; i < DEFAULT_SIZE; i++) {
for (uint j = 0; j < DEFAULT_SIZE; j++) { for (int j = 0; j < DEFAULT_SIZE; j++) {
if (game_is_black(g, i, j)) { if (game_is_black(g, i, j) && game_get_black_number(g, i, j) >= 0) {
if (game_get_black_number(g, i, j) >= 0) { int lb = 0;
square choice[4]; int notempty = 0;
int size = 0; int tab[] = {-1, 0, 1, 0, 0, -1, 0, 1};
int lb = 0; for (uint y = 0; y < 7; y = y + 2) {
int notempty = 0; if (j + tab[y + 1] >= 0 && j + tab[y + 1] < DEFAULT_SIZE && i + tab[y] >= 0 && i + tab[y] < DEFAULT_SIZE) {
if (i > 0) { if (game_is_lightbulb(g, i + tab[y], j + tab[y + 1]))
choice[size] = game_get_square(g, i - 1, j);
size++;
} else {
notempty++;
}
if (i < DEFAULT_SIZE - 1) {
choice[size] = game_get_square(g, i + 1, j);
size++;
} else {
notempty++;
}
if (j > 0) {
choice[size] = game_get_square(g, i, j - 1);
size++;
} else {
notempty++;
}
if (j < DEFAULT_SIZE - 1) {
choice[size] = game_get_square(g, i, j + 1);
size++;
} else {
notempty++;
}
for (int l = 0; l < size; l++) {
if (choice[l] == S_LIGHTBULB ||
choice[l] == (S_LIGHTBULB | F_LIGHTED) ||
choice[l] == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
lb++; lb++;
} else if (choice[l] != S_BLANK) { else if (!game_is_blank(g, i + tab[y], j + tab[y + 1]) || game_is_lighted(g, i + tab[y], j + tab[y + 1]))
notempty++; notempty++;
} } else {
} notempty++;
if (lb > game_get_black_number(g, i, j)) {
game_set_square(g, i, j,
(game_get_state(g, i, j) | F_ERROR));
}
// look if there is enough empty cell around the wall
else if (notempty >
abs(game_get_black_number(g, i, j) - 4)) {
game_set_square(g, i, j,
(game_get_state(g, i, j) | F_ERROR));
} }
} }
if (lb > game_get_black_number(g, i, j)) {
game_set_square(g, i, j, (game_get_state(g, i, j) | F_ERROR));
}
// look if there is enough empty cell around the wall
else if (notempty > abs(game_get_black_number(g, i, j) - 4)) {
game_set_square(g, i, j, (game_get_state(g, i, j) | F_ERROR));
}
} }
} }
} }
@@ -288,9 +232,7 @@ void game_update_flags(game g) {
bool game_is_over(cgame g) { bool game_is_over(cgame g) {
for (uint i = 0; i < DEFAULT_SIZE; i++) { for (uint i = 0; i < DEFAULT_SIZE; i++) {
for (uint j = 0; j < DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE; j++) {
if ((game_get_state(g, i, j) == S_BLANK && if ((game_get_state(g, i, j) == S_BLANK && game_get_flags(g, i, j) == S_BLANK) || game_get_flags(g, i, j) == F_ERROR ||
game_get_flags(g, i, j) == S_BLANK) ||
game_get_flags(g, i, j) == F_ERROR ||
game_get_flags(g, i, j) == (F_LIGHTED | F_ERROR)) game_get_flags(g, i, j) == (F_LIGHTED | F_ERROR))
return false; return false;
} }
@@ -301,8 +243,7 @@ bool game_is_over(cgame g) {
void game_restart(game g) { void game_restart(game g) {
for (uint i = 0; i < DEFAULT_SIZE; i++) { for (uint i = 0; i < DEFAULT_SIZE; i++) {
for (uint j = 0; j < DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE; j++) {
if (game_get_state(g, i, j) == S_MARK || if (game_get_state(g, i, j) == S_MARK || game_get_state(g, i, j) == S_LIGHTBULB)
game_get_state(g, i, j) == S_LIGHTBULB)
game_set_square(g, i, j, S_BLANK); game_set_square(g, i, j, S_BLANK);
} }
} }
+17 -18
View File
@@ -33,20 +33,20 @@ typedef unsigned int uint;
* https://en.wikipedia.org/wiki/Bitwise_operations_in_C. * https://en.wikipedia.org/wiki/Bitwise_operations_in_C.
**/ **/
typedef enum { typedef enum {
/* states */ /* states */
S_BLANK = 0, /**< a blank square */ S_BLANK = 0, /**< a blank square */
S_LIGHTBULB = 1, /**< a light bulb */ S_LIGHTBULB = 1, /**< a light bulb */
S_MARK = 2, /**< a marked square (as not-a-light) */ S_MARK = 2, /**< a marked square (as not-a-light) */
S_BLACK = 8, /**< black wall base code */ S_BLACK = 8, /**< black wall base code */
S_BLACK0 = S_BLACK, /**< a numbered black wall (with 0 adjacent lights) */ S_BLACK0 = S_BLACK, /**< a numbered black wall (with 0 adjacent lights) */
S_BLACK1, /**< a numbered black wall (with 1 adjacent light) */ S_BLACK1, /**< a numbered black wall (with 1 adjacent light) */
S_BLACK2, /**< a numbered black wall (with 2 adjacent lights) */ S_BLACK2, /**< a numbered black wall (with 2 adjacent lights) */
S_BLACK3, /**< a numbered black wall (with 3 adjacent lights) */ S_BLACK3, /**< a numbered black wall (with 3 adjacent lights) */
S_BLACK4, /**< a numbered black wall (with 4 adjacent lights) */ S_BLACK4, /**< a numbered black wall (with 4 adjacent lights) */
S_BLACKU, /**< an unnumbered black wall (any number of adjacent lights) */ S_BLACKU, /**< an unnumbered black wall (any number of adjacent lights) */
/* flags */ /* flags */
F_LIGHTED = 16, /**< lighted flag */ F_LIGHTED = 16, /**< lighted flag */
F_ERROR = 32 /**< error flag */ F_ERROR = 32 /**< error flag */
} square; } square;
/** state mask used in square enum */ /** state mask used in square enum */
@@ -61,14 +61,14 @@ typedef enum {
/** /**
* @brief The structure pointer that stores the game state. * @brief The structure pointer that stores the game state.
**/ **/
typedef struct game_s *game; typedef struct game_s* game;
/** /**
* @brief The structure constant pointer that stores the game state. * @brief The structure constant pointer that stores the game state.
* @details That means that it is not possible to modify the game using this * @details That means that it is not possible to modify the game using this
* pointer. * pointer.
**/ **/
typedef const struct game_s *cgame; typedef const struct game_s* cgame;
/** /**
* @brief Creates a new game with default size and initializes it. * @brief Creates a new game with default size and initializes it.
@@ -78,7 +78,7 @@ typedef const struct game_s *cgame;
* @pre @p squares must be an initialized array of default size squared. * @pre @p squares must be an initialized array of default size squared.
* @return the created game * @return the created game
**/ **/
game game_new(square *squares); game game_new(square* squares);
/** /**
* @brief Creates a new empty game with defaut size. * @brief Creates a new empty game with defaut size.
@@ -251,7 +251,6 @@ bool game_is_lighted(cgame g, uint i, uint j);
**/ **/
bool game_has_error(cgame g, uint i, uint j); bool game_has_error(cgame g, uint i, uint j);
/** /**
* @brief Checks if a given move is legal. * @brief Checks if a given move is legal.
* @details This function checks that it is possible to play a move at a given * @details This function checks that it is possible to play a move at a given
+2 -5
View File
@@ -7,13 +7,10 @@ char charPrintCell(square sqState) {
return ' '; return ' ';
} }
if ((sqState == S_LIGHTBULB) || if ((sqState == S_LIGHTBULB) ||
(sqState == (S_LIGHTBULB | F_ERROR)) | (sqState == (S_LIGHTBULB | F_ERROR)) | (sqState == (S_LIGHTBULB | F_LIGHTED)) | (sqState == (S_LIGHTBULB | F_ERROR | F_LIGHTED))) {
(sqState == (S_LIGHTBULB | F_LIGHTED)) |
(sqState == (S_LIGHTBULB | F_ERROR | F_LIGHTED))) {
return '*'; return '*';
} }
if ((sqState == S_MARK) || if ((sqState == S_MARK) || (sqState == (S_MARK | F_ERROR)) | (sqState == (S_MARK | F_LIGHTED))) {
(sqState == (S_MARK | F_ERROR)) | (sqState == (S_MARK | F_LIGHTED))) {
return '-'; return '-';
} }
if (sqState == S_BLACK0 || sqState == (S_BLACK0 | F_ERROR)) { if (sqState == S_BLACK0 || sqState == (S_BLACK0 | F_ERROR)) {
+2 -4
View File
@@ -332,8 +332,7 @@ bool test_game_is_lighted(void) {
return true; return true;
} }
game_set_square(g, 0, 0, S_BLANK | F_LIGHTED | F_ERROR); game_set_square(g, 0, 0, S_BLANK | F_LIGHTED | F_ERROR);
if ((game_get_flags(g, 0, 0) == (F_LIGHTED | F_ERROR)) && if ((game_get_flags(g, 0, 0) == (F_LIGHTED | F_ERROR)) && game_is_lighted(g, 0, 0)) {
game_is_lighted(g, 0, 0)) {
game_delete(g); game_delete(g);
return true; return true;
} }
@@ -350,8 +349,7 @@ bool test_game_has_error(void) {
return true; return true;
} }
game_set_square(g, 0, 0, S_LIGHTBULB | F_LIGHTED | F_ERROR); game_set_square(g, 0, 0, S_LIGHTBULB | F_LIGHTED | F_ERROR);
if ((game_get_flags(g, 0, 0) == (F_LIGHTED | F_ERROR)) && if ((game_get_flags(g, 0, 0) == (F_LIGHTED | F_ERROR)) && game_has_error(g, 0, 0)) {
game_has_error(g, 0, 0)) {
game_delete(g); game_delete(g);
return true; return true;
} }
+21 -51
View File
@@ -58,9 +58,7 @@ bool test_game_restart(void) {
bool test_game_new(void) { bool test_game_new(void) {
square tab[DEFAULT_SIZE * DEFAULT_SIZE]; square tab[DEFAULT_SIZE * DEFAULT_SIZE];
game g; game g;
square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK, square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU, F_ERROR};
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 i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
g = NULL; g = NULL;
for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) {
@@ -70,8 +68,7 @@ bool test_game_new(void) {
g = game_new(tab); g = game_new(tab);
for (uint i2 = 0; i2 < DEFAULT_SIZE; i2++) { for (uint i2 = 0; i2 < DEFAULT_SIZE; i2++) {
for (uint j = 0; j < DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE; j++) {
printf("list :%d, get square :%d\n", list[i], printf("list :%d, get square :%d\n", list[i], game_get_square(g, i2, j));
game_get_square(g, i2, j));
assert(list[i] == game_get_square(g, i2, j)); assert(list[i] == game_get_square(g, i2, j));
} }
} }
@@ -87,8 +84,7 @@ bool test_game_new(void) {
bool test_game_print(void) { bool test_game_print(void) {
square tab[DEFAULT_SIZE * DEFAULT_SIZE]; square tab[DEFAULT_SIZE * DEFAULT_SIZE];
square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU};
S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU};
game g; game g;
for (uint i = 0; i < sizeof(list) / sizeof(list[0]); i++) { for (uint i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) {
@@ -108,25 +104,14 @@ bool test_game_print(void) {
bool test_game_get_square(void) { bool test_game_get_square(void) {
game g; game g;
square tab[DEFAULT_SIZE * DEFAULT_SIZE]; square tab[DEFAULT_SIZE * DEFAULT_SIZE];
square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK, square list[] = {S_BLANK, S_LIGHTBULB, S_MARK, S_BLACK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU, F_ERROR};
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 i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE * DEFAULT_SIZE; j++) {
tab[j] = list[i]; tab[j] = list[i];
g = game_new(tab); g = game_new(tab);
printf( printf("[%d, %d] %d %d\n", j / (DEFAULT_SIZE), j - (j / (DEFAULT_SIZE)) - (j / (DEFAULT_SIZE) * (DEFAULT_SIZE - 1)),
"[%d, %d] %d %d\n", j / (DEFAULT_SIZE), game_get_square(g, j / (DEFAULT_SIZE), j - (j / (DEFAULT_SIZE)) - (j / (DEFAULT_SIZE) * (DEFAULT_SIZE - 1))), tab[j]);
j - (j / (DEFAULT_SIZE)) - assert(game_get_square(g, j / (DEFAULT_SIZE), j - (j / (DEFAULT_SIZE)) - (j / (DEFAULT_SIZE) * (DEFAULT_SIZE - 1))) == tab[j]);
(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); game_delete(g);
} }
} }
@@ -192,8 +177,7 @@ int checklightbulb(game g, uint i, uint j, bool wall) {
// if true we can break the loop because every others cases wont be lighted // if true we can break the loop because every others cases wont be lighted
// by the bulb we are testing // by the bulb we are testing
if (game_is_lightbulb(g, i, j)) { if (game_is_lightbulb(g, i, j)) {
if (/*test if this lightbulb is well updated*/ flags == S_BLANK || if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR) {
flags == F_ERROR) {
// printf("156\n"); // printf("156\n");
return 0; return 0;
} }
@@ -214,8 +198,7 @@ int checklightbulb(game g, uint i, uint j, bool wall) {
for (int i2 = i - 1; i2 >= 0; i2--) { for (int i2 = i - 1; i2 >= 0; i2--) {
flags = game_get_square(g, i2, j); flags = game_get_square(g, i2, j);
// printf("i2:%d i2state:%d i2flags:%d\n",i2,state,flags); // printf("i2:%d i2state:%d i2flags:%d\n",i2,state,flags);
if (flags == (S_LIGHTBULB | F_LIGHTED) || if (flags == (S_LIGHTBULB | F_LIGHTED) || flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
return -1; return -1;
} }
if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) { if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) {
@@ -224,8 +207,7 @@ int checklightbulb(game g, uint i, uint j, bool wall) {
} }
for (int i2 = i + 1; i2 < DEFAULT_SIZE; i2++) { for (int i2 = i + 1; i2 < DEFAULT_SIZE; i2++) {
flags = game_get_square(g, i2, j); flags = game_get_square(g, i2, j);
if (flags == (S_LIGHTBULB | F_LIGHTED) || if (flags == (S_LIGHTBULB | F_LIGHTED) || flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
return -1; return -1;
} }
if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) { if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) {
@@ -234,8 +216,7 @@ int checklightbulb(game g, uint i, uint j, bool wall) {
} }
for (int j2 = j - 1; j2 >= 0; j2--) { for (int j2 = j - 1; j2 >= 0; j2--) {
flags = game_get_square(g, i, j2); flags = game_get_square(g, i, j2);
if (flags == (S_LIGHTBULB | F_LIGHTED) || if (flags == (S_LIGHTBULB | F_LIGHTED) || flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
return -1; return -1;
} }
if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) { if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) {
@@ -244,8 +225,7 @@ int checklightbulb(game g, uint i, uint j, bool wall) {
} }
for (int j2 = j + 1; j2 < DEFAULT_SIZE; j2++) { for (int j2 = j + 1; j2 < DEFAULT_SIZE; j2++) {
flags = game_get_square(g, i, j2); flags = game_get_square(g, i, j2);
if (flags == (S_LIGHTBULB | F_LIGHTED) || if (flags == (S_LIGHTBULB | F_LIGHTED) || flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
flags == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
return -1; return -1;
} }
if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) { if (flags != F_LIGHTED && flags != (F_LIGHTED | S_MARK)) {
@@ -270,17 +250,14 @@ bool check_update(game g) {
for (uint j = 0; j < DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE; j++) {
if (game_is_lightbulb(g, i, j)) { if (game_is_lightbulb(g, i, j)) {
square flags = game_get_flags(g, i, j); square flags = game_get_flags(g, i, j);
if (/*test if this lightbulb is well updated*/ flags == if (/*test if this lightbulb is well updated*/ flags == S_BLANK || flags == F_ERROR) {
S_BLANK ||
flags == F_ERROR) {
// printf("196\n"); // printf("196\n");
return false; return false;
} }
// loop checking every case before i // loop checking every case before i
wall = false; wall = false;
for (int k = i - 1; k >= 0; k--) { for (int k = i - 1; k >= 0; k--) {
if (/*test if current case is a wall*/ game_is_black(g, k, if (/*test if current case is a wall*/ game_is_black(g, k, j)) {
j)) {
wall = true; wall = true;
} else { } else {
int tmp = checklightbulb(g, k, j, wall); int tmp = checklightbulb(g, k, j, wall);
@@ -294,8 +271,7 @@ bool check_update(game g) {
} }
wall = false; wall = false;
for (int k = i + 1; k < DEFAULT_SIZE; k++) { for (int k = i + 1; k < DEFAULT_SIZE; k++) {
if (/*test if current case is a wall*/ game_is_black(g, k, if (/*test if current case is a wall*/ game_is_black(g, k, j)) {
j)) {
wall = true; wall = true;
} else { } else {
int tmp = checklightbulb(g, k, j, wall); int tmp = checklightbulb(g, k, j, wall);
@@ -309,8 +285,7 @@ bool check_update(game g) {
} }
wall = false; wall = false;
for (int k = j - 1; k >= 0; k--) { for (int k = j - 1; k >= 0; k--) {
if (/*test if current case is a wall*/ game_is_black(g, i, if (/*test if current case is a wall*/ game_is_black(g, i, k)) {
k)) {
wall = true; wall = true;
} else { } else {
int tmp = checklightbulb(g, i, k, wall); int tmp = checklightbulb(g, i, k, wall);
@@ -324,8 +299,7 @@ bool check_update(game g) {
} }
wall = false; wall = false;
for (int k = j + 1; k < DEFAULT_SIZE; k++) { for (int k = j + 1; k < DEFAULT_SIZE; k++) {
if (/*test if current case is a wall*/ game_is_black(g, i, if (/*test if current case is a wall*/ game_is_black(g, i, k)) {
k)) {
wall = true; wall = true;
} else { } else {
int tmp = checklightbulb(g, i, k, wall); int tmp = checklightbulb(g, i, k, wall);
@@ -337,8 +311,7 @@ bool check_update(game g) {
} }
} }
} }
} else if (/*test if current case is a wall*/ game_is_black(g, i, } else if (/*test if current case is a wall*/ game_is_black(g, i, j)) {
j)) {
if (game_get_black_number(g, i, j) != -1) { if (game_get_black_number(g, i, j) != -1) {
// lookup for lightbulb and emptycells // lookup for lightbulb and emptycells
size = 0; size = 0;
@@ -370,8 +343,7 @@ bool check_update(game g) {
notempty++; notempty++;
} }
for (int l = 0; l < size; l++) { for (int l = 0; l < size; l++) {
if (choice[l] == (S_LIGHTBULB | F_LIGHTED) || if (choice[l] == (S_LIGHTBULB | F_LIGHTED) || choice[l] == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
choice[l] == (S_LIGHTBULB | F_LIGHTED | F_ERROR)) {
lb++; lb++;
} else if (choice[l] != S_BLANK) { } else if (choice[l] != S_BLANK) {
notempty++; notempty++;
@@ -381,8 +353,7 @@ bool check_update(game g) {
res = false; res = false;
} }
// look if there is enough empty cell around the wall // look if there is enough empty cell around the wall
else if (notempty > else if (notempty > abs(game_get_black_number(g, i, j) - 4)) {
abs(game_get_black_number(g, i, j) - 4)) {
res = false; res = false;
} }
if (game_get_flags(g, i, j) == F_ERROR) { if (game_get_flags(g, i, j) == F_ERROR) {
@@ -410,8 +381,7 @@ bool brutforce(game g, int a, bool* deldup) {
fprintf(stderr, "Not enought memory\n"); fprintf(stderr, "Not enought memory\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
square list[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, square list[] = {S_LIGHTBULB, S_MARK, S_BLACK0, S_BLACK1, S_BLACK2, S_BLACK3, S_BLACK4, S_BLACKU, S_BLANK};
S_BLACK3, S_BLACK4, S_BLACKU, S_BLANK};
for (uint i = 0; i < DEFAULT_SIZE; i++) { for (uint i = 0; i < DEFAULT_SIZE; i++) {
for (uint j = 0; j < DEFAULT_SIZE; j++) { for (uint j = 0; j < DEFAULT_SIZE; j++) {
// printf("state: %d i:%d j:%d\n",game_get_state(g,i,j),i,j); // printf("state: %d i:%d j:%d\n",game_get_state(g,i,j),i,j);
+1 -2
View File
@@ -122,8 +122,7 @@ bool test_game_is_black() {
bool test_game_is_lightbulb() { bool test_game_is_lightbulb() {
game g = game_new_empty(); game g = game_new_empty();
game_set_square(g, 0, 0, S_LIGHTBULB); game_set_square(g, 0, 0, S_LIGHTBULB);
if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && if ((game_get_square(g, 0, 0) == S_LIGHTBULB) && game_is_lightbulb(g, 0, 0)) {
game_is_lightbulb(g, 0, 0)) {
game_delete(g); game_delete(g);
return true; return true;
} }
+1 -2
View File
@@ -79,8 +79,7 @@ int main(void) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} else if (c == 'l' || c == 'm' || c == 'b') { } else if (c == 'l' || c == 'm' || c == 'b') {
if (scanf(" %u %u", &i, &j) == 2) { if (scanf(" %u %u", &i, &j) == 2) {
if (i < DEFAULT_SIZE && j < DEFAULT_SIZE && if (i < DEFAULT_SIZE && j < DEFAULT_SIZE && !game_is_black(g, i, j)) {
!game_is_black(g, i, j)) {
if (c == 'l') { if (c == 'l') {
game_set_square(g, i, j, S_LIGHTBULB); game_set_square(g, i, j, S_LIGHTBULB);
game_update_flags(g); game_update_flags(g);