Better code segmentation
This commit is contained in:
parent
f009a278ea
commit
2f32921976
5 changed files with 141 additions and 133 deletions
68
format.hpp
Normal file
68
format.hpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
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<int> response) {
|
||||
return YELLOW + to_string(response[0]) + BACK + " / " +
|
||||
GREEN + to_string(response[1]) + BACK + "\n";
|
||||
}
|
||||
string format_guess(vector<int> guess) {
|
||||
string r = "";
|
||||
for(int col : guess)
|
||||
r += to_string(col) + " ";
|
||||
r += "\n";
|
||||
return r;
|
||||
}
|
||||
|
||||
string format_guess_history(vector<int> sequence, vector<int> guess) {
|
||||
vector<string> 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<int> sequence) {
|
||||
string r = "";
|
||||
for(int col : sequence)
|
||||
r += RED + to_string(col) + BACK + " ";
|
||||
return r;
|
||||
}
|
66
gamestate.hpp
Normal file
66
gamestate.hpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using std::vector;
|
||||
using std::cout;
|
||||
|
||||
struct Game {
|
||||
int N, M;
|
||||
// Table of NxM saying which position can hold which color
|
||||
vector<vector<bool>> possible;
|
||||
Game(int p_N, int p_M) : N(p_N), M(p_M) {
|
||||
possible = vector<vector<bool>>(N, vector<bool>(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<int> guess, response;
|
||||
Historic_guess(vector<int> p_guess, vector<int> p_response) {
|
||||
guess = p_guess;
|
||||
response = p_response;
|
||||
}
|
||||
};
|
1
main.cpp
1
main.cpp
|
@ -4,6 +4,7 @@
|
|||
#include "solver.hpp"
|
||||
#include "gen.hpp"
|
||||
#include "validate.hpp"
|
||||
#include "format.hpp"
|
||||
|
||||
using std::cout;
|
||||
using std::cin;
|
||||
|
|
74
solver.hpp
74
solver.hpp
|
@ -1,69 +1,5 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using std::vector;
|
||||
using std::cout;
|
||||
|
||||
struct Game {
|
||||
int N, M;
|
||||
// Table of NxM saying which position can hold which color
|
||||
vector<vector<bool>> possible;
|
||||
Game(int p_N, int p_M) : N(p_N), M(p_M) {
|
||||
possible = vector<vector<bool>>(N, vector<bool>(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<int> guess, response;
|
||||
Historic_guess(vector<int> p_guess, vector<int> 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<int>(p_N, -1);
|
||||
}
|
||||
|
||||
// Guessing
|
||||
vector<int> 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<int> p_guess, vector<int> 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--;
|
||||
|
|
65
validate.hpp
65
validate.hpp
|
@ -1,11 +1,7 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::to_string;
|
||||
|
||||
|
||||
vector<int> validate(vector<int> sequence, vector<int> guess) {
|
||||
int N = sequence.size();
|
||||
|
@ -38,64 +34,3 @@ vector<int> validate(vector<int> sequence, vector<int> 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<int> response) {
|
||||
return YELLOW + to_string(response[0]) + BACK + " / " +
|
||||
GREEN + to_string(response[1]) + BACK + "\n";
|
||||
}
|
||||
string format_guess(vector<int> guess) {
|
||||
string r = "";
|
||||
for(int col : guess)
|
||||
r += to_string(col) + " ";
|
||||
r += "\n";
|
||||
return r;
|
||||
}
|
||||
|
||||
string format_guess_history(vector<int> sequence, vector<int> guess) {
|
||||
vector<string> 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<int> sequence) {
|
||||
string r = "";
|
||||
for(int col : sequence)
|
||||
r += RED + to_string(col) + BACK + " ";
|
||||
return r;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue