Advertisement
vulpy

oh frack

Jul 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.68 KB | None | 0 0
  1. // Program: Assignment 3 OH, Frack!
  2. // Programmer: Peter Chen
  3. // Purpose: a dice game called "Frack"
  4.  
  5. #include "pch.h"
  6. #include <iostream>
  7. #include <ctime>
  8. #include <iomanip>
  9. #include <string>
  10. #include <cstdlib>
  11. using namespace std;
  12.  
  13. //--------------------------------------------------------------------------------- Program introduction
  14.  
  15. int gameIntro() { // Get the players' names
  16.  
  17. int choice;
  18. cout << "Welcome to the game OH FRACK!!!\n";
  19. cout << "Please choose what you would like to do?\n";
  20. cout << "1. Play\n";
  21. cout << "2. Rules\n";
  22. cout << "3. Exit\n";
  23. cout << "Please choose:";
  24. cin >> choice;
  25. while (choice != 1 && choice != 2 && choice != 3) {
  26. cout << "Please re-enter a valid choice:";
  27. cin >> choice;
  28. }
  29.  
  30. return choice;
  31. }
  32.  
  33. //--------------------------------------------------------------------------------- Game rules
  34. void help() {
  35. cout << "\n";
  36. cout << "1. first player rolls 5 six-sided dice\n";
  37. cout << "2. if the average of the dice is less than or equal to 3\n";
  38. cout << "the player gets 0 points from this turn and next player plays.";
  39. cout << "3. if it is greater than 3, then the player is safe and the total of the dice is their score\n";
  40. cout << "4. then they can choose either to \n";
  41. cout << "\n";
  42. cout << "a. roll again, but with one less die, their score increase if they dont frack out\n";
  43. cout << "meaning they dont get an average of less than 3\n";
  44. cout << "the player can continue do this all the way to rolling just one die\n";
  45. cout << "after they die, it is passed to the next player.\n";
  46. cout << "b. they can declare their turn over, add their total score to final score\n";
  47. cout << "the next player does the same\n";
  48. cout << "Note that the total game score is not affected by fracking out(dying once)\n";
  49. cout << "only the turn score can be reset to zero!\n";
  50. cout << "\n";
  51.  
  52. return;
  53. }
  54.  
  55. //--------------------------------------------------------------------------------- Game calculation
  56.  
  57. int getPoints() {
  58.  
  59. int points;
  60. cout << "How many points are we playing to?";
  61. cin >> points;
  62.  
  63. return points;
  64. }
  65.  
  66. int playerCalc(double roll1, int roll2, int roll3, int roll4, int roll5) {
  67. double averageR;
  68. averageR = (roll1 + roll2 + roll3 + roll3 + roll4 + roll5) / 100;
  69.  
  70. return averageR;
  71. }
  72.  
  73. //--------------------------------------------------------------------------------- Main Program
  74.  
  75. int main()
  76. {
  77. string player1; // Player 1's name
  78. string player2; // Player 2's name
  79. string p1Roll; // Player 1's roll choice
  80. string p2Roll; // Player 2's roll choice
  81. string p2Choice; // Player 2's Choice between ZERO start or player1's dice
  82.  
  83. //-------------------------------------------------------- The 5 dices players roll
  84. int dice1;
  85. int dice2;
  86. int dice3;
  87. int dice4;
  88. int dice5;
  89.  
  90. //-------------------------------------------------------- Players' choice and points they are playing to
  91. int choice;
  92. int points;
  93.  
  94. //--------------------------------------------------------- Dice count for the 2 players
  95. int diceP1 = 5;
  96. int diceP2 = 5;
  97.  
  98. //--------------------------------------------------------- Player totals and averages
  99. double p1Total;
  100. double p1AverageR;
  101. double p2Total;
  102. double p2AverageR;
  103.  
  104. //--------------------------------------------------------- Player's end score
  105. int score2 = 0;
  106. int score = 0;
  107.  
  108. //--------------------------------------------------------- Random number generator for the dices
  109. srand((unsigned)time(0));
  110. dice1 = (rand() % 5) + 1;
  111. dice2 = (rand() % 5) + 1;
  112. dice3 = (rand() % 5) + 1;
  113. dice4 = (rand() % 5) + 1;
  114. dice5 = (rand() % 5) + 1;
  115.  
  116.  
  117. choice = gameIntro();
  118. srand((unsigned)time (0));
  119.  
  120.  
  121. if (choice == 1) {
  122. cout << "Player 1 please enter your name:";
  123. cin >> player1;
  124. cout << "Player 2 please enter your name:";
  125. cin >> player2;
  126. cout << "\n";
  127.  
  128. points = getPoints();
  129.  
  130. //---------------------------------------------------------- Game starts here
  131.  
  132. while (points > score || points > score2) {
  133. cout << player1 << "'s turn 1\n";
  134. cout << "Your dices are: " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << " \n";
  135. cout << "\n";
  136.  
  137. p1Total = dice1 + dice2 + dice3 + dice4 + dice5;
  138. p1AverageR = p1Total / diceP1;
  139.  
  140. cout << "The total is " << p1Total << ", and the average is " << p1AverageR << "\n";
  141.  
  142. cout << "\n";
  143.  
  144. score = p1Total;
  145.  
  146. //----------------------------------------------------------- Loop for the players
  147. if (p1AverageR >= 2.6) {
  148. while (p1AverageR >= 2.6 && diceP1 > 0) {
  149. srand((unsigned)time(0));
  150. dice1 = (rand() % 5) + 1;
  151. dice2 = (rand() % 5) + 1;
  152. dice3 = (rand() % 5) + 1;
  153. dice4 = (rand() % 5) + 1;
  154. dice5 = (rand() % 5) + 1;
  155.  
  156.  
  157. cout << player1 << " is safe! Your turn score is now " << p1Total << "\n";
  158. cout << "Enter R to roll again, or P to pass:";
  159. cin >> p1Roll;
  160.  
  161. if (p1Roll == "R") {
  162.  
  163. diceP1--;
  164. if (diceP1 == 4) {
  165. dice5 = 0;
  166. }
  167. else if (diceP1 == 3) {
  168. dice5 = 0;
  169. dice4 = 0;
  170. }
  171. else if (diceP1 == 2) {
  172. dice5 = 0;
  173. dice4 = 0;
  174. dice3 = 0;
  175. }
  176. else if (diceP1 == 1) {
  177. dice5 = 0;
  178. dice4 = 0;
  179. dice3 = 0;
  180. dice2 = 0;
  181. }
  182. else if (diceP1 == 0) {
  183. break;
  184. }
  185.  
  186. cout << "Your dices are : " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << "\n";
  187.  
  188. p1Total = dice1 + dice2 + dice3 + dice4 + dice5;
  189. p1AverageR = p1Total / diceP1;
  190.  
  191.  
  192. cout << "\nThe total is " << p1Total << ", and the average is " << p1AverageR << "\n";
  193. if (p1AverageR >= 2.6) {
  194. score += p1Total;
  195. }
  196. else {
  197. p1Total = 0;
  198. score += p1Total;
  199. }
  200. cout << score << "\n";
  201. }// End of first if in while
  202.  
  203. else if (p1Roll == "P") {
  204.  
  205. diceP1 = 0;
  206. break;
  207. }// End of else if
  208. }// End while the user is higher than 2.6
  209. }// End of if loop
  210. else if (p1AverageR < 2.6) {
  211. score = 0;
  212. cout << "You got a score of 0\n";
  213. }
  214.  
  215. cout << player1 << "'s total score for the first round is " << score << ".\n";
  216.  
  217.  
  218. //----------------------------------------------------------------------------PLAYER 2 SECTION
  219.  
  220. cout << "What would " << player2 << " like to do?\n";
  221. cout << "Do you want to start from zero(Z), or with " << player1 << "'s points(P) with " << diceP1 << "?";
  222.  
  223. cin >> p2Choice;
  224.  
  225. cout << "\n";
  226.  
  227. if (p2Choice == "Z") {
  228. diceP2 = 5;
  229.  
  230. dice1 = (rand() % 5) + 1;
  231. dice2 = (rand() % 5) + 1;
  232. dice3 = (rand() % 5) + 1;
  233. dice4 = (rand() % 5) + 1;
  234. dice5 = (rand() % 5) + 1;
  235.  
  236. cout << "Your dices are: " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << " \n";
  237.  
  238. p2Total = dice1 + dice2 + dice3 + dice4 + dice5;
  239. p2AverageR = p2Total / diceP2;
  240.  
  241. cout << "The total is " << p2Total << ", and the average is " << p2AverageR << "\n";
  242.  
  243. score2 = p2Total;
  244.  
  245. if (p2AverageR >= 2.6) {
  246. while (p2AverageR >= 2.6 && diceP2 > 0) {
  247. srand((unsigned)time(0));
  248. dice1 = (rand() % 5) + 1;
  249. dice2 = (rand() % 5) + 1;
  250. dice3 = (rand() % 5) + 1;
  251. dice4 = (rand() % 5) + 1;
  252. dice5 = (rand() % 5) + 1;
  253.  
  254.  
  255. cout << player2 << " is safe! Your turn score is now " << p2Total << "\n";
  256. cout << "Enter R to roll again, or P to pass:";
  257. cin >> p2Roll;
  258.  
  259. if (p2Roll == "R") {
  260.  
  261. diceP2--;
  262. if (diceP2 == 4) {
  263. dice5 = 0;
  264. }
  265. else if (diceP2 == 3) {
  266. dice5 = 0;
  267. dice4 = 0;
  268. }
  269. else if (diceP2 == 2) {
  270. dice5 = 0;
  271. dice4 = 0;
  272. dice3 = 0;
  273. }
  274. else if (diceP2 == 1) {
  275. dice5 = 0;
  276. dice4 = 0;
  277. dice3 = 0;
  278. dice2 = 0;
  279. }
  280. else if (diceP2 == 0) {
  281. break;
  282. }
  283.  
  284. cout << "Your dices are : " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << "\n";
  285.  
  286. p2Total = dice1 + dice2 + dice3 + dice4 + dice5;
  287. p2AverageR = p2Total / diceP2;
  288.  
  289.  
  290. cout << "\nThe total is " << p2Total << ", and the average is " << p2AverageR << "\n";
  291. if (p2AverageR >= 2.6) {
  292. score2 += p2Total;
  293. }
  294. else {
  295. p2Total = 0;
  296. score2 += p2Total;
  297. }
  298. cout << score << "\n";
  299. }// End of first if in while
  300.  
  301. else if (p2Roll == "P") {
  302.  
  303. diceP1 = 0;
  304. break;
  305. }// End of else if
  306. }// End while the user is higher than 2.6
  307. }// End of if loop
  308. if (p2AverageR < 2.6) {
  309. score2 = 0;
  310. cout << "You got a score of 0\n";
  311. }
  312.  
  313. cout << player2 << "'s total score for the first round is " << score2 << ".\n";
  314.  
  315.  
  316. }
  317.  
  318. //---------------------------------------------------------------------------- Player 2 chooses PASS
  319. else if (p2Choice == "P") {
  320. diceP2 = diceP1;
  321.  
  322.  
  323.  
  324. if (diceP2 == 4) {
  325. dice5 = 0;
  326. }
  327. else if (diceP2 == 3) {
  328. dice5 = 0;
  329. dice4 = 0;
  330. }
  331. else if (diceP2 == 2) {
  332. dice5 = 0;
  333. dice4 = 0;
  334. dice3 = 0;
  335. }
  336. else if (diceP2 == 1) {
  337. dice5 = 0;
  338. dice4 = 0;
  339. dice3 = 0;
  340. dice2 = 0;
  341. }
  342.  
  343. cout << "Your dices are: " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << " \n";
  344.  
  345. p2Total = dice1 + dice2 + dice3 + dice4 + dice5;
  346. p2AverageR = p2Total / diceP2;
  347.  
  348. cout << "The total is " << p2Total << ", and the average is " << p2AverageR << "\n";
  349.  
  350. score2 = p2Total;
  351.  
  352. if (p2AverageR >= 2.6) {
  353. while (p2AverageR >= 2.6 && diceP2 > 0) {
  354. srand((unsigned)time(0));
  355. dice1 = (rand() % 5) + 1;
  356. dice2 = (rand() % 5) + 1;
  357. dice3 = (rand() % 5) + 1;
  358. dice4 = (rand() % 5) + 1;
  359. dice5 = (rand() % 5) + 1;
  360.  
  361.  
  362. cout << player2 << " is safe! Your turn score is now " << p2Total << "\n";
  363. cout << "Enter R to roll again, or P to pass:";
  364. cin >> p2Roll;
  365.  
  366. if (p2Roll == "R") {
  367.  
  368. diceP2--;
  369. if (diceP2 == 4) {
  370. dice5 = 0;
  371. }
  372. else if (diceP2 == 3) {
  373. dice5 = 0;
  374. dice4 = 0;
  375. }
  376. else if (diceP2 == 2) {
  377. dice5 = 0;
  378. dice4 = 0;
  379. dice3 = 0;
  380. }
  381. else if (diceP2 == 1) {
  382. dice5 = 0;
  383. dice4 = 0;
  384. dice3 = 0;
  385. dice2 = 0;
  386. }
  387. else if (diceP2 == 0) {
  388. break;
  389. }
  390.  
  391. cout << "Your dices are : " << dice1 << " " << dice2 << " " << dice3 << " " << dice4 << " " << dice5 << "\n";
  392.  
  393. p2Total = dice1 + dice2 + dice3 + dice4 + dice5;
  394. p2AverageR = p2Total / diceP2;
  395.  
  396.  
  397. cout << "\nThe total is " << p2Total << ", and the average is " << p2AverageR << "\n";
  398. if (p2AverageR >= 2.6) {
  399. score2 += p2Total;
  400. }
  401. else {
  402. p2Total = 0;
  403. score2 += p2Total;
  404. }
  405. cout << score << "\n";
  406. }// End of first if in while
  407.  
  408. else if (p2Roll == "P") {
  409.  
  410. diceP2 = 0;
  411. break;
  412. }// End of else if
  413. }// End while the user is higher than 2.6
  414. }// End of if loop
  415. if (p2AverageR < 2.6) {
  416. score2 = 0;
  417. cout << "You got a score of 0\n";
  418. }
  419.  
  420. cout << player2 << "'s total score for the first round is " << score2 << ".\n";
  421.  
  422. }
  423. } // End of while loop for points
  424. } // First if
  425.  
  426. else if (choice == 2){ // Rules of the game
  427. help();
  428. }
  429. else if (choice == 3) { // Exit the game
  430. exit(0);
  431. }
  432.  
  433.  
  434. cout << "\nBy: Peter Chen Section: S11\n";
  435.  
  436. system("pause");
  437.  
  438. return 0;
  439. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement