Programming Question: Heads or Tails Game Simulation. This game is a simple two-
ID: 3770228 • Letter: P
Question
Programming Question: Heads or Tails Game Simulation. 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. Below is the main function with the logic you need to follow. int main() { //Get the number of rounds of game to be played //for each round //player1 guesses and player2 tosses. //check if player1 guess is correct. And Keep track of the points for player1 and player2. //player2 guesses and player1 tosses. //check if player2 guess is correct. And keep track of the points of player1 and player2. //After all the rounds are played display who is the winner or the if it was a tie system("pause"); return 0; }
Explanation / Answer
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main() {
// your code goes here
int n;
cin>>n;
int p1;
int p2;
int p1_c=0,p2_c=0;
srand (time(NULL));
for(int i=0;i<n;i++){
if(i%2==0){//player 1 toss
p1 = rand() % 2;
p2 = rand() % 2;//player 2 guessed
if(p1==p2)
p2_c=p2_c+2;
else
p1_c++;
}
else{
p1 = rand() % 2;//player 1 guessed
p2 = rand() % 2;//player 2 tossed
if(p1==p2)
p1_c=p1_c+2;
else
p2_c++;
}
}
cout<< "Player 1 got "<<p1_c<<" points"<<endl;
cout<< "Player 2 got "<<p2_c<<" points"<<endl;
if(p1_c > p2_c)
cout<< "Player 1 wins the game";
else if(p1_c < p2_c)
cout<< "Player 2 wins the game";
else
cout<< "The game ends in a tie";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.