1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <iostream> //for standard I/O
#include <ctime> //needed to access the computer's clock
#include <cstdlib> //necessary for rand() and RAND_MAX
using namespace std;
void PrintHeading(int, int, int); //prototype for printing initial heading
int GetBet(int); //prototype for getting bet(input) from user
int DrawNum (int); //prototype for function that generates random number
int GetGuess(int, int); //prototype for getting guesses from user
int main()
{
const int START_BALANCE = 1000, //user initially receives $1000
MIN = 1, //minimum number that can be guessed
MAX = 100; //maximum number that can be guessed
int betAmount, //amount that the user wants to bet
numDrawn, //the random number from the computer
guess, //the guess from the user
winnings, //the amount that the user won after the round
money = START_BALANCE, //the user's money balance(initialized with $1000)
guessNumber = 0, //number of the guess(1-6)
gamesWon = 0, //how many games the user has won
totalGames = 0; //total games the user has played
unsigned int seed; //seeds rand()
bool won = false; //initializes won as false
double percentageWon; //calculates the percentage of games won out of total played
char playAgain; //user inputs Y/N about whether they want to play another round
//call function that prints overall heading
PrintHeading(money, MIN, MAX);
/*assign the return of the GetBet function to betAmount
and changes user's money balance to reflect their bet*/
betAmount = GetBet(money);
money = money - betAmount;
/*obtain value for the time from the computer's clock and with
it call srand to initialize "seed" for the rand() function*/
seed = static_cast<unsigned>(time (NULL));
srand (seed);
//get number drawn by computer
numDrawn = DrawNum(MAX);
do //do loop
{
guessNumber++; //increment the number of guesses
cout << endl << "Please enter guess #" << guessNumber << ": "; //prints the prompt to gain user's guess
guess = GetGuess(MIN, MAX); //assigns return from GetGuess function to guess
if(guess < numDrawn) //if the user's guess is less than the computer's number
cout << "Try again, that guess is too low!" << endl << endl;
else if(guess > numDrawn) //if the user's guess is more than the computer's number
cout << "Try again, that guess is too high!" << endl << endl;
else if(guess == numDrawn) //if user guesses the computer's number
{
won = true; //reset won to true
cout << "That's the correct number!" << endl << endl; //print winning statement
}
else //changes any invalid guesses to "too low"
cout << "Try again, that guess is too low!" << endl << endl;
} //do loop
while (won != true && guessNumber < 6); //while user's guess is incorrect and guessnumber is under 6
if(won) //if won is true
{
gamesWon++; //increment games won
totalGames++; //increment total games
winnings = betAmount / guessNumber; //initialize winnings to specific formula
money += winnings; //add any winnings to user's money balance
percentageWon = 100.0 * (gamesWon/totalGames);//initialize percentage won with formula
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl
<< "You won this round!" << endl //print closing message telling winning statistics
<< "You have won $" << winnings << " in this round." << endl
<< "You now have $" << money << " total." << endl
<< "You have won " << percentageWon << "% of the games you have played." << endl
<< "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
} //if won is true
else //else
{
totalGames++; //increment total games played
percentageWon = 100.0 * (gamesWon/totalGames); //percentage of games won out of total
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl //print closing message telling losing statistics
<< "Sorry! The correct number was " << numDrawn << "." << endl
<< "You lost $" << betAmount << " in this round." << endl
<< "You now have $" << money << " total." << endl
<< "You have won " << percentageWon << "% of the games you have played." << endl
<< "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl << endl << endl;
}
cout << "Would you like to play another round? Enter Y/N: "; //ask the user if they want to play again
cin >> playAgain; //read input (Y/N) from user
if(playAgain != 'Y' || playAgain != 'y') //if playAgain is not yes
{ //print final message
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl
<< "Thanks for playing the High-Low Betting Game!" << endl
<< "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl << endl;
return 0; //return zero
}
void PrintHeading(int balance, int min, int max) //function that prints the initial heading of game
{ //PrintHeading function
cout << "===============================================" << endl
<< "* Welcome to the High-Low Betting Game!\t *" << endl
<< "* You have $" << balance << " to begin the game.\t *" << endl
<< "* Valid guesses are numbers between " << min << " and " << max << ".*" << endl
<< "===============================================" << endl;
return;
} //PrintHeading function
int GetBet(int balance) //function that prompts and obtains bet from user
{ //GetBet function
int bet;
cout << "Please enter the amount of money you want to bet: ";
cin >> bet; //bet is input from user
if(bet > 0 && bet <= balance) //if bet is valid(between zero and total money balance
return bet; //return the bet to main
else //if input is invalid
{
cout << "That is not a valid bet!" << endl
<< "Please enter a valid bet: "; //prompt for a new valid bet
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cin >> bet;
}
} //GetBet function
int DrawNum(int max) //function that calculates random number
{ //DrawNum function
double x = RAND_MAX + 1.0; //x and y are both auxiliary
int y; //variables used to do the
//calculation
y = static_cast<int> (1 + rand() * (max / x));
return (y); //return random number to main
} //DrawNum function
int GetGuess(int min, int max) //function that gets user's guess
{ //GetGuess function
int userGuess;
cin >> userGuess; //user inputs guess
if (userGuess >= min && userGuess <= max)
return userGuess; //if guess is between 1 and 100, return it to main
} //GetGuess function
|