From f0d6f2e20e4b854bb719d636bbbaea2c5783c00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophyl=20Hoguet?= Date: Fri, 18 Feb 2022 03:59:49 +0100 Subject: [PATCH] game analyze related #13 --- .vscode/launch.json | 2 +- game_test.c | 8 +++---- game_tools.c | 58 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 15864e7..6a1f9c6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -35,7 +35,7 @@ "request": "launch", "program": "${workspaceFolder}/build/game_test", "args": [ - "game_save" + "game_solve" ], "stopAtEntry": false, "cwd": "${fileDirname}", diff --git a/game_test.c b/game_test.c index be41f54..c58829c 100644 --- a/game_test.c +++ b/game_test.c @@ -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; } diff --git a/game_tools.c b/game_tools.c index 2e7d1a1..83fe40c 100644 --- a/game_tools.c +++ b/game_tools.c @@ -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; } }