Chat with us, powered by LiveChat The program must generate a pseudo-random number between 1 and 100, inclusive | Writedemy

The program must generate a pseudo-random number between 1 and 100, inclusive

The program must generate a pseudo-random number between 1 and 100, inclusive

Question

The program must generate a pseudo-random number between 1 and 100, inclusive. The user will then be presented with opportunities to guess the number. After each guess, the program must indicate whether the guess was too high, too low, or correct.

To make the game more interesting, the user will bet on each number. Initially, the program will give the user $1000. Each round, prompt the user for a bet. Then prompt for guesses until the user is correct, or has made 6 wrong guesses. The payoff is calculated as follows:

1. If the player guesses correctly within the first 6 guesses, the player wins bet / # guesses. So if the player bet $100 and correctly guessed on the 4th try, the payoff is $25.

2. If the player does not guess correctly within the first 6 guesses, the player loses the amount bet.

Once the round has ended (either by a correct guess or by using up the 6 guesses), the program must display the current status (see the sample execution output below for the minimum required information) and prompt the user to see if he/she wants to play again. This will continue until the user indicates that they do not want to play again or until the player runs out of money.


Using functions and random numbers

To help you implement this program, we have provided function prototypes below. A large part of your grade on this and all future projects in this course is based on how well you utilize functions and parameters. You must write and implement the corresponding functions in your program based on these prototypes. You may add additional functions if you wish, but there is no need to.

/*

PrintHeading simply prints the introductory output.

Parameters: initial amount of money received

*/

void PrintHeading(int money)

/*

GetBet prompts for and reads in a bet. The function performs all

error checking necessary to insure that a valid bet is read in

and does not return until a valid bet is entered.

Parameters:

money: the amount of money the player currently has

bet: the bet chosen by the user

*/

void GetBet(int money, int& bet);

/*

GetGuess reads in a guess. The user is not prompted for the guess in

this function. The user only gets one chance to input a guess value.

Return Value: the value of the guess if the input is valid

0 if the input guess was not valid

*/

int GetGuess(void);

/*

CalcNewMoney determines the amount of money the player has won or

lost during the last game.

Parameters:

money: the amount of money the player had going into the game

bet: the amount the player bet on the current game

guesses: the number of guesses it took the player to win.

-1 if the player did not guess correctly

Return Value: the new amount of money the player has

*/

int CalcNewMoney(int money, int bet, int guesses);

/*

PlayAgain prompts the user to play the game again and reads in a response,

using a single character to represent a yes or no reply.

Error checking is performed on that response.

Return Value: true if the user wants to play again

false if the user does not want to play again.

*/

bool PlayAgain(void);

/*

PlayGame plays a single game, performing all the necessary calculations,

input, and output.

Parameters:

money: the amount of money the player has at the start of the game.

Return Value: how much the player has after the game.

*/

int PlayGame(int money);

Generating Pseudo-Random Numbers

The function rand( ) will generate a pseudo-random number between 0 and RAND_MAX, and was fully discussed in lectures. Because an algorithm implementing number generation on a computer cannot truly be random, this function actually computes a complex sequence of numbers that appear to be random. You are required to use theDrawNum toscalethe value received from rand() into the appropriate range. You may copy it from this lottery.cpp program::

#include<iostream> /* for standard I/O routines */

#include<ctime> /* to access the computer’s clock */

#include<cstdlib> /* needed for rand() and RAND_MAX */

usingnamespace std;

/* ================================================================ */

/* function prototypes for user-defined functions */

int DrawNum (int); /* prototype for random number */

/* generator function */

/* ================================================================ */

int main ()

{

/* named constants */

constint START_BALANCE = 10; /* player’s starting balance */

constint WINNINGS = 500; /* what player wins, if he/she does */

constint BET = 1; /* the bet amount required */

constint MIN = 1; /* minimum value to be picked or drawn */

constint MAX = 999; /* maximum value to be picked or drawn */

/* declare variables */

int numPicked, /* number player picks */

numDrawn, /* number computer draws */

balance = START_BALANCE, /* start off with this amount */

/* on hand */

betNumber = 0; /* number of current bet */

bool won =false; /* reset to true if player */

/* does win */

unsignedint seed; /* seeds (initializes) rand() */

/* print an introductory title */

cout

<<“======================================================” << endl;

cout

<<” Welcome to the Mini-Lottery!!!” << endl;

cout

<<“======================================================” << endl;

/* get a 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);

/* keep playing until either the player runs out of money */

/* or wins once */

while ((balance != 0) && (!won))

{

/* increment and print bet number */

betNumber++;

cout << endl <<“This is bet # ” << betNumber << endl;

/* decrement balance and get number picked by player */

balance = balance – BET;

cout <<“Pick a number between ” << MIN <<” and ” << MAX

<<” -> “;

cin >> numPicked;

while ((numPicked < MIN) || (numPicked > MAX))

{

cout <<“Invalid number! Please try again -> “;

cin >> numPicked;

} /* end of error handling for number picked */

/* get number drawn by computer and print it */

numDrawn = DrawNum (MAX);

cout <<“Number Drawn was: ” << numDrawn << endl;

/* check for a match */

if (numPicked == numDrawn)

{ /* do match */

won =true;

cout <<“The numbers match! You win!” << endl;

}

else /* do NOT match */

cout <<“Sorry, no match.” << endl;

} /* while balance not zero and not won */

/* print out an appropriate closing message */

cout << endl <<“Game over !!!” << endl;

if (won)

cout <<“You spent $” << START_BALANCE – balance

<<” to win ” << WINNINGS << endl;

else

cout <<“Oops! You\’re BROKE! Better luck next time!”

<< endl << endl;

return (0);

} /* end of main function */

