Cleaning historic guess didn't check for -1

This commit is contained in:
Matúš Púll 2024-11-07 23:40:48 +01:00
parent 9a2589c553
commit a444c5db1c
3 changed files with 5 additions and 88 deletions

View file

@ -1,86 +0,0 @@
#include "global.hpp"
#include <algorithm>
#include <chrono>
#include <random>
class Game {
// Table of NxM saying which position can hold which color
vector<vector<bool>> possible;
std::default_random_engine random_engine;
public:
int N, M;
Game(int p_N, int p_M) : N(p_N), M(p_M) {
possible = vector<vector<bool>>(N, vector<bool>(M, 1));
unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();
random_engine = std::default_random_engine(seed);
}
// Learning functions
void cannot_be(int n, int col) {
possible[n][col] = false;
}
void must_be(int n, int must_col) {
for(int col = 0; col < M; col++)
if(col != must_col)
possible[n][col] = 0;
}
void empty_color(int col) {
for(int n = 0; n < N; n++)
possible[n][col] = 0;
}
// Checking
bool can(int n, int col) {
return possible[n][col];
}
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;
}
// Get 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;;
}
}
// Get all possible colors for all positions
vector<vector<int>> list_all_possibilities() {
auto r = vector<vector<int>>(N, vector<int>(0));
for(int col = 0; col < M; col++)
for(int n = 0; n < N; n++)
if(possible[n][col])
r[n].push_back(col);
for(int n = 0; n < N; n++)
std::shuffle(r[n].begin(), r[n].end(), random_engine);
return r;
}
};
// 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;
}
};

View file

@ -169,6 +169,8 @@ void Solver::print() {
Historic_guess Solver::clean(Historic_guess hist) {
// The in-place colors we know
for(int n = 0; n < N; n++) {
if(hist.guess[n] == -1)
continue;
if(known.final_color(n) == hist.guess[n]) {
hist.guess[n] = -1;
hist.response[1] -= 1;
@ -180,11 +182,12 @@ Historic_guess Solver::clean(Historic_guess hist) {
if(hist.guess[n] == -1)
continue;
for(int i = 0; i < N; i++) {
if(i == n)
if(i == n || hist.guess[i] == -1)
continue;
if(known.final_color(i) == hist.guess[n]) {
hist.guess[n] = -1;
hist.response[0] -= 1;
break;
}
}
}

View file

@ -31,7 +31,6 @@ public:
void not_here(vector<int> guess);
void empty(vector<int> guess);
void all_are_here(vector<int> guess);
};
// For remembering guesses with their responses
@ -40,6 +39,7 @@ struct Historic_guess {
Historic_guess(vector<int> p_guess, vector<int> p_response);
};
// Solving the game
class Solver {
Game known;
int N, M;