選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

base.h 6.1KB

5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
5年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*------------ -------------- -------- --- ----- --- -- - -
  2. * Libre Blackjack - standard blackjack dealer
  3. *
  4. * Copyright (C) 2020 jeremy theler
  5. *
  6. * This file is part of Libre Blackjack.
  7. *
  8. * Libre Blackjack is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Libre Blackjack is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Libre Blackjack. If not, see <http://www.gnu.org/licenses/>.
  20. *------------------- ------------ ---- -------- -- - - -
  21. */
  22. #ifndef BASE_H
  23. #define BASE_H
  24. #include <string>
  25. #include <list>
  26. #include <random>
  27. // TODO: namespace
  28. enum class DealerAction {
  29. None,
  30. StartNewHand,
  31. AskForBets,
  32. DealPlayerFirstCard,
  33. AskForInsurance,
  34. CheckforBlackjacks,
  35. PayOrTakeInsuranceBets,
  36. AskForPlay,
  37. MoveOnToNextHand,
  38. HitDealerHand,
  39. Payout
  40. };
  41. enum class PlayerActionRequired {
  42. None,
  43. Bet,
  44. Insurance,
  45. Play
  46. };
  47. enum class PlayerActionTaken {
  48. None,
  49. // common
  50. Quit,
  51. Help,
  52. Count,
  53. UpcardValue,
  54. Bankroll,
  55. Hands,
  56. Table,
  57. // particular
  58. Bet,
  59. Insure,
  60. DontInsure,
  61. Stand,
  62. Double,
  63. Split,
  64. Hit,
  65. };
  66. // alphabetically-sorted
  67. enum class Suit {
  68. Clubs = 0,
  69. Diamonds = 1,
  70. Hearts = 2,
  71. Spades = 3
  72. };
  73. class Card {
  74. public:
  75. Card(unsigned int);
  76. ~Card() { };
  77. // TODO: delete copy & move
  78. // TODO: decidir si conviene usar getters o public members
  79. Suit getSuit() { return suit; };
  80. unsigned int getNumber() { return number; };
  81. unsigned int getValue() { return value; };
  82. std::string getNumberASCII() { return numberASCII; };
  83. std::string getSuitUTF8() { return suitUTF8; };
  84. Suit suit;
  85. unsigned int number;
  86. unsigned int value;
  87. std::string ascii() {
  88. return numberASCII + suitASCII;
  89. }
  90. std::string utf8(bool single = false) {
  91. return single ? singleUTF8 : numberASCII + suitUTF8;
  92. }
  93. std::string text();
  94. private:
  95. std::string numberASCII;
  96. std::string suitASCII;
  97. std::string suitUTF8;
  98. std::string suitName;
  99. std::string singleUTF8;
  100. };
  101. extern Card card[52];
  102. // TODO: base + daugthers, para diferenciar entre dealer y player y otros juegos
  103. class Hand {
  104. public:
  105. std::list<unsigned int> cards;
  106. // inline on purpose
  107. int total() {
  108. unsigned int soft = 0;
  109. unsigned int n = 0;
  110. unsigned int value = 0;
  111. for (auto tag : cards) {
  112. value = card[tag].value;
  113. n += value;
  114. soft += (value == 11);
  115. }
  116. // this loop should be only executed once if everything works fine
  117. while (n > 21 && soft > 0){
  118. n -= 10;
  119. soft--;
  120. }
  121. return (soft)?(-n):(n);
  122. };
  123. // inline on purpose
  124. bool blackjack() {
  125. return (total() == 21 && cards.size() == 2);
  126. };
  127. // inline on purpose
  128. bool busted() {
  129. return (total() > 21);
  130. }
  131. void draw(bool = true);
  132. };
  133. class PlayerHand : public Hand {
  134. public:
  135. std::list<unsigned int> cards;
  136. int bet = 0;
  137. int id = 0;
  138. bool insured = false;
  139. };
  140. class DealerHand : public Hand {
  141. public:
  142. std::list<unsigned int> cards;
  143. bool holeCardShown = false;
  144. };
  145. class Player {
  146. public:
  147. Player() = default;
  148. ~Player() = default;
  149. // delete copy and move constructors
  150. Player(Player&) = delete;
  151. Player(const Player&) = delete;
  152. Player(Player &&) = delete;
  153. Player(const Player &&) = delete;
  154. /*
  155. PlayerAction getNextAction() {
  156. return nextAction;
  157. }
  158. void setNextAction(PlayerAction a) {
  159. nextAction = a;
  160. }
  161. */
  162. virtual int play() = 0;
  163. PlayerActionRequired actionRequired = PlayerActionRequired::None;
  164. PlayerActionTaken actionTaken = PlayerActionTaken::None;
  165. bool hasSplit = false;
  166. bool hasDoubled = false;
  167. unsigned int flatBet = 0;
  168. unsigned int currentBet = 0;
  169. unsigned int n_hands = 0; // this is different from the dealer's due to splitting
  170. unsigned int n_insured_hands = 0;
  171. unsigned int player_blackjacks = 0;
  172. unsigned int dealer_blackjacks = 0;
  173. unsigned int insured_wins = 0;
  174. unsigned int pushes = 0;
  175. unsigned int losses = 0;
  176. unsigned int wins = 0;
  177. unsigned int blackjack_wins = 0;
  178. // TODO: blackjack_pushes?
  179. bool no_insurance = false;
  180. bool always_insure = false;
  181. double bankroll = 0;
  182. double worst_bankroll = 0;
  183. double total_money_waged = 0;
  184. double current_result = 0;
  185. double mean = 0;
  186. double M2 = 0;
  187. double variance = 0;
  188. std::list<PlayerHand> hands;
  189. std::list<PlayerHand>::iterator currentHand;
  190. };
  191. class Dealer {
  192. public:
  193. Dealer() {};
  194. ~Dealer() {};
  195. // delete copy and move constructors
  196. Dealer(Dealer&) = delete;
  197. Dealer(const Dealer&) = delete;
  198. Dealer(Dealer &&) = delete;
  199. Dealer(const Dealer &&) = delete;
  200. // maybe this first one does not need to be deleted
  201. virtual void shuffle() = 0;
  202. virtual void deal(Player *) = 0;
  203. virtual int dealCard(Hand * = nullptr) = 0;
  204. virtual int process(Player *) = 0;
  205. void setNextAction(DealerAction a) {
  206. next_action = a;
  207. }
  208. void getNextAction(DealerAction a) {
  209. next_action = a;
  210. }
  211. bool getInputNeeded(void) {
  212. return input_needed;
  213. }
  214. void setInputNeeded(bool flag) {
  215. input_needed = flag;
  216. }
  217. bool finished(void) {
  218. return done;
  219. }
  220. bool finished(bool d) {
  221. return (done = d);
  222. }
  223. bool done = false;
  224. bool input_needed = false;
  225. DealerAction next_action = DealerAction::None;
  226. // TODO: most of the games will have a single element, but maybe
  227. // there are games where the dealer has more than one hand
  228. // std::list <Hand> hands;
  229. DealerHand hand;
  230. };
  231. #endif