/* ================================================================ */

/*******************************/

/* function drawNum */

/*******************************/

int DrawNum (int max) /* the maximum number to generate */

/* The standard function rand returns an integer between 0 and

RAND_MAX, and requires no parameter. Our function drawNum calls

rand and does the necessary calculations to convert the number

rand returns to a value which falls between 1 and max, which is

what we need here. Note RAND_MAX is 32767 in many systems.

This is how the function works:

0 <= rand() <= RAND_MAX we know how rand() behaves

<= rand() * MAX/(RAND_MAX + 1) multiplying by MAX and

<= RAND_MAX * MAX/(RAND_MAX + 1) dividing by RAND_MAX + 1

= MAX * (RAND_MAX/(RAND_MAX + 1))

< MAX

1 <= 1 + rand() * MAX/(RAND_MAX + 1) < MAX + 1

1 <= the above truncated to an int <= MAX

*/

{ /* function drawNum */

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); /* y contains the result */

} /* function drawNum */

/* End of PROGRAM Lottery */

/* ================================================================ */

Additionally, the sequence of numbers generated will be the same every time you run your program unless you seed the number generator, that is, give it a random starting value. Traditionally, the current time on the computer is used. To do this, as the first line (after variable declarations) in main, include the following code:

srand(static_cast<unsigned>(time(NULL)));

This function only needs to be called once during your entire program. To use these functions, you will need to #include the appropriate header files.


Operation Specifications

· Your program must deal with incorrect input. For example, when prompted for a bet, if the user does not enter an integer, you must display an appropriate message and re-prompt the user for input. The only situation where you should not re-prompt the user after incorrect input is during the “guess a number” prompt. If the user does not guess a valid integer or does not enter an integer, the guess will be considered wrong. Your program may display either the “Too high” or “Too low” message in this case. The invalid guess must still count as a guess.

· When incorrect input is entered, your program must then ignore everything entered up until the newline character.

· If the payoff is not an integer (i.e. 100 / 3), you must truncate the decimal portion.

· Your program should not be case sensitive. That is, answering the “play again” question with either an upper case or a lower case letter is acceptable.

· The bet cannot be for more money than the player currently has and must be a positive integer.

· If the user does not guess correctly within the given 6 guesses, your program must print the correct answer in addition to the usual status information.


Example Run

A sample execution of the program is below. This output is very basic just to show you the fundamental elements. You must use your creativity to make your own output look nicer and more spaced.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Welcome to the high-low betting game.
You have $1000 to begin the game.
Valid guesses are numbers between 1 and 100.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Please enter a bet: 500
Guess 1: 50
Too low…
Guess 2: 75
Too high…
Guess 3: 62
Too low…
Guess 4: 67
Too high…
Guess 5: 63
Correct!
You just won $100
You have $1100
You have won 100% of the games you played

Play again? y
Please enter a bet: abc
That isn’t a valid bet!
Please enter a bet: -1
That isn’t a valid bet!
Please enter a bet: 285
Guess 1: how can i win???
Too low…
Guess 2: 100011
Too low…
Guess 3: -1
Too low…
Guess 4: 52
Too low…
Guess 5: 80
Too high…
Guess 6: 70
Too low…
Sorry… the correct answer was 78
You lost $285
You have $815
You have won 50% of the games you played

Play again? n
Thanks for playing.


.

Use Of Data Structures

This project doesnotrequire the use of any data structures and you should not use any. The only classes you may use are the standard string class and standard iostream classes.

Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.

Do you need an answer to this or any other questions?

About Writedemy

We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.

How It Works

To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Are there Discounts?

All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.

Hire a tutor today CLICK HERE to make your first order