#pragma once #include #include using std::vector; using std::cout; struct Game { int N, M; // Table of NxM saying which position can hold which color vector> possible; Game(int p_N, int p_M) : N(p_N), M(p_M) { possible = vector>(N, vector(M, 1)); } // Learning functions void cannot_be(int n, int col) { possible[n][col] = false; } void must_be(int must_n, int col) { for(int n = 0; n < N; n++) if(n != must_n) possible[n][col] = 0; } void empty_color(int col) { for(int n = 0; n < N; n++) possible[n][col] = 0; } // Checking int final_color(int n) { int final_col, count = 0; for(int col = 0; col < M; col++) if(possible[n][col]) { final_col = col; count++; } if(count == 1) return final_col; return -1; } // Print known information void print() { cout << " "; for(int col = 0; col < M; col++) cout << col; cout << std::endl; for(int i = 0; i < N; i++) { cout << i; for(auto col : possible[i]) cout << col; cout << std::endl;; } } }; // For remembering guesses with their responses struct Historic_guess { vector guess, response; Historic_guess(vector p_guess, vector p_response) { guess = p_guess; response = p_response; } }; class Solver { Game known; vector history = {}; vector sequence; public: Solver(int p_N, int p_M) : known({p_N, p_M}) { sequence = vector(p_N, -1); } vector guess() { // TODO return {}; } // Clear what we already know Historic_guess clean(Historic_guess hist) { // The correct colors we know for(int n = 0; n < known.N; n++) { if(known.final_color(n) == hist.guess[n]) { hist.guess[n] = -1; hist.response[1] -= 1; } } // The out-of-place colors we know for(int n = 0; n < known.N; n++) { if(hist.guess[n] == -1) continue; for(int i = 0; i < known.N; i++) { if(i == n) continue; if(known.final_color(i) == hist.guess[n]) { hist.guess[n] = -1; hist.response[0] -= 1; } } } return hist; } // Specific reactions void if_not_here_then_nowhere(vector guess) { // Get all positions of these colors auto positions_of_colors = vector>(known.M, vector(0)); for(int n = 0; n < known.N; n++) if(guess[n] > -1) positions_of_colors[guess[n]].push_back(n); // If color can't be anywhere here, it can't be in the sequence for(int col = 0; col < known.M; col++) { int possible_count = 0; for(int n : positions_of_colors[col]) if(known.possible[n][col]) possible_count++; if(possible_count == 0 && positions_of_colors[col].size() > 0) known.empty_color(col); } } void not_here(vector guess) { for(int n = 0; n < known.N; n++) if(guess[n] > -1) known.cannot_be(n, guess[n]); } void empty(vector guess) { for(int col : guess) if(col > -1) known.empty_color(col); } bool learn_back(Historic_guess hist) { bool something_to_learn = true; hist = clean(hist); // TODO Actually learn return something_to_learn; } void learn(vector p_guess, vector p_response) { bool something_to_learn; // A bit of cleaning auto cleaned = clean({p_guess, p_response}); vector guess = cleaned.guess; vector response = cleaned.response; // None of these colors are there if(response[0] == 0 && response[1] == 0) { empty(guess); something_to_learn = false; } // None at the right spot else if(response[1] == 0) { not_here(guess); something_to_learn = false; } // At least only on the right spot else if(response[0] == 0) { if_not_here_then_nowhere(guess); // TODO maybe I can guess which one is right something_to_learn = true; } // TODO nonzero / nonzero else { something_to_learn = true; } // Learn from previous guesses for(int i = 0; i < history.size(); i++) if(!learn_back(history[i])) { // If there is nothing left to learn from the guess history.erase(history.begin()+i); i--; } // Write to history if(something_to_learn) history.push_back({p_guess, p_response}); } void print() { known.print(); } };