Question

A simple game of chance Using a while loop write a simple coin flip game. Specifics:...

A simple game of chance

Using a while loop write a simple coin flip game.

Specifics:

The user starts with a bank of $10.00
It costs a dollar to play
A correct guess pays $2.00
The while loop is used for the game loop. It should ask if the user wants to play.
The while loop will check for a value of 'Y' or 'N'. You should also allow for lower case letters to be input.
You MUST prime the while loop by asking if the user wants to play before the loop starts.
You should output the bank before asking for a guess.
You should use the random number generator to generate a value of 0 and 1. A value of 0 = Heads (H), and a value of 1 = Tails (T);
Your program will take in a value of 'h' (Heads) or 't' (Tails) as the input value of the guess.
To determine if the user wins you will compare the users guess to the value generated by the random number generator.
Note that a conversion will have to take place to convert the number generated to a character.
The game should continue until the user indicates they do not want to play anymore. Your code should ask if the user wants to play again.
When the while loop ends you should thank the user for playing and output the bank.
A do-while loop is not a while loop. You MUST use a while loop.

C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#include<stdlib.h>

#include<time.h>

using namespace std;

int main(){

              //seeding random number generator with time to generate different numbers

              srand(time(NULL));

              //starting balance

              double balance=10.00;

              char ch;

              //asking user if he wants to play; before loop starts

              cout<<"Do you want to play the game: (Y/N) ";

              cin>>ch;

              char secret,user_guess;

              //looping as long as user wishes to play

              while(toupper(ch)=='Y'){

                           //reducing balance by 1, for playing the game

                           balance--;

                           //displaying balance

                           cout<<"Your bank has $"<<balance<<endl;

                           //generating a random number 0 or 1

                           int num=rand()%2;

                           if(num==0){

                                         //secret character is h

                                         secret='h';

                           }else{

                                         //secret character is t

                                         secret='t';

                           }

                           //asking user guess

                           cout<<"Enter your guess: (h or t) ";

                           cin>>user_guess;

                           //checking if match

                           if(tolower(user_guess)==secret){

                                         balance+=2;//correct, adding 2 to balance

                                         cout<<"Right guess! Your current balance is $"<<balance<<endl;

                           }else{

                                         //wrong

                                         cout<<"Wrong guess! It was "<<secret<<endl;

                           }

                           //checking if balance is now zero

                           if(balance==0){

                                         //automatically terminating the game

                                         cout<<"Game over! You have no money left!"<<endl;

                                         ch='N';

                           }else{

                                         //asking user if he likes to play again

                                         cout<<"Do you want to play again? (Y/N) ";

                                         cin>>ch;

                           }

              }

              //displaying greeting and final bank

              cout<<"Thank you! Your final bank has $"<<balance<<endl;

              return 0;

}

/*OUTPUT 1*/

Do you want to play the game: (Y/N) y

Your bank has $9

Enter your guess: (h or t) h

Wrong guess! It was t

Do you want to play again? (Y/N) y

Your bank has $8

Enter your guess: (h or t) t

Wrong guess! It was h

Do you want to play again? (Y/N) y

Your bank has $7

Enter your guess: (h or t) h

Wrong guess! It was t

Do you want to play again? (Y/N) y

Your bank has $6

Enter your guess: (h or t) h

Right guess! Your current balance is $8

Do you want to play again? (Y/N) y

Your bank has $7

Enter your guess: (h or t) h

Right guess! Your current balance is $9

Do you want to play again? (Y/N) n

Thank you! Your final bank has $9

/*OUTPUT 2*/

Do you want to play the game: (Y/N) N

Thank you! Your final bank has $10

Add a comment
Know the answer?
Add Answer to:
A simple game of chance Using a while loop write a simple coin flip game. Specifics:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Microsoft Visual Source, C++, Course: CIS-5, Intro to Programming) Write a program that starts a player...

    (Microsoft Visual Source, C++, Course: CIS-5, Intro to Programming) Write a program that starts a player off with a bank of $15.00. A coin will flip and randomly choose heads or tails.The user will guess heads or tails to win. If the coin flip matches the player's guess his bet will be doubled. It costs 1 dollar to play and the program will bet that amount automatically each time as long as there is the available bank amount. Note: Do...

  • In C++ please Create a coin-flipping game. Ask the user how many times to flip the...

    In C++ please Create a coin-flipping game. Ask the user how many times to flip the coin, and use the random function to determine heads or tails each time a coin is flipped. Assume the user starts with $50. Every time the coin is flipped calculate the total (heads +$10, tails -$10). Create another function to test if the user has gone broke yet (THIS FUNCTION MUST RETURN A BOOLEAN TRUE/FALSE VALUE). End the program when the user is broke...

  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly...

    newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing your code easier. You can write this a number of ways, but try to stick to the instructions outlined here. Write three methods to make your game work. The first method should generate a random number and return the value to main...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Write a program that simulates the toss of a coin. Whenever a coin is tossed the...

    Write a program that simulates the toss of a coin. Whenever a coin is tossed the result will be either a head or tail. Prompt the user as shown by entering an ‘H’ for heads or ‘T’ for tails. Use a loop for input validation of an ‘h’ or ‘t’. Make sure that both an upper case and lower case will be accepted. Have the computer generate a random number. Assign a char variable a (‘h’ ^ ’t’) head or...

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

  • Can someone please rewrite this code using a while loop? Please write it in python. #The...

    Can someone please rewrite this code using a while loop? Please write it in python. #The winning number my_number = 12 #variable for the input name user_n = input("Hello! What is your name?") #Ask the user print("Well",user_n,": I am thinking of a number between 1 and 20." + "Take a guess.") user_n = float(input("Take a guess.")) #if statements if user_n == my_number: print("Good job" ,user_n, "You guessed my number!") if user_n > my_number: print("Your guess is high. You lose.") if...

  • 4. Boris and Natasha agree to play the following game. They will flip a coin 5...

    4. Boris and Natasha agree to play the following game. They will flip a coin 5 times in a row. They will compute S = ( number of heads H – number of tails T). a) Boris will pay Natasha S. Graph Natasha’s payoff as a function of S. What is the expected value of S? b) How much should Natasha be willing to pay Boris to play this game? After paying this amount, what is her best case and...

  • Boris and Natasha agree to play the following game. They will flip a (fair) coin 5...

    Boris and Natasha agree to play the following game. They will flip a (fair) coin 5 times in a row. They will compute S = (number of heads H – number of tails T). a) Boris will pay Natasha S. Graph Natasha’s payoff as a function of S. What is the expected value of S? b) How much should Natasha be willing to pay Boris to play this game? After paying this amount, what is her best case and worst...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT