Advertisement
jaybeau21

Case Study

Apr 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.48 KB | None | 0 0
  1. /*Case Study: Black Jack
  2. Jarod Beaumariage, Brandi Sumey, Desmond Hull 4-10-2017*/
  3.  
  4. #include <iostream>
  5. #include <string>
  6. #include <windows.h>
  7. #include <iomanip>
  8. #include <ctime>
  9. #include "MyConsole.h"
  10.  
  11. using namespace std;
  12.  
  13. void DrawCard(int &card)
  14. {
  15. /* Generates a random card between an A and a K
  16. A random card value from 2 to 4 returned as card*/
  17.  
  18. srand (time(NULL));
  19.  
  20. card = rand() % 12 + 2;
  21. }
  22.  
  23. //==================================================================================================================
  24.  
  25. void NameCard(int value, string &name)
  26. {
  27. /* Names each card based on value given
  28. value >= 2 and value <= 14
  29. Name of card returned as name*/
  30.  
  31. if (value == 2)
  32. {
  33. name = "2";
  34. }
  35. else if (value == 3)
  36. {
  37. name = "3";
  38. }
  39. else if (value == 4)
  40. {
  41. name = "4";
  42. }
  43. else if (value == 5)
  44. {
  45. name = "5";
  46. }
  47. else if (value == 6)
  48. {
  49. name = "6";
  50. }
  51. else if (value == 7)
  52. {
  53. name = "7";
  54. }
  55. else if (value == 8)
  56. {
  57. name = "8";
  58. }
  59. else if (value == 9)
  60. {
  61. name = "9";
  62. }
  63. else if (value == 10)
  64. {
  65. name = "10";
  66. }
  67. else if (value == 11)
  68. {
  69. name = "A";
  70. }
  71. else if (value == 12)
  72. {
  73. name = "J";
  74. }
  75. else if (value == 13)
  76. {
  77. name = "K";
  78. }
  79. else
  80. {
  81. name = "Q";
  82. }
  83. }
  84.  
  85. //==================================================================================================================
  86.  
  87. void PlayAgain(char &again)
  88. {
  89. /*Gives the player the option to play again
  90. Again returned as either y or n*/
  91.  
  92. //Deal again
  93. cout << "\nDo you want another hand dealt? Enter either 'y' or 'n': ";
  94. cin >> again;
  95. ClearScreen();
  96.  
  97. //Error in again
  98. if(again != 'y' && again != 'n')
  99. {
  100. do
  101. {
  102. ClearScreen();
  103. cout << "Error! Please enter either 'y' or 'n': ";
  104. cin >> again;
  105. ClearScreen();
  106. }while(again != 'y' && again != 'n');
  107. }
  108. }
  109.  
  110. //==================================================================================================================
  111.  
  112. void DealAmount(int &number)
  113. {
  114. /* Asks the user how many cards they would like dealt
  115. The number of cards the user enters is returned as number*/
  116.  
  117. cout << "How many cards do you want dealt? ";
  118. cin >> number;
  119. ClearScreen();
  120. }
  121.  
  122. //==================================================================================================================
  123.  
  124. void CardSum(int val1, int val2, int val3, int val4, int &total)
  125. {
  126. /*Takes the values of three cards as val1, val2, and val3, changes the value to 10 if a value is greater than 10, sums them, and outputs their totals
  127. val1 >= 0 && val1 <= 14, val2 >= 0 && val2 <= 14, val3 >= 0 && val3 <= 14, val4 >= 0 && val4 <= 14
  128. Returns the sum of the values as total*/
  129.  
  130. if (val1 == 12 || val1 == 13 || val1 == 14)
  131. {
  132. val1 = 10;
  133. }
  134. if (val2 == 12 || val2 == 13 || val2 == 14)
  135. {
  136. val2 = 10;
  137. }
  138. if (val3 == 12 || val3 == 13 || val3 == 14)
  139. {
  140. val3 = 10;
  141. }
  142. if (val4 == 12 || val4 == 13 || val4 == 14)
  143. {
  144. val4 = 10;
  145. }
  146.  
  147. total = 0;
  148. total = val1 + val2 + val3 + val4;
  149. }
  150.  
  151. //==================================================================================================================
  152.  
  153. void dealuser(int &total)
  154. {
  155. /*Deals user desired amount of cards and returns the value of their total sum
  156. Total returned as an integer with the value of the sum of the user's cards*/
  157.  
  158. string name1, name2, name3, name4;
  159. int value1, value2, value3, value4, cardamount;
  160.  
  161. do
  162. {
  163. //Call deal amount
  164. DealAmount(cardamount);
  165. ClearScreen();
  166.  
  167. //Deal number of cards requested and sums them
  168. if(cardamount == 1)
  169. {
  170. DrawCard(value1);
  171. value2 = 0;
  172. value3 = 0;
  173. value4 = 0;
  174.  
  175. NameCard(value1, name1);
  176.  
  177. CardSum(value1, value2, value3, value4, total);
  178.  
  179. cout << "Your first card is a " << name1 << "." << endl;
  180. }
  181. else if(cardamount == 2)
  182. {
  183. DrawCard(value1);
  184. Sleep(1000);
  185. DrawCard(value2);
  186. value3 = 0;
  187. value4 = 0;
  188.  
  189. NameCard(value1, name1);
  190. NameCard(value2, name2);
  191.  
  192. CardSum(value1, value2, value3, value4, total);
  193.  
  194. cout << "Your first card is a " << name1 << "." << endl;
  195. cout << "Your second card is a " << name2 << "." << endl;
  196. }
  197. else if(cardamount == 3)
  198. {
  199. DrawCard(value1);
  200. Sleep(1000);
  201. DrawCard(value2);
  202. Sleep(1000);
  203. DrawCard(value3);
  204. value4 = 0;
  205.  
  206. NameCard(value1, name1);
  207. NameCard(value2, name2);
  208. NameCard(value3, name3);
  209.  
  210. CardSum(value1, value2, value3, value4, total);
  211.  
  212. cout << "Your first card is a " << name1 << "." << endl;
  213. cout << "Your second card is a " << name2 << "." << endl;
  214. cout << "Your third card is a " << name3 << "." << endl;
  215. }
  216. else if(cardamount == 4)
  217. {
  218. DrawCard(value1);
  219. Sleep(1000);
  220. DrawCard(value2);
  221. Sleep(1000);
  222. DrawCard(value3);
  223. Sleep(1000);
  224. DrawCard(value4);
  225.  
  226. NameCard(value1, name1);
  227. NameCard(value2, name2);
  228. NameCard(value3, name3);
  229. NameCard(value4, name4);
  230.  
  231. CardSum(value1, value2, value3, value4, total);
  232.  
  233. cout << "Your first card is a " << name1 << "." << endl;
  234. cout << "Your second card is a " << name2 << "." << endl;
  235. cout << "Your third card is a " << name3 << "." << endl;
  236. cout << "Your fourth card is a " << name4 << "." << endl;
  237. }
  238. else
  239. {
  240. cout << "Error! Please choose a number of cards between 1 and 4." << endl << endl;
  241. }
  242. }while(cardamount < 1 || cardamount > 4);
  243.  
  244. //Inform user of their card total value
  245. SetColor(9);
  246. cout << "Your card total is " << total << "." << endl;
  247. SetColor(7);
  248. }
  249.  
  250. //==================================================================================================================
  251.  
  252. void dealcomputer(int &total)
  253. {
  254. /*Generate three cards for the computer, total their value, and inform user of the cards and total
  255. Generates three cards, calculates their total, and informs the user of the cardes and their value*/
  256.  
  257. int card1, card2, card3, card4;
  258. string name1, name2, name3;
  259.  
  260. card4 = 0;
  261.  
  262. DrawCard(card1);
  263. NameCard(card1, name1);
  264. Sleep(1000);
  265. DrawCard(card2);
  266. NameCard(card2, name2);
  267. Sleep(1000);
  268. DrawCard(card3);
  269. NameCard(card3, name3);
  270.  
  271. CardSum(card1, card2, card3, card4, total);
  272.  
  273. cout << endl << endl << "The computer's first card is a " << name1 << "." << endl;
  274. cout << "The computer's second card is a " << name2 << "." << endl;
  275. cout << "The computer's third card is a " << name3 << "." << endl;
  276. SetColor(14);
  277. cout << "The computer's total is " << total << "." << endl;
  278. SetColor(7);
  279.  
  280. }
  281.  
  282. //==================================================================================================================
  283.  
  284. void FindWinner(int computer_in, int user_in, int &winner)
  285. {
  286. /*Determines winner of the round from computer_in and user_in
  287. computer_in > 0 and user_in > 0
  288. Winner returned as 1 if computer won, 2 if user won, or 0 if it was a draw*/
  289.  
  290. if(computer_in > 21 && user_in > 21)
  291. {
  292. winner = 0;
  293. }
  294. else if(computer_in > user_in && computer_in <= 21)
  295. {
  296. winner = 1;
  297. }
  298. else if(user_in > computer_in && user_in <=21)
  299. {
  300. winner = 2;
  301. }
  302. else if(user_in < computer_in && computer_in > 21)
  303. {
  304. winner = 2;
  305. }
  306. else if(computer_in < user_in && user_in > 21)
  307. {
  308. winner = 1;
  309. }
  310. else if(user_in == computer_in)
  311. {
  312. winner = 0;
  313. }
  314. else
  315. {
  316. winner = 0;
  317. }
  318. }
  319.  
  320. //==================================================================================================================
  321.  
  322. void updatecount(int winner, int &computerwins, int &userwins)
  323. {
  324. /* Updates the count of how many wins each player has
  325. winner == 0 || winner == 1 || winner == 2
  326. Updated computer wins returned as compwins and updated user wins returned as userwins*/
  327.  
  328. if (winner == 1)
  329. {
  330. computerwins += 1;
  331. }
  332.  
  333. else if (winner == 2)
  334. {
  335. userwins += 1;
  336. }
  337. }
  338.  
  339. //==================================================================================================================
  340. void ReportWinner(int winvalue)
  341. {
  342. /*Informs the user who won the round based on winvalue
  343. winvalue >= 0 && winvalue <= 2
  344. The winner of the round is reported*/
  345.  
  346. if(winvalue == 1)
  347. {
  348. SetColor(4);
  349. cout << endl << "Sorry, the computer won this hand." << endl;
  350. SetColor(7);
  351. }
  352. else if (winvalue == 2)
  353. {
  354. SetColor(2);
  355. cout << endl << "Congratulations, you won this hand!" << endl;
  356. SetColor(7);
  357. }
  358. else
  359. {
  360. SetColor(5);
  361. cout << endl << "This hand resulted in a draw." << endl;
  362. SetColor(7);
  363. }
  364.  
  365. }
  366.  
  367. //==================================================================================================================
  368.  
  369. void ReportResult(int compwins, int userwins)
  370. {
  371. cout << "The computer has won " << compwins << " times." << endl << endl;
  372. cout << "The user has won " << userwins << " times." << endl << endl;
  373.  
  374. if(userwins > compwins)
  375. {
  376. cout << "Congratulations, you have won this playing session!" << endl;
  377. }
  378. else if(compwins > userwins )
  379. {
  380. cout << "Sorry, but the computer has won this playing session." << endl;
  381. }
  382. else
  383. {
  384. cout << "You and the computer have tied this playing session." << endl;
  385. }
  386. }
  387.  
  388. //==================================================================================================================
  389.  
  390. int Outro ()
  391. {
  392. int choice;
  393. cout << endl;
  394. cout << "Thank you for playing the game! Have a great day." << endl;
  395.  
  396. return 0;
  397. }
  398.  
  399. //==================================================================================================================
  400.  
  401. int Intro()
  402. {
  403. cout << "Welcome to The Game of 21!" << endl;
  404. cout << "You will be asked to pick your amount of cards in a few seconds. You must pick a value between 1 and 4 cards." << endl;
  405. cout << "Now I am going to explain a few rules to you so you can understand the game a little better." << endl << endl;
  406. Sleep(10000);
  407. cout << "This is a game of cards, maybe you have heard of it maybe you have not." << endl;
  408. cout << "Each card is worth its respective number, and special cards, such as Kings, Queens, or Jacks, are worth 10." << endl;
  409. cout << "Ace's, however, are worth 11. The dealer will start with 3 cards." << endl;
  410. cout << "If both scores are over 21, or if both are equal but less than or equal to 21, the game is declared a draw." << endl;
  411. cout << "Otherwise the winner is the one with the highest score equal to or less than 21." << endl;
  412. cout << "If one score is over 21, but the other is 21 or less, the player with 21 or less is declared the winner." << endl;
  413. cout << "Now that you know the rules, lets play the game!" << endl << endl;
  414. Sleep(10000);
  415.  
  416. return 0;
  417. }
  418.  
  419. //==================================================================================================================
  420.  
  421. void PlayGame(int &computerwins, int &userwins)
  422. /* Calls majority of functions to run the game adn keeps track of how many times computer wins and use wins
  423. Number of times the computer wins returned as computerwins and number of times user wins returned as user wins*/
  424. {
  425. int usertotal, computertotal, winner;
  426.  
  427. dealuser(usertotal);
  428. dealcomputer(computertotal);
  429. FindWinner(computertotal, usertotal, winner);
  430. ReportWinner(winner);
  431. updatecount(winner, computerwins, userwins);
  432. }
  433.  
  434. //==================================================================================================================
  435.  
  436. int main()
  437. {
  438. int computerwins = 0, userwins = 0;
  439. char play;
  440.  
  441. Intro();
  442.  
  443. do
  444. {
  445. //Run the game
  446. PlayGame(computerwins, userwins);
  447.  
  448. //Determine if game is played again
  449. PlayAgain(play);
  450.  
  451. }while(play != 'n');
  452.  
  453. ReportResult(computerwins, userwins);
  454. cout << endl;
  455. Outro();
  456.  
  457. return 0;
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement