| hola = pianola | |||||
| pistola | |||||
| hands = 1 | |||||
| flat_bet = 0 | |||||
| PlayerActionRequired actionRequired = PlayerActionRequired::None; | PlayerActionRequired actionRequired = PlayerActionRequired::None; | ||||
| PlayerActionTaken actionTaken = PlayerActionTaken::None; | PlayerActionTaken actionTaken = PlayerActionTaken::None; | ||||
| // bool hasDoubled = false; | |||||
| bool bustedAllHands = false; | bool bustedAllHands = false; | ||||
| unsigned int currentSplits = 0; | unsigned int currentSplits = 0; | ||||
| unsigned int flatBet = 1; | |||||
| unsigned int currentBet = 0; | unsigned int currentBet = 0; | ||||
| unsigned int n_hands = 0; // this is different from the dealer's due to splitting | unsigned int n_hands = 0; // this is different from the dealer's due to splitting | ||||
| unsigned int handsInsured = 0; | unsigned int handsInsured = 0; | ||||
| unsigned int handsDoubled = 0; | unsigned int handsDoubled = 0; | ||||
| unsigned int blackjacksPlayer = 0; | unsigned int blackjacksPlayer = 0; | ||||
| unsigned int losses = 0; | unsigned int losses = 0; | ||||
| // TODO: blackjack_pushes? | // TODO: blackjack_pushes? | ||||
| unsigned int flat_bet = 1; | |||||
| bool no_insurance = false; | bool no_insurance = false; | ||||
| bool always_insure = false; | bool always_insure = false; | ||||
| #include "blackjack.h" | #include "blackjack.h" | ||||
| Blackjack::Blackjack() : mt19937(dev_random()), fiftyTwoCards(0, 51) { | |||||
| std::cout << "I'm your Blackjack dealer!" << std::endl; | |||||
| Blackjack::Blackjack(Configuration &conf) : mt19937(dev_random()), fiftyTwoCards(0, 51) { | |||||
| if (conf.exists("n_hands")) { | |||||
| n_hands = conf.getInt("n_hands"); | |||||
| } else if (conf.exists("hands")) { | |||||
| n_hands = conf.getInt("hands"); | |||||
| } | |||||
| // TODO: seed instead of dev_random | // TODO: seed instead of dev_random | ||||
| std::random_device random_device; | std::random_device random_device; | ||||
| lastPass = false; | lastPass = false; | ||||
| } | } | ||||
| if (player->flatBet) { | |||||
| player->currentHand->bet = player->flatBet; | |||||
| if (player->flat_bet) { | |||||
| player->currentHand->bet = player->flat_bet; | |||||
| nextAction = DealerAction::DealPlayerFirstCard; | nextAction = DealerAction::DealPlayerFirstCard; | ||||
| } else { | } else { | ||||
| nextAction = DealerAction::AskForBets; | nextAction = DealerAction::AskForBets; |
| #ifndef BLACKJACK_H | #ifndef BLACKJACK_H | ||||
| #define BLACKJACK_H | #define BLACKJACK_H | ||||
| #include "base.h" | #include "base.h" | ||||
| #include "conf.h" | |||||
| class Blackjack : public Dealer { | class Blackjack : public Dealer { | ||||
| public: | public: | ||||
| Blackjack(); | |||||
| Blackjack(Configuration &); | |||||
| ~Blackjack(); | ~Blackjack(); | ||||
| void shuffle() override; | void shuffle() override; |
| name = line.substr(0, delimiterPos); | name = line.substr(0, delimiterPos); | ||||
| value = line.substr(delimiterPos + 1); | value = line.substr(delimiterPos + 1); | ||||
| data[name] = value; | data[name] = value; | ||||
| // TODO: add another map of string to bools to mark wheter the option was used or not | |||||
| } | } | ||||
| } | } | ||||
| } else { | } else { | ||||
| } | } | ||||
| int Configuration::getInt(std::string key) { | |||||
| auto it = data.find(key); | |||||
| return (it != data.end()) ? std::stoi(it->second) : 0; | |||||
| } | |||||
| Configuration::~Configuration() { | Configuration::~Configuration() { | ||||
| data.clear(); | data.clear(); |
| public: | public: | ||||
| Configuration(std::string = "", bool = false); | Configuration(std::string = "", bool = false); | ||||
| ~Configuration(); | ~Configuration(); | ||||
| bool exists(std::string key) { return !(data.find(key) == data.end()); } | |||||
| void show(void); | void show(void); | ||||
| bool getBool(std::string); | |||||
| int getInt(std::string); | |||||
| std::string getString(std::string); | |||||
| private: | private: | ||||
| std::map<std::string, std::string> data; | std::map<std::string, std::string> data; |
| Player *player = nullptr; | Player *player = nullptr; | ||||
| Configuration conf; | Configuration conf; | ||||
| conf.show(); | |||||
| // conf.show(); | |||||
| // 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 | ||||
| dealer = new Blackjack(); | |||||
| dealer = new Blackjack(conf); | |||||
| if (isatty(1)) { | if (isatty(1)) { | ||||
| player = new Tty(); | |||||
| player = new Tty(conf); | |||||
| } else { | } else { | ||||
| player = new StdInOut(); | player = new StdInOut(); | ||||
| } | } | ||||
| // TODO: player strategy from file | // TODO: player strategy from file | ||||
| dealer->nextAction = DealerAction::StartNewHand; | dealer->nextAction = DealerAction::StartNewHand; | ||||
| while (!dealer->finished()) { | while (!dealer->finished()) { |
| #include <readline/history.h> | #include <readline/history.h> | ||||
| #endif | #endif | ||||
| #include "conf.h" | |||||
| #include "blackjack.h" | #include "blackjack.h" | ||||
| #include "tty.h" | #include "tty.h" | ||||
| Tty::Tty(void) { | |||||
| Tty::Tty(Configuration &conf) { | |||||
| if (conf.exists("flat_bet")) { | |||||
| flat_bet = conf.getInt("flat_bet"); | |||||
| } else if (conf.exists("flat_bet")) { | |||||
| flat_bet = conf.getInt("flat_bet"); | |||||
| } | |||||
| #ifdef HAVE_LIBREADLINE | #ifdef HAVE_LIBREADLINE | ||||
| prompt = cyan + " > " + reset; | prompt = cyan + " > " + reset; | ||||
| #endif | #endif |
| class Tty : public Player { | class Tty : public Player { | ||||
| public: | public: | ||||
| Tty(); | |||||
| Tty(Configuration &); | |||||
| ~Tty() { | ~Tty() { | ||||
| std::cout << "bye!" << std::endl; | std::cout << "bye!" << std::endl; | ||||
| }; | }; |