Advertisement
laith-0093

Code Game C++

Jan 14th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. enum objectType { ROCK, PAPER, SCISSORS };
  5. //Function prototypes
  6. void displayRules();
  7. objectType retrievePlay(char selection);
  8. bool validSelection(char selection);
  9. void convertEnum(objectType object);
  10. objectType winningObject(objectType play1, objectType play2);
  11. void gameResult(objectType play1, objectType play2, int& winner);
  12. void displayResults(int gCount, int wCount1, int wCount2);
  13. int main(int argc, _TCHAR* argv[])
  14. {
  15. //Step 1
  16. int gameCount; //variable to store the number of
  17.  
  18. //games played
  19.  
  20. int winCount1; //variable to store the number of games
  21.  
  22. //won by player 1
  23.  
  24. int winCount2; //variable to store the number of games
  25.  
  26. //won by player 2
  27. int gamewinner;
  28.  
  29. char response; //variable to get the user's response to
  30. //play the game
  31. char selection1;
  32. char selection2;
  33. objectType play1; //player1's selection
  34. objectType play2; //player2's selection
  35. //Initialize variables; Step 2
  36. gameCount = 0;
  37.  
  38. winCount1 = 0;
  39.  
  40. winCount2 = 0;
  41.  
  42. displayRules(); //Step 3
  43.  
  44. cout << "Enter Y/y to play the game: "; //Step 4
  45.  
  46. cin >> response; //Step 5
  47.  
  48. cout << endl;
  49.  
  50. while (response == 'Y' || response == 'y') //Step 6
  51.  
  52. {
  53.  
  54. cout << "Player 1 enter your choice: "; //Step 6a
  55.  
  56. cin >> selection1; //Step 6b
  57.  
  58. cout << endl;
  59.  
  60. cout << "Player 2 enter your choice: "; //Step 6c
  61.  
  62. cin >> selection2; //Step 6d
  63.  
  64. cout << endl;
  65.  
  66. //Step 6e
  67.  
  68. if (validSelection(selection1)
  69.  
  70. && validSelection(selection2))
  71.  
  72. {
  73.  
  74. play1 = retrievePlay(selection1);
  75.  
  76. play2 = retrievePlay(selection2);
  77.  
  78. gameCount++; //Step 6e.i
  79.  
  80. gameResult(play1, play2, gamewinner); //Step 6e.ii
  81.  
  82. if (gamewinner == 1) //Step 6e.iii
  83.  
  84. winCount1++;
  85.  
  86. else if (gamewinner == 2)
  87.  
  88. winCount2++;
  89.  
  90. }//end if
  91.  
  92. cout << "Enter Y/y to play the game: "; //Step 6f
  93.  
  94. cin >> response; //Step 6g
  95.  
  96. cout << endl;
  97.  
  98. }//end while
  99.  
  100. displayResults(gameCount, winCount1,
  101.  
  102. winCount2); //Step 7
  103.  
  104. return 0;
  105.  
  106. }//end main
  107.  
  108. void displayRules()
  109.  
  110. {
  111.  
  112. cout << " Welcome to the game of Rock, Paper, "
  113.  
  114. << "and Scissors." << endl;
  115.  
  116. cout << " This is a game for two players. For each "
  117.  
  118. << "game, each" << endl;
  119.  
  120. cout << " player selects one of the objects Rock, "
  121.  
  122. << "Paper, or Scissors." << endl;
  123.  
  124. cout << " The rules for winning the game are: " << endl;
  125.  
  126. cout << "1. If both players select the same object, it "
  127.  
  128. << "is a tie." << endl;
  129.  
  130. cout << "2. Rock breaks Scissors: So player who selects "
  131.  
  132. << "Rock wins." << endl;
  133.  
  134. cout << "3. Paper covers Rock: So player who selects "
  135.  
  136. << "Paper wins." << endl;
  137.  
  138. cout << "4. Scissors cuts Paper: So player who selects "
  139.  
  140. << "Scissors wins." << endl << endl;
  141.  
  142. cout << "Enter R or r to select Rock, P or p to select "
  143.  
  144. << "Paper, and S or s to select Scissors." << endl;
  145.  
  146. }
  147.  
  148. bool validSelection(char selection)
  149.  
  150. {
  151.  
  152. switch (selection)
  153.  
  154. {
  155.  
  156. case 'R':
  157.  
  158. case 'r':
  159.  
  160. case 'P':
  161.  
  162. case 'p':
  163.  
  164. case 'S':
  165.  
  166. case 's':
  167.  
  168. return true;
  169.  
  170. default:
  171.  
  172. return false;
  173.  
  174. }
  175.  
  176. }
  177.  
  178. objectType retrievePlay(char selection)
  179.  
  180. {
  181.  
  182. objectType object;
  183.  
  184. switch (selection)
  185.  
  186. {
  187.  
  188. case 'R':
  189.  
  190. case 'r':
  191.  
  192. object = ROCK;
  193.  
  194. break;
  195.  
  196. case 'P':
  197.  
  198. case 'p':
  199.  
  200. object = PAPER;
  201.  
  202. break;
  203.  
  204. case 'S':
  205.  
  206. case 's':
  207.  
  208. object = SCISSORS;
  209.  
  210. }
  211.  
  212. return object;
  213.  
  214. }
  215. void gameResult(objectType play1, objectType play2,
  216.  
  217. int& winner)
  218.  
  219. {
  220.  
  221. objectType winnerObject;
  222.  
  223. if (play1 == play2)
  224.  
  225. {
  226.  
  227. winner = 0;
  228.  
  229. cout << "Both players selected ";
  230.  
  231. convertEnum(play1);
  232.  
  233. cout << ". This game is a tie." << endl;
  234.  
  235. }
  236.  
  237. else
  238.  
  239. {
  240.  
  241. winnerObject = winningObject(play1, play2);
  242.  
  243. //Output each player's choice
  244.  
  245. cout << "Player 1 selected ";
  246.  
  247. convertEnum(play1);
  248.  
  249. cout << " and player 2 selected ";
  250.  
  251. convertEnum(play2);
  252.  
  253. cout << ". ";
  254.  
  255. //Decide the winner
  256.  
  257. if (play1 == winnerObject)
  258.  
  259. winner = 1;
  260.  
  261. else if (play2 == winnerObject)
  262.  
  263. winner = 2;
  264.  
  265. //Output the winner
  266.  
  267. cout << "Player " << winner << " wins this game."
  268.  
  269. << endl;
  270.  
  271. }
  272.  
  273. }
  274.  
  275. void convertEnum(objectType object)
  276.  
  277. {
  278.  
  279. switch (object)
  280.  
  281. {
  282.  
  283. case ROCK:
  284.  
  285. cout << "Rock";
  286.  
  287. break;
  288.  
  289. case PAPER:
  290.  
  291. cout << "Paper";
  292.  
  293. break;
  294.  
  295. case SCISSORS:
  296.  
  297. cout << "Scissors";
  298.  
  299. }
  300.  
  301. }
  302.  
  303. objectType winningObject(objectType play1, objectType play2)
  304.  
  305. {
  306.  
  307. if ((play1 == ROCK && play2 == SCISSORS) || (play2 == ROCK && play1 == SCISSORS))
  308.  
  309. return ROCK;
  310.  
  311. else if ((play1 == ROCK && play2 == PAPER) || (play2 == ROCK && play1 == PAPER))
  312.  
  313. return PAPER;
  314.  
  315. else
  316.  
  317. return SCISSORS;
  318.  
  319. }
  320.  
  321. void displayResults(int gCount, int wCount1, int wCount2)
  322.  
  323. {
  324.  
  325. cout << "The total number of plays: " << gCount
  326.  
  327. << endl;
  328.  
  329. cout << "The number of plays won by player 1: "
  330.  
  331. << wCount1 << endl;
  332.  
  333. cout << "The number of plays won by player 2: "
  334.  
  335. << wCount2 << endl;
  336.  
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement