Guest User

Untitled

a guest
Dec 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. //BRYAN BECK
  2. //ASSIGNMENT 3
  3. //DUE 12-1-16
  4. //LAST MODIFIED 12/1/16
  5.  
  6. #include <cstdlib>
  7. #include <string>
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. class Cards {
  13. public:
  14. string deck[52];
  15. string drawCard();
  16. void fill_deck();
  17. void setSeed();
  18. void reset();
  19. int size;
  20. Cards();
  21. };
  22.  
  23. class Player {
  24. public:
  25. string hand[52];
  26. int hand_size;
  27. int calculate_total();
  28. void addToHand(string card);
  29. void showCards();
  30. void reset_hand();
  31. Player();
  32. void convert_card_to_string(string card);
  33. int convert_card_to_value(string card);
  34.  
  35. };
  36.  
  37. class Dealer : public Player {
  38. public:
  39. void showFirstCards();
  40. void whoWins(Player you);
  41. Dealer();
  42.  
  43. };
  44.  
  45. int main() {
  46. Player you;
  47. Dealer dealer;
  48. Cards deck;
  49.  
  50. char ans, f_answ;
  51. int total = 0, total_p = 0, total_d = 0;
  52. do {
  53. deck.setSeed();
  54.  
  55. you.addToHand(deck.drawCard());
  56. you.addToHand(deck.drawCard());
  57.  
  58. dealer.addToHand(deck.drawCard());
  59. dealer.addToHand(deck.drawCard());
  60.  
  61. cout << endl;
  62.  
  63. cout << "YOUR CARDS: \n";
  64. cout << endl;
  65. you.showCards();
  66.  
  67.  
  68. total = you.calculate_total();
  69.  
  70. if (total == 21) {
  71.  
  72. cout << endl << "BlackJack! YOU WIN! \n";
  73. goto mylabel;
  74. }
  75.  
  76. cout << endl << "DEALER CARDS: \n";
  77. dealer.showFirstCards();
  78.  
  79. cout << endl << "Your total is: " << total << endl;
  80.  
  81. if (total < 21)
  82. do {
  83. cout << endl << "Would you like to hit(y/n)?\n";
  84. cin>> ans;
  85.  
  86. cout<<endl;
  87.  
  88. if (ans == 'y' || ans == 'Y') {
  89. you.addToHand(deck.drawCard());
  90. total_p = you.calculate_total();
  91. cout << "YOUR CARD's: \n";
  92. you.showCards();
  93. cout << endl << "Your total is: " << total_p << endl;
  94. if (total_p > 21) {
  95. cout << " You bust\n";
  96. goto mylabel;
  97. }
  98. }
  99.  
  100. } while (ans == 'y' || ans == 'Y');
  101.  
  102. if (ans == 'n' || ans == 'N') {
  103. total_p = you.calculate_total();
  104. total_d = dealer.calculate_total();
  105. }
  106.  
  107. while (total_d < 17) {
  108. dealer.addToHand(deck.drawCard());
  109. total_d = dealer.calculate_total();
  110. // cout<< total_d <<endl;//<< "DEALERS CARDS: \n";
  111. // dealer.showCards();
  112. }
  113. cout << "DEALERS CARD's: \n";
  114. dealer.showCards();
  115. cout<<endl;
  116.  
  117.  
  118. if (total_d > 21)
  119. cout << "Dealer Busts! \n";
  120.  
  121.  
  122. cout << "Dealer total is: " << total_d << endl<<endl;
  123.  
  124. dealer.whoWins(you);
  125.  
  126. mylabel:
  127.  
  128. cout<< endl << "Would you like to play again(y/n)?\n";
  129. cin>>f_answ;
  130.  
  131. deck.reset();
  132. dealer.reset_hand();
  133. you.reset_hand();
  134.  
  135. } while (f_answ == 'y' || f_answ == 'Y');
  136.  
  137. return 0;
  138. }
  139.  
  140. void Cards::setSeed() {
  141. int ui;
  142. cout << "Please enter a positive integer to seed random numbers: \n";
  143. cin>> ui;
  144. srand(ui);
  145. }
  146.  
  147. void Cards::reset() {
  148. size = 52;
  149. }
  150.  
  151. string Cards::drawCard() {
  152. int random;
  153. string temp;
  154. random = rand() % size;
  155. temp = deck[random];
  156. deck[random] = deck[size - 1];
  157. deck[size - 1] = temp;
  158. size--;
  159.  
  160. return temp;
  161. }
  162.  
  163. void Cards::fill_deck() {
  164. deck[0] = "h2";
  165. deck[1] = "h3";
  166. deck[2] = "h4";
  167. deck[3] = "h5";
  168. deck[4] = "h6";
  169. deck[5] = "h7";
  170. deck[6] = "h8";
  171. deck[7] = "h9";
  172. deck[8] = "h10";
  173. deck[9] = "hJ";
  174. deck[10] = "hQ";
  175. deck[11] = "hK";
  176. deck[12] = "hA";
  177. deck[13] = "s2";
  178. deck[14] = "s3";
  179. deck[15] = "s4";
  180. deck[16] = "s5";
  181. deck[17] = "s6";
  182. deck[18] = "s7";
  183. deck[19] = "s8";
  184. deck[20] = "s9";
  185. deck[21] = "s10";
  186. deck[22] = "sJ";
  187. deck[23] = "sQ";
  188. deck[24] = "sK";
  189. deck[25] = "sA";
  190. deck[26] = "d2";
  191. deck[27] = "d3";
  192. deck[28] = "d4";
  193. deck[29] = "d5";
  194. deck[30] = "d6";
  195. deck[31] = "d7";
  196. deck[32] = "d8";
  197. deck[33] = "d9";
  198. deck[34] = "d10";
  199. deck[35] = "dJ";
  200. deck[36] = "dQ";
  201. deck[37] = "dK";
  202. deck[38] = "dA";
  203. deck[39] = "c2";
  204. deck[40] = "c3";
  205. deck[41] = "c4";
  206. deck[42] = "c5";
  207. deck[43] = "c6";
  208. deck[44] = "c7";
  209. deck[45] = "c8";
  210. deck[46] = "c9";
  211. deck[47] = "c10";
  212. deck[48] = "cJ";
  213. deck[49] = "cQ";
  214. deck[50] = "cK";
  215. deck[51] = "cA";
  216. }
  217.  
  218. void Player::convert_card_to_string(string card) {
  219. string suit, value;
  220. suit = card.substr(0, 1);
  221. value = card.substr(1, 1);
  222.  
  223.  
  224.  
  225. if (value == "2")
  226. cout << "<Two>";
  227. else if (value == "3")
  228. cout << "<Three>";
  229. else if (value == "4")
  230. cout << "<Four>";
  231. else if (value == "5")
  232. cout << "<Five>";
  233. else if (value == "6")
  234. cout << "<Six>";
  235. else if (value == "7")
  236. cout << "<Seven>";
  237. else if (value == "8")
  238. cout << "<Eight>";
  239. else if (value == "9")
  240. cout << "<Nine>";
  241. else if (value == "1")
  242. cout << "<Ten>";
  243. else if (value == "J")
  244. cout << "<Jack>";
  245. else if (value == "Q")
  246. cout << "<Queen>";
  247. else if (value == "K")
  248. cout << "<King>";
  249. else if (value == "A")
  250. cout << "<Ace>";
  251.  
  252. if (suit == "h")
  253. cout << "<Hearts>";
  254. else if (suit == "s")
  255. cout << "<Spades>";
  256. else if (suit == "c")
  257. cout << "<Clubs>";
  258. else if (suit == "d")
  259. cout << "<Diamonds>";
  260.  
  261. cout << endl;
  262. }
  263.  
  264. int Player::convert_card_to_value(string card) {
  265. int score;
  266. string value;
  267. value = card.substr(1);
  268.  
  269. if (value == "2")
  270. score = 2;
  271. else if (value == "3")
  272. score = 3;
  273. else if (value == "4")
  274. score = 4;
  275. else if (value == "5")
  276. score = 5;
  277. else if (value == "6")
  278. score = 6;
  279. else if (value == "7")
  280. score = 7;
  281. else if (value == "8")
  282. score = 8;
  283. else if (value == "9")
  284. score = 9;
  285. else if (value == "10")
  286. score = 10;
  287. else if (value == "J")
  288. score = 10;
  289. else if (value == "Q")
  290. score = 10;
  291. else if (value == "K")
  292. score = 10;
  293. else if (value == "A")
  294. score = 11;
  295.  
  296. return score;
  297.  
  298. }
  299.  
  300. int Player::calculate_total() {
  301. int temp = 0, total = 0, count = 0;
  302. // string card, value;
  303. //
  304. // value = card.substr(1, 1);
  305.  
  306. for (int i = 0; i < hand_size; i++) {
  307. temp = convert_card_to_value(hand[i]);
  308. total = temp + total;
  309. }
  310. if (total > 21) {
  311. for (int i = 0; i < hand_size; i++) {
  312. if (hand[i] == "hA" || hand[i] == "cA" || hand[i] == "sA" || hand[i] == "dA") {
  313. total = total - 10;
  314. count++;
  315. }
  316. if (count > 1 && total + 10 <= 21)
  317. //if (total + 10 < 21)
  318. total = total + 10;
  319. // else
  320. // total = total515
  321.  
  322. }
  323. }
  324.  
  325.  
  326.  
  327.  
  328. return total;
  329. }
  330.  
  331. void Player::addToHand(string card) {
  332. hand[hand_size++] = card;
  333. }
  334.  
  335. void Player::showCards() {
  336. for (int i = 0; i < hand_size; i++) {
  337. convert_card_to_string(hand[i]);
  338. }
  339. }
  340.  
  341. void Player::reset_hand() {
  342. for (int i = 0; i < hand_size; i++) {
  343. hand[i] = " ";
  344. }
  345. hand_size = 0;
  346.  
  347. }
  348.  
  349. void Dealer::showFirstCards() {
  350. convert_card_to_string(hand[0]);
  351. cout << "<Hidden>\n";
  352. }
  353.  
  354. void Dealer::whoWins(Player you) {
  355. int total_p, total_d;
  356. total_d = calculate_total();
  357. total_p = you.calculate_total();
  358.  
  359. if (total_d > 21)
  360. cout << "You win\n";
  361. else if (total_d == total_p)
  362. cout << "TIE \n";
  363. else if (total_d > 21 && total_p > 21)
  364. cout << "You Lose \n";
  365. else if (total_p > 21)
  366. cout << "You lose \n";
  367. else if (total_p > total_d)
  368. cout << "You win!\n";
  369. else if (total_d > total_p)
  370. cout << "You Lose. \n";
  371.  
  372. }
  373.  
  374. Player::Player() {
  375. hand_size = 0;
  376. }
  377.  
  378. Dealer::Dealer() {
  379. }
  380.  
  381. Cards::Cards() {
  382. size = 52;
  383. fill_deck();
  384. }
Add Comment
Please, Sign In to add comment