Added support for commandline arguments and defaults

This commit is contained in:
Matúš Púll 2024-11-07 18:41:26 +01:00
parent 2304c0277a
commit d405a2db95
2 changed files with 41 additions and 31 deletions

27
input.hpp Normal file
View file

@ -0,0 +1,27 @@
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::cout;
using std::cin;
string get_param(string arg, vector<string> args) {
for(int i = 0; i < args.size(); i += 2) {
if(args[i] == arg && i < args.size()-1)
return string(args[i+1]);
}
return "-1";
}
string get_input(string arg, vector<string> args, string prompt_text, string default_arg) {
string r = get_param(arg, args);
if(r == "-1") {
cout << prompt_text << "(default=" << default_arg << ") : ";
getline(cin, r);
if(r == "")
r = default_arg;;
}
return r;
}

View file

@ -1,15 +1,10 @@
#include <iostream>
#include <string>
#include <vector>
#include "solver.hpp"
#include "gen.hpp"
#include "validate.hpp"
#include "format.hpp"
#include "input.hpp"
using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::getline;
// To avoid typos
@ -17,33 +12,21 @@ using std::getline;
#define HUMAN "human"
#define RANDOM "random"
int main(void) {
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
cout << "Length of sequence (default=5) : ";
string string_N; getline(cin, string_N);
int N = (string_N == "") ? 5 : stoi(string_N);
cout << "Number of colors (default=8) : ";
string string_M; getline(cin, string_M);
int M = (string_M == "") ? 8 : stoi(string_M);
cout << "Who plays [" << UNDERLINE << HUMAN << BACK << "/" << BOT << "] : ";
string player; getline(cin, player);
if(player == "") player = HUMAN;
bool learn = 0;
if(player == HUMAN) {
cout << "Do you want to know what bot learns [" << UNDERLINE << "y" << BACK << "/n] : ";
string reply; getline(cin, reply);
if(reply == "") reply = "y";
learn = (reply == "y");
}
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 == BOT) {
cout << "Who generates the sequence [" << HUMAN << "/" << RANDOM << "]: ";
cin >> gen;
}
if(player != HUMAN)
gen = get_input("-g", args, "Who generates the seque", RANDOM);
// Generate the sequence
auto sequence = vector<int>(N);