86 lines
1.9 KiB
C++
86 lines
1.9 KiB
C++
#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;
|
|
}
|
|
};
|