Browse Source

command line args

master
gtheler 5 years ago
parent
commit
91c7dff3c6
4 changed files with 101 additions and 14 deletions
  1. +2
    -5
      src/blackjack.cpp
  2. +92
    -4
      src/conf.cpp
  3. +6
    -3
      src/conf.h
  4. +1
    -2
      src/main.cpp

+ 2
- 5
src/blackjack.cpp View File

} }
break; break;
/*
case DealerAction::PayOrTakeInsuranceBets:
break;
*/
case DealerAction::AskForPlay: case DealerAction::AskForPlay:


player->actionRequired = PlayerActionRequired::Play; player->actionRequired = PlayerActionRequired::Play;
player->currentHand->render(); player->currentHand->render();
std::cout << "dealer upcard is " << card[upCard].utf8() << std::endl; std::cout << "dealer upcard is " << card[upCard].utf8() << std::endl;
std::cout << "your total is " << playerTotal << std::endl;
std::cout << "your total is " << player->currentHand->total() << std::endl;
std::cout << "play please" << std::endl; std::cout << "play please" << std::endl;
return; return;
break; break;
case DealerAction::MoveOnToNextHand: case DealerAction::MoveOnToNextHand:
// see if we finished all the player's hands // see if we finished all the player's hands
if (++player->currentHand != player->hands.end()) { if (++player->currentHand != player->hands.end()) {

+ 92
- 4
src/conf.cpp View File

#include <fstream> #include <fstream>
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <getopt.h>


// source https://www.walletfox.com/course/parseconfigfile.php // source https://www.walletfox.com/course/parseconfigfile.php


Configuration::Configuration(int argc, char **argv) { Configuration::Configuration(int argc, char **argv) {
const struct option longopts[] = {
///op+conf+option `-c`path or `--conf=`path
///op+conf+desc Specify the path to the [configuration file]. Default is `./blackjack.conf`
{"conf", required_argument, NULL, 'c'},

///op+hands+option `-n`$n$ or `--hands=`$n$
///op+hands+desc Specify the number of hands to play. Corresponds to the `hands` variable in the [configuration file].
{"hands", required_argument, NULL, 'n'},
///op+decks+option `-d`$n$ or `--decks=`$n$
///op+decks+desc Specify the number of decks to use in the shoe. Corresponds to the `decks` variable in the [configuration file].
{"decks", required_argument, NULL, 'd'},
///op+flatbet+option `-f` or `--flatbet`
///op+flatbet+desc Do not ask for the amount to bet before starting a new hand and use a flat unit bet. Corresponds to the `flat_bet` variable in the [configuration file].
{"flatbet", optional_argument, NULL, 'f'},

///op+general+option `--`configuration_variable`[=`*value*`]`
///op+general+desc Any configuration variable from the [configuration file] can be set from the command line.
///op+general+desc For example, passing `--no_insurance` is like setting `no_insurance = 1` in the configuration file. Command-line options override configuration options.
///op+internal+option `-i` or `--internal`
///op+internal+desc Use the internal player to play against itself. See [internal player] for details.
{"internal", no_argument, NULL, 'i'},
///op+help+option `-h` or `--help`
///op+help+desc Print this informative help message on standard output and exit successfully.
{"help", no_argument, NULL, 'h'},
///op+version+option `-v` or `--version`
///op+version+desc Print the version number and licensing information of Hello on standard output and then exit successfully.
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0}
};

int i, optc; int i, optc;
int option_index = 0; int option_index = 0;
int opterr = 0;
while ((optc = getopt_long_only(argc, argv, "c:hvd:n:if:", longopts, &option_index)) != -1) {
switch (optc) {
case 'h':
show_help = true;
break;
case 'v':
show_version = true;
break;
case 'c':
std::cout << "custom conf " << optarg << std::endl;
configFilePath = std::move(std::string(optarg));
explicitConfigFile = true;
break;
case 'd':
data["decks"] = std::move(std::string(optarg));
break;
case 'n':
data["hands"] = std::move(std::string(optarg));
break;
case 'i':
data["player"] = "internal";
break;
case 'f':
if (optarg != NULL) {
data["flat_bet"] = optarg;
} else {
data["flat_bet"] = "1";
}
break;
case '?':
{
std::string line(argv[optind - 1]);
std::size_t delimiterPos = line.find("=");
if (delimiterPos != std::string::npos) {
auto name = line.substr(0, delimiterPos);
auto value = line.substr(delimiterPos + 1);
data[name] = value;
} else {
data[line] = "true";
}
}
break;
default:
break;
}
}
// TODO: if blackjack.conf exists parse it
parseConfigFile();
std::ifstream default_file(configFilePath);
if (default_file.good()) {
readConfigFile(configFilePath, explicitConfigFile);
}
} }


int Configuration::parseConfigFile(std::string filePath) {
int Configuration::readConfigFile(std::string filePath, bool mandatory) {
// std::ifstream is RAII, i.e. no need to call close // std::ifstream is RAII, i.e. no need to call close
std::ifstream fileStream(filePath); std::ifstream fileStream(filePath);
if (delimiterPos != std::string::npos) { if (delimiterPos != std::string::npos) {
name = line.substr(0, delimiterPos); name = line.substr(0, delimiterPos);
value = line.substr(delimiterPos + 1); value = line.substr(delimiterPos + 1);
data[name] = value;
if (!exists(name)) {
data[name] = value;
}
// TODO: add another map of string to bools to mark wheter the option was used or not // TODO: add another map of string to bools to mark wheter the option was used or not
} else { } else {
// TODO: warning? // TODO: warning?

+ 6
- 3
src/conf.h View File

Configuration(int, char **); Configuration(int, char **);
~Configuration(); ~Configuration();


int readConfigFile(std::string);
int readConfigFile(std::string, bool = false);
bool exists(std::string key) { return !(data.find(key) == data.end()); }
// bool exists(std::string key) { return !(data.find(key) == data.end()); }
bool exists(std::string key) { return (data.count(key) != 0); }


void show(void); void show(void);
std::string getString(std::string); std::string getString(std::string);
private: private:
std::map<std::string, std::string> data;
std::string configFilePath = "./blackjack.conf";
bool explicitConfigFile = false;
bool show_help = false; bool show_help = false;
bool show_version = false; bool show_version = false;
bool show_bar = false; bool show_bar = false;
bool bar_already_alloced = false; bool bar_already_alloced = false;
std::map<std::string, std::string> data;
unsigned int hands_per_char = false; unsigned int hands_per_char = false;
}; };

+ 1
- 2
src/main.cpp View File

Dealer *dealer = nullptr; Dealer *dealer = nullptr;
Player *player = nullptr; Player *player = nullptr;
Configuration conf;
// conf.show();
Configuration conf(argc, argv);
// TODO: read the args/conf to know what kind of dealer and player we are having // TODO: read the args/conf to know what kind of dealer and player we are having
// TODO: pass args/conf to the constructor // TODO: pass args/conf to the constructor

Loading…
Cancel
Save