Fixed cleaning and if_not_here_then_nowhere
This commit is contained in:
parent
8b5a69ffbc
commit
ea6f60fc29
2 changed files with 34 additions and 13 deletions
46
solver.cpp
46
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<vector<int>> Game::list_all_possibilities() {
|
||||
auto r = vector<vector<int>>(N, vector<int>(0));
|
||||
for(int col = 0; col < M; col++)
|
||||
|
@ -102,7 +108,7 @@ void Game::all_are_here(vector<int> 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<int> p_guess, vector<int> 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<bool> 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<int> p_guess, vector<int> 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});
|
||||
|
||||
|
|
|
@ -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<vector<int>> list_all_possibilities();
|
||||
vector<vector<int>> get_positions_of_colors(vector<int> guess);
|
||||
|
|
Loading…
Reference in a new issue