| blackjack_SOURCES = \ | blackjack_SOURCES = \ | ||||
| src/main.cpp \ | src/main.cpp \ | ||||
| src/blackjack.cpp | |||||
| src/blackjack.cpp \ | |||||
| src/stdinout.cpp |
| setInputNeeded(true); | setInputNeeded(true); | ||||
| } | } | ||||
| void Blackjack::ask() { | |||||
| std::string input_buffer; | |||||
| std::cout << "what do you want to do?" << std::endl; | |||||
| std::cin >> input_buffer; | |||||
| if (input_buffer == "hit" || input_buffer == "h") { | |||||
| player_command = Command::Hit; | |||||
| } else { | |||||
| player_command = Command::None; | |||||
| } | |||||
| } | |||||
| // returns zero if it is a common command and we need to ask again | // returns zero if it is a common command and we need to ask again | ||||
| // returns positive if what was asked was answered | // returns positive if what was asked was answered | ||||
| // returns negative if what was aked was not asnwered or the command does not apply | // returns negative if what was aked was not asnwered or the command does not apply | ||||
| int Blackjack::process() { | |||||
| int Blackjack::process(Command command) { | |||||
| switch (player_command) { | |||||
| switch (command) { | |||||
| case Command::Hit: | case Command::Hit: | ||||
| std::cout << "ok, you hit" << std::endl; | std::cout << "ok, you hit" << std::endl; | ||||
| finished(true); | finished(true); |
| *------------------- ------------ ---- -------- -- - - - | *------------------- ------------ ---- -------- -- - - - | ||||
| */ | */ | ||||
| #ifndef BLACKJACK_H | |||||
| #define BLACKJACK_H | |||||
| enum class DealerAction { | enum class DealerAction { | ||||
| None, | None, | ||||
| StartNewHand, | StartNewHand, | ||||
| void deal(); | void deal(); | ||||
| void ask(); | void ask(); | ||||
| int process(); | |||||
| int process(Command); | |||||
| void setNextAction(DealerAction a) { | void setNextAction(DealerAction a) { | ||||
| double insurance = 0; | double insurance = 0; | ||||
| int bet = 0; | int bet = 0; | ||||
| }; | |||||
| class Player { | |||||
| public: | |||||
| Player() = default; | |||||
| ~Player() = default; | |||||
| // delete copy and move constructors | |||||
| Player(Player&) = delete; | |||||
| Player(const Player&) = delete; | |||||
| Player(Player &&) = delete; | |||||
| Player(const Player &&) = delete; | |||||
| virtual int play(Command *, int *) = 0; | |||||
| }; | }; | ||||
| #endif |
| #include <iostream> | #include <iostream> | ||||
| #include "blackjack.h" | #include "blackjack.h" | ||||
| #include "stdinout.h" | |||||
| int main(int argc, char **argv) { | int main(int argc, char **argv) { | ||||
| // TODO: pass args/conf to the constructor | // TODO: pass args/conf to the constructor | ||||
| Blackjack dealer; | Blackjack dealer; | ||||
| StdInOut player; | |||||
| std::cout << "Let's play" << std::endl; | std::cout << "Let's play" << std::endl; | ||||
| dealer.setNextAction(DealerAction::StartNewHand); | dealer.setNextAction(DealerAction::StartNewHand); | ||||
| Command command = Command::None; | |||||
| while (!dealer.finished()) { | while (!dealer.finished()) { | ||||
| dealer.setInputNeeded(false); | dealer.setInputNeeded(false); | ||||
| // TODO: clean buffers | // TODO: clean buffers | ||||
| if (dealer.getInputNeeded()) { | if (dealer.getInputNeeded()) { | ||||
| do { | do { | ||||
| // TODO: check for too many errors meaning dealer and player do not understand each other | // TODO: check for too many errors meaning dealer and player do not understand each other | ||||
| dealer.ask(); | |||||
| } while (dealer.process() <= 0); | |||||
| player.play(&command, nullptr); | |||||
| } while (dealer.process(command) <= 0); | |||||
| } | } | ||||
| } | } | ||||
| #include <iostream> | |||||
| #include "blackjack.h" | |||||
| #include "stdinout.h" | |||||
| int StdInOut::play(Command *command, int *param) { | |||||
| std::cout << "what do you want to do" << std::endl; | |||||
| std::string input_buffer; | |||||
| std::cin >> input_buffer; | |||||
| if (input_buffer == "hit" || input_buffer == "h") { | |||||
| *command = Command::Hit; | |||||
| } else { | |||||
| *command = Command::None; | |||||
| } | |||||
| return 0; | |||||
| } |
| #ifndef STDINOUT_H | |||||
| #define STDINOUT_H | |||||
| #include "blackjack.h" | |||||
| class StdInOut : Player { | |||||
| public: | |||||
| StdInOut() { }; | |||||
| ~StdInOut() { }; | |||||
| int play(Command *, int *) override; | |||||
| }; | |||||
| #endif |