jaredec18

Untitled

Sep 22nd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
  2.  
  3. // Coin.h
  4.  
  5. #ifndef coin_h
  6.  
  7. #define coin_h
  8.  
  9. #include<stdlib.h>
  10.  
  11. #include<time.h>
  12.  
  13. #include<iostream>
  14.  
  15. using namespace std;
  16.  
  17. //Coin class
  18.  
  19. class Coin{
  20.  
  21. //required variable to store coin's side
  22.  
  23. string sideUp;
  24.  
  25. public:
  26.  
  27. //public method prototypes
  28.  
  29. Coin();
  30.  
  31. void toss();
  32.  
  33. string getSideUp();
  34.  
  35. };
  36.  
  37. #endif
  38.  
  39. //Coin.cpp
  40.  
  41. #include "coin.h"
  42.  
  43. //implementation of all methods
  44.  
  45. Coin::Coin(){
  46.  
  47. //tossing the coin
  48.  
  49. toss();
  50.  
  51. }
  52.  
  53. //this method will toss the coin, update sideUp variable
  54.  
  55. void Coin::toss(){
  56.  
  57. //generating a random number (0 or 1)
  58.  
  59. int i=rand()%2;
  60.  
  61. //if i is 0, setting sideUp as heads, or else tails
  62.  
  63. if(i==0){
  64.  
  65. sideUp="heads";
  66.  
  67. }else{
  68.  
  69. sideUp="tails";
  70.  
  71. }
  72.  
  73. }
  74.  
  75. string Coin::getSideUp(){
  76.  
  77. return sideUp;
  78.  
  79. }
  80.  
  81. //200_assign3.cpp
  82.  
  83. #include "coin.h"
  84.  
  85. #include<iomanip>
  86.  
  87. int main(){
  88.  
  89. //seeding random number generator
  90.  
  91. srand(time(NULL));
  92.  
  93. //initializing variables
  94.  
  95. double playerBalance=0,computerBalance=0;
  96.  
  97. Coin quarter, dime, nickel; //denoting each type of coins
  98.  
  99. int round=1;
  100.  
  101. bool gameOver=false; //loop controller
  102.  
  103. //setting decimal point precision to 2
  104.  
  105. cout<<setprecision(2)<<fixed;
  106.  
  107. //displaying starting balance
  108.  
  109. cout<<"Your starting balance : $"<<playerBalance<<endl;
  110.  
  111. cout<<"The Computer's starting balance : $"<<computerBalance<<endl;
  112.  
  113. //looping until game is over
  114.  
  115. while(!gameOver){
  116.  
  117. //tossing three coins
  118.  
  119. quarter.toss();
  120.  
  121. dime.toss();
  122.  
  123. nickel.toss();
  124.  
  125. //updating player's balance if a coin draw heads
  126.  
  127. if(quarter.getSideUp()=="heads"){
  128.  
  129. playerBalance+=0.25;
  130.  
  131. }
  132.  
  133. if(dime.getSideUp()=="heads"){
  134.  
  135. playerBalance+=0.10;
  136.  
  137. }
  138.  
  139. if(nickel.getSideUp()=="heads"){
  140.  
  141. playerBalance+=0.05;
  142.  
  143. }
  144.  
  145. //tossing three coins again
  146.  
  147. quarter.toss();
  148.  
  149. dime.toss();
  150.  
  151. nickel.toss();
  152.  
  153. //updating computer's balance if a coin draw heads
  154.  
  155. if(quarter.getSideUp()=="heads"){
  156.  
  157. computerBalance+=0.25;
  158.  
  159. }
  160.  
  161. if(dime.getSideUp()=="heads"){
  162.  
  163. computerBalance+=0.10;
  164.  
  165. }
  166.  
  167. if(nickel.getSideUp()=="heads"){
  168.  
  169. computerBalance+=0.05;
  170.  
  171. }
  172.  
  173. //displaying balance after this round
  174.  
  175. cout<<"Your balance after round "<<round<<": $"<<playerBalance<<endl;
  176.  
  177. cout<<"The Computer's balance after round "<<round<<": $"<<computerBalance<<endl;
  178.  
  179. //checking if any of them has won
  180.  
  181. if(playerBalance>=1 || computerBalance>=1){
  182.  
  183. //game is over
  184.  
  185. gameOver=true;
  186.  
  187. }
  188.  
  189. round++;
  190.  
  191. }
  192.  
  193. //displaying ending balance
  194.  
  195. cout<<"Your ending balance : $"<<playerBalance<<endl;
  196.  
  197. cout<<"The Computer's ending balance : $"<<computerBalance<<endl;
  198.  
  199. //identifying the winner
  200.  
  201. if(playerBalance==computerBalance){
  202.  
  203. //tie
  204.  
  205. cout<<"Tie! Nobody wins"<<endl;
  206.  
  207. }else if(playerBalance>computerBalance){
  208.  
  209. //player
  210.  
  211. cout<<"Congratulations! You won"<<endl;
  212.  
  213. }else{
  214.  
  215. //computer
  216.  
  217. cout<<"Sorry! The computer won"<<endl;
  218.  
  219. }
  220.  
  221.  
  222.  
  223. }
  224.  
  225. //OUTPUT (multiple runs)
  226.  
  227. Your starting balance : $0.00
  228.  
  229. The Computer's starting balance : $0.00
  230.  
  231. Your balance after round 1: $0.30
  232.  
  233. The Computer's balance after round 1: $0.00
  234.  
  235. Your balance after round 2: $0.35
  236.  
  237. The Computer's balance after round 2: $0.35
  238.  
  239. Your balance after round 3: $0.75
  240.  
  241. The Computer's balance after round 3: $0.45
  242.  
  243. Your balance after round 4: $0.80
  244.  
  245. The Computer's balance after round 4: $0.80
  246.  
  247. Your balance after round 5: $1.20
  248.  
  249. The Computer's balance after round 5: $1.20
  250.  
  251. Your ending balance : $1.20
  252.  
  253. The Computer's ending balance : $1.20
  254.  
  255. Congratulations! You won
  256.  
  257. Your starting balance : $0.00
  258.  
  259. The Computer's starting balance : $0.00
  260.  
  261. Your balance after round 1: $0.00
  262.  
  263. The Computer's balance after round 1: $0.10
  264.  
  265. Your balance after round 2: $0.15
  266.  
  267. The Computer's balance after round 2: $0.10
  268.  
  269. Your balance after round 3: $0.25
  270.  
  271. The Computer's balance after round 3: $0.40
  272.  
  273. Your balance after round 4: $0.55
  274.  
  275. The Computer's balance after round 4: $0.80
  276.  
  277. Your balance after round 5: $0.90
  278.  
  279. The Computer's balance after round 5: $1.20
  280.  
  281. Your ending balance : $0.90
  282.  
  283. The Computer's ending balance : $1.20
  284.  
  285. Sorry! The computer won
  286.  
  287. Your starting balance : $0.00
  288.  
  289. The Computer's starting balance : $0.00
  290.  
  291. Your balance after round 1: $0.40
  292.  
  293. The Computer's balance after round 1: $0.40
  294.  
  295. Your balance after round 2: $0.55
  296.  
  297. The Computer's balance after round 2: $0.40
  298.  
  299. Your balance after round 3: $0.70
  300.  
  301. The Computer's balance after round 3: $0.70
  302.  
  303. Your balance after round 4: $0.85
  304.  
  305. The Computer's balance after round 4: $0.95
  306.  
  307. Your balance after round 5: $1.00
  308.  
  309. The Computer's balance after round 5: $1.00
  310.  
  311. Your ending balance : $1.00
  312.  
  313. The Computer's ending balance : $1.00
  314.  
  315. Tie! Nobody wins
Advertisement
Add Comment
Please, Sign In to add comment