From 2f32921976692acd9db1909f799c52046df3892e Mon Sep 17 00:00:00 2001 From: Matuush Date: Mon, 4 Nov 2024 14:50:15 +0100 Subject: [PATCH] Better code segmentation --- format.hpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++ gamestate.hpp | 66 +++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 1 + solver.hpp | 74 +++++---------------------------------------------- validate.hpp | 65 -------------------------------------------- 5 files changed, 141 insertions(+), 133 deletions(-) create mode 100644 format.hpp create mode 100644 gamestate.hpp diff --git a/format.hpp b/format.hpp new file mode 100644 index 0000000..a339cd8 --- /dev/null +++ b/format.hpp @@ -0,0 +1,68 @@ +#pragma once +#include +#include + +using std::vector; +using std::string; +using std::to_string; + +#define YELLOW "\033[33m" +#define GREEN "\033[32m" +#define RED "\033[31m" +#define BACK "\033[0m" + +string format_response(vector response) { + return YELLOW + to_string(response[0]) + BACK + " / " + + GREEN + to_string(response[1]) + BACK + "\n"; +} +string format_guess(vector guess) { + string r = ""; + for(int col : guess) + r += to_string(col) + " "; + r += "\n"; + return r; +} + +string format_guess_history(vector sequence, vector guess) { + vector r(sequence.size()); + int N = sequence.size(); + // Find correct values + for(int i = 0; i < N; i++) { + if(sequence[i] == guess[i]) { + r[i] = GREEN + to_string(guess[i]) + BACK; + guess[i] = -1; + sequence[i] = -1; + } + } + + // Find values that are there somewhere + for(int i = 0; i < N; i++) { + if(guess[i] == -1) continue; + for(int j = 0; j < N; j++) { + if(sequence[j] == guess[i]) { + r[i] = YELLOW + to_string(guess[i]) + BACK; + guess[i] = -1; + sequence[j] = -1; + break; + } + } + } + + // Remaining values + for(int i = 0; i < N; i++) { + if(guess[i] != -1) + r[i] = to_string(guess[i]); + } + + string r_string = ""; + for(string s : r) + r_string += s + " "; + return r_string; +} + +string format_lost_sequence(vector sequence) { + string r = ""; + for(int col : sequence) + r += RED + to_string(col) + BACK + " "; + return r; +} diff --git a/gamestate.hpp b/gamestate.hpp new file mode 100644 index 0000000..a8cf3d7 --- /dev/null +++ b/gamestate.hpp @@ -0,0 +1,66 @@ +#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; + } +}; diff --git a/main.cpp b/main.cpp index 09d67fd..93fcca0 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include "solver.hpp" #include "gen.hpp" #include "validate.hpp" +#include "format.hpp" using std::cout; using std::cin; diff --git a/solver.hpp b/solver.hpp index 67c701d..a44e9cf 100644 --- a/solver.hpp +++ b/solver.hpp @@ -1,69 +1,5 @@ #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; - } -}; +#include "gamestate.hpp" class Solver { Game known; @@ -73,6 +9,8 @@ public: Solver(int p_N, int p_M) : known({p_N, p_M}) { sequence = vector(p_N, -1); } + + // Guessing vector guess() { // TODO return {}; @@ -134,7 +72,7 @@ public: known.empty_color(col); } - bool learn_back(Historic_guess hist) { + bool extract_info(Historic_guess hist) { bool something_to_learn; // A bit of cleaning @@ -169,11 +107,11 @@ public: } void learn(vector p_guess, vector p_response) { // Learn something new - bool something_to_learn = learn_back({p_guess, p_response}); + bool something_to_learn = extract_info({p_guess, p_response}); // Learn from previous guesses for(int i = 0; i < history.size(); i++) - if(!learn_back(history[i])) { + if(!extract_info(history[i])) { // If there is nothing left to learn from the guess history.erase(history.begin()+i); i--; diff --git a/validate.hpp b/validate.hpp index ad0700b..e6fbf0c 100644 --- a/validate.hpp +++ b/validate.hpp @@ -1,11 +1,7 @@ #pragma once #include -#include using std::vector; -using std::string; -using std::to_string; - vector validate(vector sequence, vector guess) { int N = sequence.size(); @@ -38,64 +34,3 @@ vector validate(vector sequence, vector guess) { return {r_somewhere, r_correct}; } - -#define YELLOW "\033[33m" -#define GREEN "\033[32m" -#define RED "\033[31m" -#define BACK "\033[0m" - -string format_response(vector response) { - return YELLOW + to_string(response[0]) + BACK + " / " + - GREEN + to_string(response[1]) + BACK + "\n"; -} -string format_guess(vector guess) { - string r = ""; - for(int col : guess) - r += to_string(col) + " "; - r += "\n"; - return r; -} - -string format_guess_history(vector sequence, vector guess) { - vector r(sequence.size()); - int N = sequence.size(); - // Find correct values - for(int i = 0; i < N; i++) { - if(sequence[i] == guess[i]) { - r[i] = GREEN + to_string(guess[i]) + BACK; - guess[i] = -1; - sequence[i] = -1; - } - } - - // Find values that are there somewhere - for(int i = 0; i < N; i++) { - if(guess[i] == -1) continue; - for(int j = 0; j < N; j++) { - if(sequence[j] == guess[i]) { - r[i] = YELLOW + to_string(guess[i]) + BACK; - guess[i] = -1; - sequence[j] = -1; - break; - } - } - } - - // Remaining values - for(int i = 0; i < N; i++) { - if(guess[i] != -1) - r[i] = to_string(guess[i]); - } - - string r_string = ""; - for(string s : r) - r_string += s + " "; - return r_string; -} - -string format_lost_sequence(vector sequence) { - string r = ""; - for(int col : sequence) - r += RED + to_string(col) + BACK + " "; - return r; -}