From d405a2db956e3b84e5310dd20b31fec7310f065e Mon Sep 17 00:00:00 2001 From: Matuush Date: Thu, 7 Nov 2024 18:41:26 +0100 Subject: [PATCH] Added support for commandline arguments and defaults --- input.hpp | 27 +++++++++++++++++++++++++++ main.cpp | 45 ++++++++++++++------------------------------- 2 files changed, 41 insertions(+), 31 deletions(-) create mode 100644 input.hpp diff --git a/input.hpp b/input.hpp new file mode 100644 index 0000000..12c5216 --- /dev/null +++ b/input.hpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using std::vector; +using std::string; +using std::cout; +using std::cin; + +string get_param(string arg, vector 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 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; +} diff --git a/main.cpp b/main.cpp index b17571e..65f0f6d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,10 @@ -#include -#include -#include #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 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(N);