#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; // Searched sequence vector sequence; Game(int p_N, int p_M) : N(p_N), M(p_M) { possible = vector>(N, vector(M, 1)); sequence = vector(N, -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; } 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;; } } }; class Solver { Game known; vector> history = {}; public: Solver(int p_N, int p_M) : known({p_N, p_M}) {} vector guess() { // TODO return {}; } void learn_back(vector historic_guess) { // TODO } void learn(vector guess, vector response) { // Reduce color positions if(response[0] == 0 && response[1] == 0) { // None of these colors are there for(int col : guess) known.empty_color(col); } else if(response[1] == 0) { // None at the right spot // Not here for(int n = 0; n < known.N; n++) known.cannot_be(n, guess[n]); } else if(response[0] == 0) { // At least only on the right spot // Get all positions of these colors auto positions_of_colors = vector>(known.M, vector(0)); for(int n = 0; n < known.N; n++) 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); } } else { // TODO nonzero / nonzero } // Learn from previous guesses for(vector past_guess : history) learn_back(past_guess); // Write to history for(int r : response) guess.push_back(r); history.push_back(guess); } void print() { known.print(); } };