game analyze related #13

This commit is contained in:
Théophyl Hoguet committed 2022-03-05 22:20:37 +01:00
1 parent b514e98e70
commit f0d6f2e20e
3 files changed
+61 -7

No files matched your search

+1 -1
View File
@@ -35,7 +35,7 @@
"request": "launch",
"program": "${workspaceFolder}/build/game_test",
"args": [
"game_save"
"game_solve"
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
+4 -4
View File
@@ -1183,10 +1183,10 @@ bool test_game_solve(bool whoami, char** name) {
*name = (char*)__func__;
return false;
}
game g_default = game_new_ext(4, 4, ext_4x4_squares, false);
game g_default_sol = game_new_ext(4, 4, sol_4x4_squares, false);
assert(game_solve(g_default));
assert(game_equal(g_default, g_default_sol));
game g = game_load("/home/nessar/lightup-07c/build/test.txt");
game_solve(g);
game_print(g);
game_delete(g);
return true;
}
+56 -2
View File
@@ -6,6 +6,11 @@
#define NB_CHAR_HEADER_WITHTOUT_DIMENSIONS 4
#define JWRAPPING (j + game_nb_cols(g) + dir[index_dir + 1]) % game_nb_cols(g)
#define IWRAPPING (i + game_nb_rows(g) + dir[index_dir]) % game_nb_rows(g)
#define JNORMAL j + dir[index_dir + 1]
#define INORMAL i + dir[index_dir]
void test_output(int i, int excepted, char* message) {
if (i != excepted) {
fprintf(stderr, message);
@@ -116,9 +121,58 @@ bool aux_game_solve(game g, uint deep) {
return false;
}
void add_next_to(game g, uint i, uint j, square s) {
int dir[] = {1, 0, -1, 0, 0, 1, 0, -1};
for (uint index_dir = 0; index_dir < sizeof(dir) / sizeof(dir[0]); index_dir += 2) {
if (/* normal check*/ (JNORMAL < game_nb_cols(g) && INORMAL < game_nb_rows(g)) ||
/*wrapping check*/ (game_is_wrapping(g) &&
/*not the same case*/ !(IWRAPPING == i && JWRAPPING == j))) {
if (game_get_state(g, IWRAPPING, JWRAPPING) == S_BLANK) {
game_set_square(g, IWRAPPING, JWRAPPING, s);
}
}
}
}
void game_analyze(game g) {
uint not_empty = 0;
int dir[] = {1, 0, -1, 0, 0, 1, 0, -1};
for (uint i = 0; i < game_nb_rows(g); i++) {
for (uint j = 0; j < game_nb_cols(g); j++) {
if (game_get_state(g, i, j) >= S_BLACK1) {
for (uint index_dir = 0; index_dir < sizeof(dir) / sizeof(dir[0]); index_dir += 2) {
if (/* normal check*/ (JNORMAL < game_nb_cols(g) && INORMAL < game_nb_rows(g)) ||
/*wrapping check*/ (game_is_wrapping(g) &&
/*not the same case*/ !(IWRAPPING == i && JWRAPPING == j))) {
if (!game_is_blank(g, IWRAPPING, JWRAPPING) || game_is_lighted(g, IWRAPPING, JWRAPPING))
not_empty++;
} else {
not_empty++;
}
}
if (not_empty == abs(game_get_black_number(g, i, j) - 4)) {
add_next_to(g, i, j, S_LIGHTBULB);
}
} else if (game_get_state(g, i, j) == S_BLACK0)
add_next_to(g, i, j, S_MARK);
}
}
}
bool game_solve(game g) {
for (uint deep = 1; deep < game_nb_cols(g) * game_nb_rows(g); deep++) {
if (aux_game_solve(g, deep)) {
game g_copy = game_copy(g);
game_analyze(g_copy);
if (game_is_over(g_copy)) {
game to_delete = g;
*g = *g_copy;
game_delete(to_delete);
return true;
}
for (uint deep = 1; deep < game_nb_cols(g_copy) * game_nb_rows(g_copy); deep++) {
if (aux_game_solve(g_copy, deep)) {
game to_delete = g;
*g = *g_copy;
game_delete(to_delete);
return true;
}
}