| @@ -17,4 +17,5 @@ blackjack_LDADD = $(all_libraries) | |||
| blackjack_SOURCES = \ | |||
| src/main.cpp \ | |||
| src/blackjack.cpp | |||
| src/blackjack.cpp \ | |||
| src/stdinout.cpp | |||
| @@ -37,25 +37,12 @@ void Blackjack::deal() { | |||
| 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 positive if what was asked was answered | |||
| // 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: | |||
| std::cout << "ok, you hit" << std::endl; | |||
| finished(true); | |||
| @@ -20,6 +20,9 @@ | |||
| *------------------- ------------ ---- -------- -- - - - | |||
| */ | |||
| #ifndef BLACKJACK_H | |||
| #define BLACKJACK_H | |||
| enum class DealerAction { | |||
| None, | |||
| StartNewHand, | |||
| @@ -74,7 +77,7 @@ class Blackjack { | |||
| void deal(); | |||
| void ask(); | |||
| int process(); | |||
| int process(Command); | |||
| void setNextAction(DealerAction a) { | |||
| @@ -108,12 +111,19 @@ class Blackjack { | |||
| double insurance = 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 | |||
| @@ -23,6 +23,7 @@ | |||
| #include <iostream> | |||
| #include "blackjack.h" | |||
| #include "stdinout.h" | |||
| int main(int argc, char **argv) { | |||
| @@ -30,10 +31,13 @@ int main(int argc, char **argv) { | |||
| // TODO: pass args/conf to the constructor | |||
| Blackjack dealer; | |||
| StdInOut player; | |||
| std::cout << "Let's play" << std::endl; | |||
| dealer.setNextAction(DealerAction::StartNewHand); | |||
| Command command = Command::None; | |||
| while (!dealer.finished()) { | |||
| dealer.setInputNeeded(false); | |||
| // TODO: clean buffers | |||
| @@ -41,8 +45,8 @@ int main(int argc, char **argv) { | |||
| if (dealer.getInputNeeded()) { | |||
| do { | |||
| // 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); | |||
| } | |||
| } | |||
| @@ -0,0 +1,20 @@ | |||
| #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; | |||
| } | |||
| @@ -0,0 +1,12 @@ | |||
| #ifndef STDINOUT_H | |||
| #define STDINOUT_H | |||
| #include "blackjack.h" | |||
| class StdInOut : Player { | |||
| public: | |||
| StdInOut() { }; | |||
| ~StdInOut() { }; | |||
| int play(Command *, int *) override; | |||
| }; | |||
| #endif | |||