Made the strategy work

This commit is contained in:
Matúš Púll 2024-12-26 23:09:21 +01:00
parent c095843041
commit 604f229cd8
2 changed files with 21 additions and 6 deletions

View file

@ -2,7 +2,7 @@
void Solver::generate_set(vector<int> carry) { void Solver::generate_set(vector<int> carry) {
if(carry.size() == N) { if(carry.size() == N) {
possible.push_back(carry); possible.insert(carry);
return; return;
} }
@ -21,17 +21,29 @@ vector<int> Solver::guess() {
return minimax({}).guess; return minimax({}).guess;
} }
void Solver::learn(vector<int> guess, Response response) { void Solver::learn(vector<int> guess, Response response) {
vector<vector<int>> next_possible(0); set<vector<int>> next_possible;
for(auto sequence : possible) for(auto sequence : possible)
if(validate(sequence, guess) == response) if(validate(sequence, guess) == response)
next_possible.push_back(sequence); next_possible.insert(sequence);
possible = next_possible; possible = next_possible;
} }
// TODO // TODO
int Solver::get_weight(vector<int> guess) { int Solver::get_weight(vector<int> guess) {
return 0; // Indexing by N*somewhere + correct and holding how many sequences got that response
vector<int> response_count(N*N+1, 0);
for(auto sequence : possible) {
Response response = validate(sequence, guess);
response_count[N*response.somewhere + response.correct]++;
}
// Get highest possible number of sequences left
int max = 0;
for(int count : response_count)
if(count > max)
max = count;
return max - possible.count(guess);
} }
Weighed_guess Solver::minimax(vector<int> carry) { Weighed_guess Solver::minimax(vector<int> carry) {
if(carry.size() == N) if(carry.size() == N)
@ -42,7 +54,7 @@ Weighed_guess Solver::minimax(vector<int> carry) {
carry.push_back(col); carry.push_back(col);
Weighed_guess next = minimax(carry); Weighed_guess next = minimax(carry);
if(next.weight > best.weight) if(best.weight == -1 || next.weight < best.weight)
best = next; best = next;
carry.pop_back(); carry.pop_back();

View file

@ -1,6 +1,9 @@
#pragma once #pragma once
#include <set>
#include "global.hpp" #include "global.hpp"
using std::set;
// For deciding the best guess // For deciding the best guess
struct Weighed_guess { struct Weighed_guess {
int weight; int weight;
@ -12,7 +15,7 @@ struct Weighed_guess {
// Solving the game // Solving the game
class Solver { class Solver {
int N, M; int N, M;
vector<vector<int>> possible = vector<vector<int>>(0); set<vector<int>> possible;
public: public:
Solver(int N, int M); Solver(int N, int M);