Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- using namespace std;
- int displaystats(int gamesplayed, int wins, int losses, int bank);
- int main()
- {
- int bank = 100;//intital bank value
- int bet = 0;//desired wager
- int wins = 0;//games won
- int losses = 0;//games lost
- int gamesplayed = 0;//how many rounds you played
- int compdice1 = 0;//first rolled dice for computer
- int compdice2 = 0;//second rolled dice for computer
- int playdice1 = 0;//first rolled dice for player
- int playdice2 = 0;//seconds rolled dice for player
- int newdice = 0;//the dice to risk your wager
- int comproll = 0;//the sum of the computers roll
- int playroll = 0;//the sum of the players roll
- /**
- We are going to be dealing with rand(). This function does not automatically seed itself.
- We can seed it using the srand() function. (This is a little more elegant than your current
- form of adding a slight amount of entropy to it.
- **/
- srand(time(0));
- do
- {
- if (bank < 0)
- {
- cout << "You have " << bank << " coins in your bank." << endl;
- cout << "I am sorry you are out of money." << endl;
- displaystats(gamesplayed, wins, losses, bank);
- break;
- }
- else if (bank > 0)
- {
- cout << "You have " << bank << " coins in your bank." << endl;
- cout << "How many coins would you like to bet? ";
- cin >> bet;
- ///The following have been modified... See above.
- compdice1 = rand() % 6 + 1;//computer dice
- compdice2 = rand() % 6 + 1;//computer second dice
- playdice1 = rand() % 6 + 1;//player dice
- playdice2 = rand() % 6 + 1;//player second dice
- comproll = compdice1 + compdice2;//computer sum
- playroll = playdice1 + playdice2;//player sume
- cout << "Your roll was " << playdice1 << " and " << playdice2 << " with a sum of " << playroll << endl;
- if (playroll < comproll)
- {
- ///As you want to assess option as either "yes" or "no", I have changed its type to string.
- string option;//option to roll another dice
- cout << "You win!" << endl;
- cout << "Would you like to roll a third dice to earn 1.5 times your bet, yes or no? ";
- cin >> option;
- if (option == "yes")///Changed this from const char to const char*
- {
- int newroll;//the new sum of the three dice
- int newdice;//the extra roll
- newdice = (rand() + time(0)) % 6 + 1;
- newroll = playroll + newdice;//the value of players roll
- if (newroll > comproll)
- {
- cout << "The computer rolled " << comproll << endl;
- cout << "You now rolled higher than the computer therefore, I am sorry you lose this round." << endl;
- cout << "Your bank now equals " << bank - bet << endl;
- losses++;
- gamesplayed++;
- bank = bank - bet; ///We have to actually change the bank's value now don't we?
- }
- else if (newroll < comproll)
- {
- cout << "You win!" << endl;
- cout << "Your bank now equals " << bank + (1.5 * bet) << endl;
- wins++;
- gamesplayed++;
- bank += 1.5 * bet;///We have to actually change the bank's value now don't we?
- }
- }
- else ///No need to assess if it is equal to "no", we will use this path as the default.
- {
- cout << "Your bank now equals " << bank + bet << endl;
- wins++;
- gamesplayed++;
- bank += bet;///We have to actually change the bank's value now don't we?
- }
- }
- else if (playroll > comproll)
- {
- cout << "The computer rolled " << comproll << endl;
- cout << "You rolled higher than the computer therefore, I am sorry you lose this round." << endl;
- cout << "Your bank now equals " << bank - bet << endl;
- losses++;
- gamesplayed++;
- bank = bank - bet;///We have to actually change the bank's value now don't we?
- }
- else if (playroll == comproll) /// Remember to use the == operator. This assess equality, = is the assignment operator.
- {
- cout << "The computer also rolled " << comproll << endl;
- cout << "I am sorry you now lose double your bet!" << endl;
- cout << "Your bank now equals " << bank - (2 * bet) << endl;
- losses++;
- gamesplayed++;
- bank = bank - 2 * bet;///We have to actually change the bank's value now don't we?
- }
- }
- } while (bank > 0);
- int stats = displaystats(gamesplayed, wins, losses, bank);
- cout << "Your stats are " << stats << endl;
- return 0;
- }
- int displaystats(int gamesplayed, int wins, int losses, int bank)
- {
- cout << "Games Played: " << gamesplayed << endl;
- cout << "Wins: " << wins << endl;
- cout << "Losses: " << losses << endl;
- cout << "Bank Total: " << bank << endl;
- return (gamesplayed, wins, losses, bank);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement