logik/main.cpp

93 lines
2.2 KiB
C++

#include "global.hpp"
#include "solver.hpp"
#define GUESS_COUNT 10
// 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");
string gen = player == HUMAN ? RANDOM : get_input("-g", args, "Who generates the seque", RANDOM);
bool human_player = player == HUMAN;
// 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 << '\n';
for(int n = 0; n < N; n++)
cin >> sequence[n];
}
// Clearing
if(N < 1) return 1;
if(M < 1) return 1;
if(player != HUMAN && player != BOT) return 1;
if(gen != HUMAN && gen != RANDOM) return 1;
for(auto col : sequence)
if(col < 0 || col >= M) return 1;
// Human info
if(human_player)
cout << "\nGuesses are " << N << " space-separated numbers from 0 to " << M-1 << '\n'
<< "Responses are in format [correct out of place] / [correct in place]" << "\n\n";
// Init solver
Solver bot(N, M);
// Guessing
vector<vector<int>> history = {};
for(int guesses = 0; guesses < GUESS_COUNT; guesses++) {
vector<int> guess(N);
Response response;
// Human playing
if(human_player) {
cout << "Guess " << history.size() << " : ";
for(int n = 0; n < N; n++)
cin >> guess[n];
response = validate(sequence, guess);
cout << format_response(response);
}
// Bot playing
else {
guess = bot.guess();
response = validate(sequence, guess);
bot.learn(guess, response);
}
history.push_back(guess);
if(history.back() == sequence) break;
}
// Terminal clearing
cout << '\n';
// Print game history
if(human_player)
cout << "History\n";
for(auto guess : history)
cout << format_guess_history(sequence, guess) << '\n';
// Loss
if(history.back() != sequence) {
cout << format_lost_sequence(sequence) << '\n';
return 1;
}
return 0;
}