Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- // Coin.h
- #ifndef coin_h
- #define coin_h
- #include<stdlib.h>
- #include<time.h>
- #include<iostream>
- using namespace std;
- //Coin class
- class Coin{
- //required variable to store coin's side
- string sideUp;
- public:
- //public method prototypes
- Coin();
- void toss();
- string getSideUp();
- };
- #endif
- //Coin.cpp
- #include "coin.h"
- //implementation of all methods
- Coin::Coin(){
- //tossing the coin
- toss();
- }
- //this method will toss the coin, update sideUp variable
- void Coin::toss(){
- //generating a random number (0 or 1)
- int i=rand()%2;
- //if i is 0, setting sideUp as heads, or else tails
- if(i==0){
- sideUp="heads";
- }else{
- sideUp="tails";
- }
- }
- string Coin::getSideUp(){
- return sideUp;
- }
- //200_assign3.cpp
- #include "coin.h"
- #include<iomanip>
- int main(){
- //seeding random number generator
- srand(time(NULL));
- //initializing variables
- double playerBalance=0,computerBalance=0;
- Coin quarter, dime, nickel; //denoting each type of coins
- int round=1;
- bool gameOver=false; //loop controller
- //setting decimal point precision to 2
- cout<<setprecision(2)<<fixed;
- //displaying starting balance
- cout<<"Your starting balance : $"<<playerBalance<<endl;
- cout<<"The Computer's starting balance : $"<<computerBalance<<endl;
- //looping until game is over
- while(!gameOver){
- //tossing three coins
- quarter.toss();
- dime.toss();
- nickel.toss();
- //updating player's balance if a coin draw heads
- if(quarter.getSideUp()=="heads"){
- playerBalance+=0.25;
- }
- if(dime.getSideUp()=="heads"){
- playerBalance+=0.10;
- }
- if(nickel.getSideUp()=="heads"){
- playerBalance+=0.05;
- }
- //tossing three coins again
- quarter.toss();
- dime.toss();
- nickel.toss();
- //updating computer's balance if a coin draw heads
- if(quarter.getSideUp()=="heads"){
- computerBalance+=0.25;
- }
- if(dime.getSideUp()=="heads"){
- computerBalance+=0.10;
- }
- if(nickel.getSideUp()=="heads"){
- computerBalance+=0.05;
- }
- //displaying balance after this round
- cout<<"Your balance after round "<<round<<": $"<<playerBalance<<endl;
- cout<<"The Computer's balance after round "<<round<<": $"<<computerBalance<<endl;
- //checking if any of them has won
- if(playerBalance>=1 || computerBalance>=1){
- //game is over
- gameOver=true;
- }
- round++;
- }
- //displaying ending balance
- cout<<"Your ending balance : $"<<playerBalance<<endl;
- cout<<"The Computer's ending balance : $"<<computerBalance<<endl;
- //identifying the winner
- if(playerBalance==computerBalance){
- //tie
- cout<<"Tie! Nobody wins"<<endl;
- }else if(playerBalance>computerBalance){
- //player
- cout<<"Congratulations! You won"<<endl;
- }else{
- //computer
- cout<<"Sorry! The computer won"<<endl;
- }
- }
- //OUTPUT (multiple runs)
- Your starting balance : $0.00
- The Computer's starting balance : $0.00
- Your balance after round 1: $0.30
- The Computer's balance after round 1: $0.00
- Your balance after round 2: $0.35
- The Computer's balance after round 2: $0.35
- Your balance after round 3: $0.75
- The Computer's balance after round 3: $0.45
- Your balance after round 4: $0.80
- The Computer's balance after round 4: $0.80
- Your balance after round 5: $1.20
- The Computer's balance after round 5: $1.20
- Your ending balance : $1.20
- The Computer's ending balance : $1.20
- Congratulations! You won
- Your starting balance : $0.00
- The Computer's starting balance : $0.00
- Your balance after round 1: $0.00
- The Computer's balance after round 1: $0.10
- Your balance after round 2: $0.15
- The Computer's balance after round 2: $0.10
- Your balance after round 3: $0.25
- The Computer's balance after round 3: $0.40
- Your balance after round 4: $0.55
- The Computer's balance after round 4: $0.80
- Your balance after round 5: $0.90
- The Computer's balance after round 5: $1.20
- Your ending balance : $0.90
- The Computer's ending balance : $1.20
- Sorry! The computer won
- Your starting balance : $0.00
- The Computer's starting balance : $0.00
- Your balance after round 1: $0.40
- The Computer's balance after round 1: $0.40
- Your balance after round 2: $0.55
- The Computer's balance after round 2: $0.40
- Your balance after round 3: $0.70
- The Computer's balance after round 3: $0.70
- Your balance after round 4: $0.85
- The Computer's balance after round 4: $0.95
- Your balance after round 5: $1.00
- The Computer's balance after round 5: $1.00
- Your ending balance : $1.00
- The Computer's ending balance : $1.00
- Tie! Nobody wins
Advertisement
Add Comment
Please, Sign In to add comment