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

This game is a simple two-player game. The two Players take turns in tossing a c

ID: 3770226 • Letter: T

Question

This game is a simple two-player game. The two Players take turns in tossing a coin. A toss can result in a “head” or a “tail”. Before one player tosses the coin, the other player guesses a head or tail. If the guess is correct, the player who did the guessing gets 2 points. If the guess is not correct, the player who did the tossing gets 1 point.

Write a C++ program that simulates this game between 2 players. Your program should ask the user how many rounds the game should be played. The user enters the number of rounds the game should be played. Then the guessing game should be played the number of rounds the user enters. Here, both the guessing and the tossing of the coin should be done by the computer using the rand() function. You have to generate a random number between 1 and 2. 1 can represent “head” and 2 can represent “tail”. In this program, the only input the user provides is the number of rounds the game should be played. Both the players begin the game with zero points. During the entire game, you need to keep track of the total number of points acquired by both the players.

After all the rounds of the guessing game are played, your C++ program should display the winner. Your program should also display the final total points of both the players.

Player 1 wins if the total number of points of Player 1 is more than that of Player 2.

Player 2 wins if the total number of points of Player 2 is more than that of Player 1.

It’s a tie if the total number of points of both Player 1 and Player 2 are equal.

Explanation / Answer

#include <stdlib.h>
#include <iostream>
#include <time.h>

#define MAX 1000

using namespace std;


int main() {

int rounds,score_player1=0,score_player2=0;
int player1_toss,player2_toss,player1_guess,player2_guess;

srand(time(NULL));
cout<<"Enter the number of rounds to be played::";
cin>>rounds;

while(rounds--)
{
player2_guess = rand()%2+1;
player1_toss = rand()%2+1;
if(player1_toss==player2_guess)
score_player2+=2;
else
score_player1+=1;

player1_guess = rand()%2+1;
player2_toss = rand()%2+1;
if(player2_toss==player1_guess)
score_player1+=2;
else
score_player2+=1;
}

if(score_player1>score_player2)
cout<<"Player 1 won!!!!"<<endl;
else if(score_player1<score_player2)
cout<<"Player 2 won!!!!"<<endl;
else
cout<<"There is a tie ";

cout<<"Final Score ";
cout<<"Player 1 -> "<<score_player1<<endl;
cout<<"Player 2 -> "<<score_player2<<endl;
return 1;
}

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