From ea6f60fc29dbab827efb3a60770b0058246bcb90 Mon Sep 17 00:00:00 2001 From: Matuush Date: Fri, 8 Nov 2024 21:39:02 +0100 Subject: [PATCH] Fixed cleaning and if_not_here_then_nowhere --- solver.cpp | 46 +++++++++++++++++++++++++++++++++------------- solver.hpp | 1 + 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/solver.cpp b/solver.cpp index faa43c4..c825f81 100644 --- a/solver.cpp +++ b/solver.cpp @@ -31,6 +31,12 @@ int Game::final_color(int n) { return final_col; return -1; } +bool Game::is_empty(int col) { + for(int n = 0; n < N; n++) + if(possible[n][col]) + return false; + return true; +} vector> Game::list_all_possibilities() { auto r = vector>(N, vector(0)); for(int col = 0; col < M; col++) @@ -102,7 +108,7 @@ void Game::all_are_here(vector guess) { auto positions_of_colors = get_positions_of_colors(guess); for(int col = 0; col < M; col++) { - if(!positions_of_colors[col].size()) + if(positions_of_colors[col].size() == 0) empty_color(col); } } @@ -114,8 +120,8 @@ Historic_guess::Historic_guess(vector p_guess, vector p_response) { response = p_response; } -// Solver +// Solver Solver::Solver(int p_N, int p_M) : N(p_N), M(p_M), known({p_N, p_M}) {} // Check, if it could have been this @@ -160,26 +166,35 @@ void Solver::print() { // Clean guess and response from info we know Historic_guess Solver::clean(Historic_guess hist) { + vector already_used_in_cleaning(N, 0); + + // Clean empty colors + for(int n = 0; n < N; n++) + if(known.is_empty(hist.guess[n])) + hist.guess[n] = -2; + // The in-place colors we know for(int n = 0; n < N; n++) { - if(hist.guess[n] == -1) + if(hist.guess[n] <= -1) continue; if(known.final_color(n) == hist.guess[n]) { hist.guess[n] = -1; hist.response[1] -= 1; + already_used_in_cleaning[n] = 1; } } // The out-of-place colors we know for(int n = 0; n < N; n++) { - if(hist.guess[n] == -1) + if(hist.guess[n] <= -1 || known.can(n, hist.guess[n])) continue; for(int i = 0; i < N; i++) { - if(i == n || hist.guess[i] == -1) + if(i == n || hist.guess[i] <= -1 || already_used_in_cleaning[i]) continue; if(known.final_color(i) == hist.guess[n]) { hist.guess[n] = -1; hist.response[0] -= 1; + already_used_in_cleaning[i] = 1; break; } } @@ -205,8 +220,8 @@ bool Solver::extract_info(Historic_guess hist) { possible_count++; // None of these colors are there - if(response[0] == 0 && response[1] == 0) { - known.empty(guess); + if(hist.response[0] == 0 && hist.response[1] == 0) { + known.empty(hist.guess); something_to_learn = false; } @@ -221,8 +236,10 @@ bool Solver::extract_info(Historic_guess hist) { known.here(guess); something_to_learn = false; } - else - known.if_not_here_then_nowhere(guess); + // TODO tady + else if(hist.response[0] == 0){ + known.if_not_here_then_nowhere(hist.guess); + } } // Nonzero / nonzero @@ -232,13 +249,16 @@ bool Solver::extract_info(Historic_guess hist) { known.here(guess); } - // All guessed colors are in the sequence - if(response[0] + response[1] == N) - known.all_are_here(guess); - return something_to_learn; } void Solver::learn(vector p_guess, vector p_response) { + // All guessed colors are in the sequence + if(p_response[0] + p_response[1] == N) + known.all_are_here(p_guess); + + if(p_guess[0] == -1) + return; + // Write to history history.push_back({p_guess, p_response}); diff --git a/solver.hpp b/solver.hpp index 51c98d0..74a560a 100644 --- a/solver.hpp +++ b/solver.hpp @@ -16,6 +16,7 @@ public: // Get known information bool can(int n, int col); int final_color(int n); + bool is_empty(int col); void print(); vector> list_all_possibilities(); vector> get_positions_of_colors(vector guess);