Guest User

blackjack c++

a guest
Dec 11th, 2013
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int countDigits(int cash);
  7. int dealCard(int deck[][16], int& total, int shh, int& aces, int& width, int& size);
  8. string idCard(int value, int& width);
  9. string idSuit(int value, int& width);
  10. void generateDeck(int deck[][16], int& size);
  11. void playBlackjack(int& money, int& split, int& splitCard, int& bet);
  12.  
  13. int main(){
  14. int cash = 100;
  15. int split = 0;
  16. string ans = "y";
  17.  
  18. //Greeting
  19. cout << "Welcome to Black Jack!" << endl;
  20.  
  21. //Keep playing
  22. while (ans == "y"&&cash > 0){
  23. int splitCard = 0;
  24. int bet = 0;
  25.  
  26. playBlackjack(cash, split, splitCard, bet);
  27.  
  28. if (cash > 0){
  29. cout << endl << "Replay? (Enter y/n)" << endl;
  30. cin >> ans;
  31. }
  32.  
  33. split = 0;
  34. }
  35.  
  36. if (cash > 0){
  37. cout << "You leave with $" << cash << "." << endl;
  38. cout << "Goodbye!" << endl;
  39. }
  40.  
  41. else{
  42. cout << endl << "You are out of money. " << endl;
  43. }
  44.  
  45. return 0;
  46. }
  47.  
  48. int countDigits(int cash){
  49. if (cash > 9)
  50. return 1 + countDigits(cash / 10);
  51. else
  52. return 1;
  53. }
  54.  
  55. int dealCard(int deck[][16], int& total, int shh, int& aces, int& width, int& size){
  56. int num;
  57. int suit;
  58.  
  59. width = 0;
  60.  
  61. do{
  62. num = rand() % 13;
  63. suit = rand() % 16;
  64. } while (deck[num][suit] == 0);
  65.  
  66. if (shh == 0)
  67. cout << idCard(num + 1, width) << " of " << idSuit(deck[num][suit], width);
  68.  
  69. deck[num][suit] = 0;
  70.  
  71. if (num == 10 || num == 11 || num == 12)
  72. num = 9;
  73.  
  74. if (num == 0){
  75. aces = aces + 1;
  76.  
  77. if (total < 11)
  78. num = 10;
  79. }
  80.  
  81. total = total + num + 1;
  82. size = size - 1;
  83.  
  84. return num;
  85. }
  86.  
  87. string idCard(int value, int& width){
  88. string card;
  89. //Switch statement that gives actual name to card values
  90. switch (value){
  91. case 1: card = "Ace";
  92. break;
  93. case 2: card = "Two";
  94. break;
  95. case 3: card = "Three";
  96. width = width + 2;
  97. break;
  98. case 4: card = "Four";
  99. width = width + 1;
  100. break;
  101. case 5: card = "Five";
  102. width = width + 1;
  103. break;
  104. case 6: card = "Six";
  105. break;
  106. case 7: card = "Seven";
  107. width = width + 2;
  108. break;
  109. case 8: card = "Eight";
  110. width = width + 2;
  111. break;
  112. case 9: card = "Nine";
  113. width = width + 1;
  114. break;
  115. case 10: card = "Ten";
  116. break;
  117. case 11: card = "Jack";
  118. width = width + 1;
  119. break;
  120. case 12: card = "Queen";
  121. width = width + 2;
  122. break;
  123. case 13: card = "King";
  124. width = width + 1;
  125. break;
  126.  
  127. }
  128.  
  129. return card;
  130. }
  131.  
  132. string idSuit(int value, int& width){
  133. string suit;
  134.  
  135. switch (value){
  136. case 1: suit = "Clubs";
  137. break;
  138. case 2: suit = "Diamonds";
  139. width = width + 3;
  140. break;
  141. case 3: suit = "Hearts";
  142. width = width + 1;
  143. break;
  144. case 4: suit = "Spades";
  145. width = width + 1;
  146. break;
  147. }
  148.  
  149. return suit;
  150. }
  151.  
  152. void generateDeck(int deck[][16], int& size){
  153. for (int i = 0; i < 13; i = i + 1){
  154. for (int j = 0; j < 16; j = j + 1)
  155. deck[i][j] = j / 4 + 1;
  156. }
  157.  
  158. size = 208;
  159. }
  160.  
  161. void playBlackjack(int& cash, int& split, int& splitCard, int& bet){
  162. int aces;
  163. int card1;
  164. int card2;
  165. int goof;
  166. int oppAces;
  167. int oppTotal;
  168. static int size = 0;
  169. int shh = 0;
  170. int text;
  171. int total;
  172. int width = 0;
  173. string move = "nb";
  174.  
  175. static int deck[13][16];
  176.  
  177. if (size <= 52)
  178. generateDeck(deck, size);
  179.  
  180. if (splitCard == 0){
  181. cout << endl << ">How much would you like to bet? (You have $" << cash << ")" << endl;
  182. cin >> bet;
  183.  
  184. while (bet<0 || bet>cash){
  185. cout << "(!) Invalid. Please enter a valid amount. " << endl;
  186. cin >> bet;
  187. }
  188. }
  189.  
  190. cout << bet;
  191.  
  192. aces = 0;
  193. oppAces = 0;
  194. oppTotal = 0;
  195. text = 0;
  196. total = 0;
  197.  
  198. if (split == 0){
  199. cout << "You are dealt a card face up and one face down." << endl;
  200. cout << "You:" << setw(29) << " " << endl;
  201. cout << "Face Down: ";
  202. card1 = dealCard(deck, total, shh, aces, width, size);
  203. cout << setw(9 - width) << "*" << endl;
  204. }
  205.  
  206. if (split == 1){
  207. cout << endl;
  208. total = splitCard + 1;
  209. }
  210.  
  211. cout << "Face Up: ";
  212. card2 = dealCard(deck, total, shh, oppAces, width, size);
  213. cout << setw(11 - width) << " " << endl;
  214.  
  215. if (total >= 10)
  216. width = 1;
  217. else
  218. width = 0;
  219.  
  220. cout << "Total value: " << total << setw(18 - width) << " " << endl;
  221. cout << "Dealer:" << setw(26) << " " << endl;
  222. cout << "Face Up: ";
  223. goof = dealCard(deck, oppTotal, shh, oppAces, width, size);
  224. cout << setw(11 - width) << " " << endl;
  225.  
  226. shh = 1;
  227. goof = dealCard(deck, oppTotal, shh, oppAces, width, size);
  228. shh = 0;
  229.  
  230. if (total < 21){
  231. cout << endl;
  232. if (card1 == card2){
  233. cout << "(h)it, (s)tand, (d)ouble down, or split a (p)air?" << endl;
  234. cin >> move;
  235.  
  236. if (move == "p"){
  237. split = 1;
  238.  
  239. cout << endl;
  240.  
  241. cout << " Splitting Pair " << endl;
  242. cout << " " << endl;
  243.  
  244. cout << endl;
  245. cout << "Pair 1" << endl;
  246. playBlackjack(cash, split, card1, bet);
  247.  
  248. cout << endl;
  249. cout << "Pair 2" << endl;
  250.  
  251. playBlackjack(cash, split, card2, bet);
  252. }
  253. }
  254. else{
  255. cout << "(h)it, (s)tand, or (d)ouble down?" << endl;
  256. cin >> move;
  257. }
  258. }
  259.  
  260. if (move == "d"){
  261. cout << endl;
  262.  
  263. cout << " Double Down " << endl;
  264. cout << " " << endl;
  265.  
  266. cout << "New Card: ";
  267. goof = dealCard(deck, total, shh, aces, width, size);
  268. cout << setw(8 - width) << "|" << endl;
  269. cout << " " << endl;
  270.  
  271. }
  272.  
  273. while (move != "s"&&move != "nb"&&move != "d"&&move != "p"&&total <= 21){
  274. while (move == "h"){
  275. cout << endl << " " << endl;
  276. cout << "New Card: ";
  277. goof = dealCard(deck, total, shh, aces, width, size);
  278. cout << setw(10 - width) << " ";
  279.  
  280. if (total > 21 && aces > 0){
  281. total = total - 10;
  282. aces = aces - 1;
  283. }
  284.  
  285. if (total >= 10)
  286. width = width + 1;
  287.  
  288. cout << endl << "Total value: " << total << setw(17) << "*" << endl;
  289. cout << " " << endl;
  290.  
  291. if (total <= 21){
  292. cout << endl;
  293. cout << "(h)it or (s)tand?" << endl;
  294. cin >> move;
  295. }
  296. else
  297. move = "s";
  298. }
  299.  
  300. while (move != "h"&&move != "s"){
  301. cout << "Please enter either 'h' or 's')" << endl;
  302. cin >> move;
  303. }
  304. }
  305.  
  306. if (move != "p"){
  307. if (total <= 21){
  308. while (oppTotal < 17){
  309. cout << endl << " " << endl;
  310. cout << "The Dealer's new card is: ";
  311. dealCard(deck, oppTotal, shh, oppAces, width, size);
  312. cout << setw(6 - width) << " ";
  313.  
  314. width = 0;
  315.  
  316. if (oppTotal >= 10)
  317. width = width + 1;
  318.  
  319. cout << endl << "The Dealer's total is: " << oppTotal << setw(20 - width) << " ";
  320. text = 1;
  321. }
  322. }
  323.  
  324. if (total <= 21 && text != 0)
  325. cout << endl << " " << endl;
  326.  
  327. cout << endl;
  328. cout << " " << endl;
  329.  
  330.  
  331. if (move == "d"){
  332. if (oppTotal > 21)
  333. cash = cash + bet;
  334. else if (total > 21)
  335. cash = cash - bet;
  336. else if (total > oppTotal)
  337. cash = cash + bet;
  338. else if (total < oppTotal)
  339. cash = cash - bet;
  340. }
  341.  
  342. if (total == 21 && move == "nb"){
  343. cash = cash*1.5;
  344.  
  345. cout << " " << endl;
  346. cout << " Twenty-One, Black Jack! " << endl;
  347.  
  348. cout << " " << endl;
  349. cout << "You have: $" << cash << "." << setw(13 - countDigits(cash)) << " " << endl;
  350. cout << " " << endl;
  351. }
  352. else if (total > 21){
  353. cash = cash - bet;
  354.  
  355. cout << "You busted!" << setw(22) << "." << endl;
  356. cout << " " << endl;
  357. cout << "You have: $" << cash << " " << setw(19 - countDigits(cash)) << "." << endl;
  358. }
  359. else if (oppTotal > 21){
  360. cash = cash + bet;
  361.  
  362. cout << "I busted!" << setw(24) << " " << endl;
  363. cout << " " << endl;
  364. cout << "You have: $" << cash << " " << setw(19 - countDigits(cash)) << " " << endl;
  365. }
  366. else if (total > oppTotal){
  367. cash = cash + bet;
  368.  
  369. if (total >= 10)
  370. width = 1;
  371. else
  372. width = 0;
  373.  
  374. cout << "Your total: " << total << setw(22 - width) << " " << endl;
  375.  
  376. if (oppTotal >= 10)
  377. width = 1;
  378. else
  379. width = 0;
  380.  
  381. cout << "Dealer's total: " << oppTotal << setw(18 - width) << " " << endl;
  382. cout << " " << endl;
  383. cout << "You won!" << setw(25) << " " << endl;
  384. cout << "You have: $" << cash << " " << setw(19 - countDigits(cash)) << " " << endl;
  385. }
  386. else if (oppTotal > total){
  387. cash = cash - bet;
  388.  
  389. if (total >= 10)
  390. width = 1;
  391. else
  392. width = 0;
  393.  
  394. cout << "Your total: " << total << setw(22 - width) << " " << endl;
  395.  
  396. if (oppTotal >= 10)
  397. width = 1;
  398. else
  399. width = 0;
  400.  
  401. cout << "Dealer's total: " << oppTotal << setw(18 - width) << " " << endl;
  402. cout << " " << endl;
  403. cout << "I won!" << setw(27) << " " << endl;
  404. cout << "You have: $" << cash << " " << setw(19 - countDigits(cash)) << " " << endl;
  405. }
  406. else if (move != "p"){
  407. if (total > 10)
  408. width = 1;
  409. else
  410. width = 0;
  411.  
  412. cout << "Your total: " << total << setw(22 - width) << " " << endl;
  413.  
  414. if (oppTotal > 10)
  415. width = 1;
  416. else
  417. width = 0;
  418.  
  419. cout << "Dealer's total: " << oppTotal << setw(18 - width) << " " << endl;
  420. cout << " " << endl;
  421. cout << "Its a tie!" << setw(22) << " " << endl;
  422. cout << "Money returned. " << endl;
  423. }
  424.  
  425. cout << " " << endl;
  426. }
  427. }
Advertisement
Add Comment
Please, Sign In to add comment