Bladeren bron

command line args

master
gtheler 5 jaren geleden
bovenliggende
commit
91c7dff3c6
4 gewijzigde bestanden met toevoegingen van 101 en 14 verwijderingen
  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 Bestand weergeven

@@ -273,10 +273,6 @@ void Blackjack::deal(Player *player) {
}
break;
/*
case DealerAction::PayOrTakeInsuranceBets:
break;
*/
case DealerAction::AskForPlay:

player->actionRequired = PlayerActionRequired::Play;
@@ -286,10 +282,11 @@ void Blackjack::deal(Player *player) {
player->currentHand->render();
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;
return;
break;
case DealerAction::MoveOnToNextHand:
// see if we finished all the player's hands
if (++player->currentHand != player->hands.end()) {

+ 92
- 4
src/conf.cpp Bestand weergeven

@@ -4,20 +4,106 @@
#include <fstream>
#include <algorithm>
#include <map>
#include <getopt.h>

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

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 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 fileStream(filePath);
@@ -39,7 +125,9 @@ int Configuration::parseConfigFile(std::string filePath) {
if (delimiterPos != std::string::npos) {
name = line.substr(0, delimiterPos);
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
} else {
// TODO: warning?

+ 6
- 3
src/conf.h Bestand weergeven

@@ -30,9 +30,10 @@ class Configuration {
Configuration(int, char **);
~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);
@@ -41,11 +42,13 @@ class Configuration {
std::string getString(std::string);
private:
std::map<std::string, std::string> data;
std::string configFilePath = "./blackjack.conf";
bool explicitConfigFile = false;
bool show_help = false;
bool show_version = false;
bool show_bar = false;
bool bar_already_alloced = false;
std::map<std::string, std::string> data;
unsigned int hands_per_char = false;
};

+ 1
- 2
src/main.cpp Bestand weergeven

@@ -34,8 +34,7 @@ int main(int argc, char **argv) {
Dealer *dealer = 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: pass args/conf to the constructor

Laden…
Annuleren
Opslaan