Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. // TestStuff.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <iostream>
  7. #include <stdlib.h> /* srand, rand */
  8. #include <time.h> /* time */
  9. #include <string>
  10.  
  11. // Blackjack
  12.  
  13. // Suits
  14. // HEART = 0,
  15. // DIAMOND = 1,
  16. // SPADE = 2,
  17. // CLUB = 3
  18.  
  19.  
  20. struct Card
  21. {
  22. int value = 0;
  23. int suit;
  24. };
  25.  
  26. void printCard(Card * card)
  27. {
  28. switch (card->value)
  29. {
  30. case 1:
  31. std::cout << "Ace";
  32. break;
  33. case 11:
  34. std::cout << "Jack";
  35. break;
  36. case 12:
  37. std::cout << "Queen";
  38. break;
  39. case 13:
  40. std::cout << "King";
  41. break;
  42. default:
  43. std::cout << card->value;
  44. }
  45.  
  46. std::cout << " of ";
  47.  
  48. switch (card->suit)
  49. {
  50. case 0:
  51. std::cout << "Hearts\n";
  52. break;
  53. case 1:
  54. std::cout << "Diamonds\n";
  55. break;
  56. case 2:
  57. std::cout << "Spades\n";
  58. break;
  59. case 3:
  60. std::cout << "Clubs\n";
  61. break;
  62. default:
  63. std::cout << "Magic\n";
  64. break;
  65. }
  66. }
  67.  
  68. // A queue type structure
  69. struct Deck
  70. {
  71. Card cards[52];
  72. };
  73.  
  74. void fillDeck(Deck * deck)
  75. {
  76. for (int i = 0; i < 4; i++)
  77. {
  78. for (int j = 1; j <= 13; j++)
  79. {
  80. Card card;
  81. card.value = j;
  82. card.suit = i;
  83.  
  84. deck->cards[(i * 13) + j - 1] = card;
  85. }
  86. }
  87. }
  88.  
  89. void printDeck(Deck * deck)
  90. {
  91. for (int i = 0; i < 4; i++)
  92. {
  93. for (int j = 1; j <= 13; j++)
  94. {
  95. std::cout << deck->cards[(i * 13) + j - 1].value << " ";
  96. }
  97. std::cout << std::endl;
  98. }
  99. }
  100.  
  101. void shuffleDeck(Deck * deck)
  102. {
  103. Card temp;
  104. for (int i = 0; i < 52; i++)
  105. {
  106. int cardA = rand() % 52;
  107. int cardB = rand() % 52;
  108.  
  109. if (cardA == cardB)
  110. continue;
  111. else
  112. {
  113. temp = deck->cards[cardA];
  114. deck->cards[cardA] = deck->cards[cardB];
  115. deck->cards[cardB] = temp;
  116. }
  117. }
  118. }
  119.  
  120. void printCard(Deck * deck, int cardNum)
  121. {
  122. if (cardNum < 52)
  123. printCard(&(deck->cards[cardNum]));
  124. else
  125. std::cout << "Bad cardNum gave, error.\n";
  126. }
  127.  
  128. Card dealCard(Deck * deck, int * deckLoc)
  129. {
  130. (*deckLoc)++;
  131. return deck->cards[((*deckLoc) - 1) % 52];
  132. }
  133.  
  134. struct Hand
  135. {
  136. Card * cards = nullptr;
  137. int size = 0;
  138. };
  139.  
  140. void addCard(Hand * hand, Card * card)
  141. {
  142. Card * temp = new Card[hand->size + 1];
  143.  
  144. for (int i = 0; i < hand->size; i++)
  145. {
  146. temp[i] = hand->cards[i];
  147. }
  148.  
  149. // Add last card then delete and point to new temp array
  150. temp[hand->size] = *card;
  151. delete[] hand->cards;
  152. hand->cards = temp;
  153.  
  154. ++(hand->size);
  155. }
  156.  
  157. void printHand(Hand * hand)
  158. {
  159. for (int i = 0; i < hand->size; i++)
  160. {
  161. printCard(&(hand->cards[i]));
  162. }
  163. }
  164.  
  165. void printHandDebug(Hand * hand)
  166. {
  167. for (int i = 0; i < hand->size; i++)
  168. {
  169. std::cout << hand->cards[i].value << " ";
  170. }
  171. }
  172.  
  173. void clearHand(Hand * hand)
  174. {
  175. delete[] hand->cards;
  176. hand->cards = nullptr;
  177. hand->size = 0;
  178. }
  179.  
  180.  
  181. /*
  182. Calculates the highest hand value you can have under 21 if possible,
  183. if you have multiple aces it will get the highest value you can without
  184. exceeding 21.
  185. */
  186. int handValue(Hand * hand)
  187. {
  188. int val = 0;
  189. int numAces = 0;
  190.  
  191. int cardWorth = 0;
  192. for (int i = 0; i < hand->size; i++)
  193. {
  194. cardWorth = hand->cards[i].value;
  195. if (cardWorth == 11 || cardWorth == 12 || cardWorth == 13)
  196. val += 10;
  197. else if (cardWorth == 1)
  198. {
  199. ++numAces;
  200. }
  201. else
  202. val += cardWorth;
  203. }
  204.  
  205. if (numAces > 0)
  206. {
  207. if ((cardWorth == 20 || cardWorth == 10) && numAces == 1)
  208. return 21;
  209. else
  210. {
  211. // Catch if you have 10 and one ace
  212. if (val + (numAces * 11) == 21)
  213. return 21;
  214. // Catch if aces == 1
  215. else if (val + numAces == 21)
  216. return 21;
  217. // Catch if you have multiple aces and exceed 21
  218. else if (val + numAces > 21)
  219. return val + numAces;
  220. // Do some calculating to see if converting one ace to 11 helps
  221. else if (val + numAces <= 21)
  222. {
  223. if ((val + 11) + (numAces - 1) <= 21)
  224. return (val + 11) + (numAces - 1);
  225. else
  226. return val + numAces;
  227.  
  228. }
  229. // You are already over 21 sorry
  230. else
  231. {
  232. return val + numAces;
  233. }
  234. }
  235. }
  236.  
  237. return val;
  238. }
  239.  
  240. void dealHands(Hand * player, Hand * dealer, Deck * deck, int * deckLoc)
  241. {
  242. std::cout << "\n\nDealing Cards\n\n";
  243. addCard(dealer, &(dealCard(deck, deckLoc)));
  244. addCard(player, &(dealCard(deck, deckLoc)));
  245. addCard(dealer, &(dealCard(deck, deckLoc)));
  246. addCard(player, &(dealCard(deck, deckLoc)));
  247.  
  248.  
  249. std::cout << "You have:\n";;
  250.  
  251. printHand(player);
  252.  
  253. std::cout << "\n\nThe dealer is showing a(n) ";
  254. printCard(&(dealer->cards[1]));
  255.  
  256. }
  257.  
  258. void playRound(Deck * deck, int numPlayers, int deckLoc)
  259. {
  260.  
  261. printDeck(deck);
  262.  
  263. Hand dealerHand;
  264. Hand playerHand;
  265.  
  266. bool playing = true;
  267. bool end = false;
  268.  
  269. std::string action;
  270.  
  271. dealHands(&playerHand, &dealerHand, deck, &deckLoc);
  272.  
  273. while (playing)
  274. {
  275.  
  276. /*if (handValue(dealerHand.cards, dealerHand.size) == 21)
  277. {
  278. std::cout << "Sorry dealer has 21, you lose.\n\nPlease play again.";
  279. done = true;
  280. playing = false;
  281. break;
  282. }*/
  283.  
  284. if (handValue(&playerHand) == 21 && end == false)
  285. {
  286. std::cout << "\n\nYou have 21 nothing to do but see what falls.\n\n";
  287. }
  288.  
  289. std::cout << "\n\nWhat would you like to do?\n";
  290. std::cin >> action;
  291.  
  292. if (end == false)
  293. {
  294. if (action == "hit")
  295. {
  296. addCard(&playerHand, &(dealCard(deck, &deckLoc)));
  297. std::cout << "\nYou now have:\n";
  298. printHand(&playerHand);
  299.  
  300. std::cout << "Hand Value: " << handValue(&playerHand) << std::endl;
  301.  
  302. if (handValue(&playerHand) > 21)
  303. {
  304. std::cout << "\nOuch, looks like you busted! Please try again!";
  305. }
  306. }
  307. else if (action == "stay")
  308. {
  309. end = true;
  310. std::cout << "\nDealer is showing: \n";
  311. printHand(&dealerHand);
  312.  
  313. while (handValue(&dealerHand) < handValue(&playerHand) && handValue(&dealerHand) <= 21)
  314. {
  315. std::cout << "\nDealer is drawing a card\n";
  316. addCard(&dealerHand, &(dealCard(deck, &deckLoc)));
  317. std::cout << "Dealer is showing: \n";
  318. printHand(&dealerHand);
  319.  
  320. }
  321.  
  322. if (handValue(&dealerHand) == 21)
  323. {
  324. std::cout << "\nDealer has Blackjack! Sorry! Please play again.";
  325. }
  326. else if (handValue(&dealerHand) == handValue(&playerHand))
  327. {
  328. std::cout << "\nRats! The Dealer tied meaning he won! Please play again";
  329. }
  330. else if (handValue(&dealerHand) > 21)
  331. {
  332. std::cout << "\nDealer busts! You won! Wanna try to best us again?";
  333. }
  334. else if (handValue(&dealerHand) > handValue(&playerHand) && handValue(&dealerHand) < 21)
  335. {
  336. std::cout << "\nSorry dealer has the higher hand, please play again.";
  337. }
  338. else
  339. {
  340. std::cout << "\n*vuvuzela sounds* Congratulations! You won! *vuvuzela sounds* Wanna try to best us again?";
  341. }
  342.  
  343. }
  344. }
  345.  
  346. if (action == "play")
  347. {
  348. std::cout << "\n\n\n---New Round---\n\n";
  349. clearHand(&playerHand);
  350. clearHand(&dealerHand);
  351. dealHands(&playerHand, &dealerHand, deck, &deckLoc);
  352. end = false;
  353. }
  354. else if (action == "reshuffle")
  355. {
  356. shuffleDeck(deck);
  357. std::cout << "\n\n\n---New Round---\n\n";
  358. clearHand(&playerHand);
  359. clearHand(&dealerHand);
  360. dealHands(&playerHand, &dealerHand, deck, &deckLoc);
  361. }
  362. else if (action == "quit")
  363. {
  364. playing = false;
  365. }
  366. else if(action == "help")
  367. {
  368. std::cout << "\n\n\nValid Commands are:\nhelp play reshuffle quit stay hit\n\n";
  369. }
  370. }
  371.  
  372. delete[] playerHand.cards;
  373. delete[] dealerHand.cards;
  374. }
  375.  
  376. int main()
  377. {
  378. srand(time(NULL));
  379.  
  380. Deck newDeck;
  381. fillDeck(&newDeck);
  382. //printDeck(&newDeck);
  383. std::cout << "\nShuffling Deck\n";
  384.  
  385. shuffleDeck(&newDeck);
  386. printDeck(&newDeck);
  387. std::cout << "\n\n\nStarting Game\n\n";
  388.  
  389. playRound(&newDeck, 1, 0);
  390.  
  391. return 0;
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement