Advertisement
Guest User

Fixed code - Rootix

a guest
Jul 13th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <string>
  6. using namespace std;
  7.  
  8. int displaystats(int gamesplayed, int wins, int losses, int bank);
  9.  
  10. int main()
  11. {
  12.     int bank = 100;//intital bank value
  13.     int bet = 0;//desired wager
  14.     int wins = 0;//games won
  15.     int losses = 0;//games lost
  16.     int gamesplayed = 0;//how many rounds you played
  17.     int compdice1 = 0;//first rolled dice for computer
  18.     int compdice2 = 0;//second rolled dice for computer
  19.     int playdice1 = 0;//first rolled dice for player
  20.     int playdice2 = 0;//seconds rolled dice for player
  21.     int newdice = 0;//the dice to risk your wager
  22.     int comproll = 0;//the sum of the computers roll
  23.     int playroll = 0;//the sum of the players roll
  24.  
  25.     /**
  26.         We are going to be dealing with rand(). This function does not automatically seed itself.
  27.         We can seed it using the srand() function. (This is a little more elegant than your current
  28.         form of adding a slight amount of entropy to it.
  29.  
  30.     **/
  31.     srand(time(0));
  32.  
  33.     do
  34.     {
  35.         if (bank < 0)
  36.         {
  37.             cout << "You have " << bank << " coins in your bank." << endl;
  38.             cout << "I am sorry you are out of money." << endl;
  39.             displaystats(gamesplayed, wins, losses, bank);
  40.             break;
  41.         }
  42.  
  43.         else if (bank > 0)
  44.         {
  45.             cout << "You have " << bank << " coins in your bank." << endl;
  46.             cout << "How many coins would you like to bet? ";
  47.             cin >> bet;
  48.  
  49.  
  50.             ///The following have been modified... See above.
  51.             compdice1 = rand() % 6 + 1;//computer dice
  52.             compdice2 = rand() % 6 + 1;//computer second dice
  53.             playdice1 = rand() % 6 + 1;//player dice
  54.             playdice2 = rand() % 6 + 1;//player second dice
  55.  
  56.             comproll = compdice1 + compdice2;//computer sum
  57.             playroll = playdice1 + playdice2;//player sume
  58.  
  59.             cout << "Your roll was " << playdice1 << " and " << playdice2 << " with a sum of " << playroll << endl;
  60.  
  61.             if (playroll < comproll)
  62.             {
  63.                 ///As you want to assess option as either "yes" or "no", I have changed its type to string.
  64.                 string option;//option to roll another dice
  65.  
  66.  
  67.                 cout << "You win!" << endl;
  68.                 cout << "Would you like to roll a third dice to earn 1.5 times your bet, yes or no? ";
  69.                 cin >> option;
  70.  
  71.                 if (option == "yes")///Changed this from const char to const char*
  72.                 {
  73.                     int newroll;//the new sum of the three dice
  74.                     int newdice;//the extra roll
  75.  
  76.                     newdice = (rand() + time(0)) % 6 + 1;
  77.                     newroll = playroll + newdice;//the value of players roll
  78.  
  79.                     if (newroll > comproll)
  80.                     {
  81.                         cout << "The computer rolled " << comproll << endl;
  82.                         cout << "You now rolled higher than the computer therefore, I am sorry you lose this round." << endl;
  83.                         cout << "Your bank now equals " << bank - bet << endl;
  84.                         losses++;
  85.                         gamesplayed++;
  86.                         bank = bank - bet; ///We have to actually change the bank's value now don't we?
  87.                     }
  88.  
  89.                     else if (newroll < comproll)
  90.                     {
  91.                         cout << "You win!" << endl;
  92.                         cout << "Your bank now equals " << bank + (1.5 * bet) << endl;
  93.                         wins++;
  94.                         gamesplayed++;
  95.                         bank += 1.5 * bet;///We have to actually change the bank's value now don't we?
  96.                     }
  97.                 }
  98.                 else ///No need to assess if it is equal to "no", we will use this path as the default.
  99.                 {
  100.                     cout << "Your bank now equals " << bank + bet << endl;
  101.                     wins++;
  102.                     gamesplayed++;
  103.                     bank += bet;///We have to actually change the bank's value now don't we?
  104.                 }
  105.             }
  106.  
  107.             else if (playroll > comproll)
  108.             {
  109.                 cout << "The computer rolled " << comproll << endl;
  110.                 cout << "You rolled higher than the computer therefore, I am sorry you lose this round." << endl;
  111.                 cout << "Your bank now equals " << bank - bet << endl;
  112.                 losses++;
  113.                 gamesplayed++;
  114.                 bank = bank - bet;///We have to actually change the bank's value now don't we?
  115.             }
  116.  
  117.             else if (playroll == comproll) /// Remember to use the == operator. This assess equality, = is the assignment operator.
  118.             {
  119.                 cout << "The computer also rolled " << comproll << endl;
  120.                 cout << "I am sorry you now lose double your bet!" << endl;
  121.                 cout << "Your bank now equals " << bank - (2 * bet) << endl;
  122.                 losses++;
  123.                 gamesplayed++;
  124.                 bank = bank - 2 * bet;///We have to actually change the bank's value now don't we?
  125.             }
  126.         }
  127.     } while (bank > 0);
  128.  
  129.     int stats = displaystats(gamesplayed, wins, losses, bank);
  130.  
  131.     cout << "Your stats are " << stats << endl;
  132.     return 0;
  133. }
  134.  
  135. int displaystats(int gamesplayed, int wins, int losses, int bank)
  136. {
  137.     cout << "Games Played: " << gamesplayed << endl;
  138.     cout << "Wins: " << wins << endl;
  139.     cout << "Losses: " << losses << endl;
  140.     cout << "Bank Total: " << bank << endl;
  141.  
  142.     return (gamesplayed, wins, losses, bank);
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement