96 lines
2.3 KiB
C++
96 lines
2.3 KiB
C++
#include "global.hpp"
|
|
#include "solver.hpp"
|
|
|
|
using std::getline;
|
|
|
|
// To avoid typos
|
|
#define BOT "bot"
|
|
#define HUMAN "human"
|
|
#define RANDOM "random"
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
// Parse into string
|
|
vector<string> args(0);
|
|
for(int i = 1; i < argc; i++)
|
|
args.push_back(argv[i]);
|
|
|
|
// Take game parameters
|
|
int N = stoi(get_input("-n", args, "Length of sequence", "5"));
|
|
int M = stoi(get_input("-m", args, "Number of colors", "8"));
|
|
string player = get_input("-p", args, string("Who plays [")+HUMAN+"/"+BOT+"]", "bot");
|
|
bool learn = "y" == get_input("-l", args, "Do you want to know what bot learns [y/n]", "y");;
|
|
string gen = RANDOM;
|
|
if(player != HUMAN)
|
|
gen = get_input("-g", args, "Who generates the seque", RANDOM);
|
|
|
|
// Generate the sequence
|
|
auto sequence = vector<int>(N);
|
|
if(gen == RANDOM)
|
|
sequence = generate(N, M);
|
|
else if(gen == HUMAN) {
|
|
cout << "Enter " << N << " space-separated numbers from 0 to " << M-1 << std::endl;
|
|
for(int n = 0; n < N; n++)
|
|
cin >> sequence[n];
|
|
}
|
|
|
|
// Terminal clearing
|
|
cout << std::endl;
|
|
|
|
// Human info
|
|
if(player == HUMAN) {
|
|
cout << "Guesses are " << N << " space-separated numbers from 0 to " << M-1 << std::endl
|
|
<< "Responses are in format [correct out of place] / [correct in place]" << std::endl;
|
|
cout << std::endl;
|
|
}
|
|
|
|
// Init solver
|
|
Solver bot(N, M);
|
|
|
|
// Guessing
|
|
vector<vector<int>> history = {};
|
|
for(int guesses = 0; guesses < 10; guesses++) {
|
|
vector<int> guess(N), response;
|
|
cout << "Guess " << history.size() << " : ";
|
|
|
|
// Bot playin
|
|
if(player == BOT) {
|
|
guess = bot.guess();
|
|
cout << format_guess(guess);
|
|
|
|
response = validate(sequence, guess);
|
|
cout << format_response(response);
|
|
bot.learn(guess, response);
|
|
}
|
|
|
|
// Human playing
|
|
else if(player == HUMAN) {
|
|
for(int n = 0; n < N; n++)
|
|
cin >> guess[n];
|
|
|
|
response = validate(sequence, guess);
|
|
cout << format_response(response);
|
|
|
|
if(learn) {
|
|
bot.learn(guess, response);
|
|
bot.print();
|
|
}
|
|
}
|
|
|
|
history.push_back(guess);
|
|
if(history.back() == sequence) break;
|
|
}
|
|
|
|
// Terminal clearing
|
|
cout << std::endl;
|
|
|
|
// Print game history
|
|
cout << "History\n";
|
|
for(auto guess : history)
|
|
cout << format_guess_history(sequence, guess) << std::endl;
|
|
|
|
if(history.back() != sequence)
|
|
cout << format_lost_sequence(sequence) << std::endl;
|
|
|
|
return 0;
|
|
}
|