Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a coin tossing game using C++ functions that works as follows: 1. In m

ID: 3871365 • Letter: I

Question

Implement a coin tossing game using C++ functions that works as follows:

1. In main( ), ask the user for the number of times to play (N) then call the function play( ) (explained below) to run the game.

2.Function play( ) receives N and:

a. Ask the user to guess the next coin flip outcome (‘h’ for head, ‘t’ for tail).

b. If the user enters anything else than ‘h’ or ‘t’, ask her again until a valid input is entered.

c. Implement and call function flipCoin( ) to get the outcome of the next coin flip. The function randomly returns ‘h’ or ‘t’.

d. If the flip coin outcome matches the user’s guess, give her one point. If not, subtract one point. e. After repeating this process N times, the function returns (to main) the total number of points earned by the user (it may be negative).

f. Remember to initialize the point counter to zero at the beginning of the function.

3. Once back in main( ), print the user points and ask the user whether she wishes to play again. If yes, repeat the process from 1.

Explanation / Answer

#include <iostream>
#include <stdlib.h>
#include<time.h>
using namespace std;
//Function to return a random number between 0 and 1
int flipCoin()
{
//Minimum is assigned to zero and maximum is assigned to one
int min = 0, max = 1;
//Generates a random number between 0 and 1
double d = (min + (int) (rand() / (double) (RAND_MAX + 1) * (max - min + 1)));
//Returns the random number
return d;
}//End of function

//Function accepts number of players and returns the point
int play(int n)
{
//To store user flip character and random flip character
char userFlip, randomFlip;
//To store random number returned by the function flipCoin
int flipResult;
//To store point of the user
int point = 0;
srand(time(0));
//Loops till number of times entered by the user
for(int counter = 0; counter < n; counter++)
{
//Loops till valid flip coin character entered by the user 'h' or 't'
do
{
//Accept user flip coin data
cout<<" Guess the next coin flip outcome ('h' for head, 't' for tail)";
cin>>userFlip;
//Checks if the user flip data is 'h' or 't'
if(userFlip == 'h' || userFlip == 't')
{
//Call the flipCoin() function to generate a random number between 0 and 1 and stores the result
flipResult = flipCoin();
//Checks if the return value is 1
if(flipResult == 1)
//Store 'h' for user flip coin
randomFlip = 'h';
//Otherwise if it is zero
else
//Store 't' for user flip coin
randomFlip = 't';
//Checks if user flip coin character is equal to random coin flip character
if(randomFlip == userFlip)
//Increase the point by one
point++;
//Otherwise
else
//Decrease the point by one
point--;
break;
}//End of if condition
//Otherwise display error message for wrong character entered by the user
else
cout<<" Invalid data. Please enter again.";
}while(1);//End of do - while
}//End of for loop
//Return the point scored
return point;
}//End of function

//Main function definition
int main()
{
//To store number of times
int N;
//To store point returned by the play() function
int points;
//To store user choice 'y' or 'n'
char choice;
//Loop till user choice is 'n'
do
{
//Accept number of times
cout<<" Enter number of times to play (N)";
cin>>N;
//Calls the play() function and store the return point
points = play(N);
//Display user point
cout<<" User point: "<<points;
//Accept user choice
cout<<" Would you like to play again. (y/n)";
cin>>choice;
//Checks if user choice is 'N' or 'n' then stop
if(choice == 'N' || choice =='n')
break;
//Checks if user choice is 'Y' or 'y' then continue the game
else if(choice == 'Y' || choice =='y')
continue;
//Otherwise display error message
else
cout<<" Invalid choice.";
}while(1); //End of do - while
}//End of main function

Sample Run:


Enter number of times to play (N)4

Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)y

Invalid data. Please enter again.
Guess the next coin flip outcome ('h' for head, 't' for tail)t

User point: -2
Would you like to play again. (y/n)y

Enter number of times to play (N)3

Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)j

Invalid data. Please enter again.
Guess the next coin flip outcome ('h' for head, 't' for tail)k

Invalid data. Please enter again.
Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)h

User point: -1
Would you like to play again. (y/n)y

Enter number of times to play (N)8

Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)h

Guess the next coin flip outcome ('h' for head, 't' for tail)t

Guess the next coin flip outcome ('h' for head, 't' for tail)t

Guess the next coin flip outcome ('h' for head, 't' for tail)t

Guess the next coin flip outcome ('h' for head, 't' for tail)t

User point: -2
Would you like to play again. (y/n)n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote