Write a program to keep track of a match consisting of a series of games between two people: player A and player B, and report the outcome. The input consists of a sequence of letters A or B. If the input is A, it indicates that A has won a game. If it is B, then it indicates B has won a game. The first player to win 5 or more games with a difference of 2 or more games between him and his opponent wins the match. If no player wins the match in 20 games then the match is declared a tie after these 20 games have been played.
INPUT
### The next n lines contain a char value each
c0
c1
...
cn
n ≤ 20 . ci is the outcome of the ith game. It can take a value of either A/B. A indicates that this game was won by Player A and B indicates that it was won by Player B. The input will end when a player has won according to the given rules or 20 characters have been given.
OUTPUT
At the end of the input, if player A wins, output “ A”, If player B wins output “B”. If no one has won, output “Tie”
Note: DO NOT output the quotes
Please print a newline( using “cout<<endl;” is one way to do it) after printing your answer
#include<iostream>
using namespace std;
int main(){
int total_games = 0; //it will count the number of games played
char game; //it is the variable to store the wins (input)
int win_by_A=0; //it will count the game won by A
int win_by_B=0; //it will count the game won by B
while(total_games<=20){ //If the game played is less that 20, loop will get execute
cin>>game; //take the input as winner
total_games++; // increment the count of total game played
if(game=='A'){ //if input is 'A' (game won by A)
win_by_A++; // increment the win_by_A by 1
}
else if(game=='B'){ //if input is 'B' that is game won by B
win_by_B++; // increment the win_by_B by 1
}
/*if the wins by A exceeds 5 and also the difference between wins become greater than 2,
'A' will be displayed as winner and program will end */
if(win_by_A>=5 and win_by_A-win_by_B>=2){
cout<<"A"<<endl;
exit(0);
}
/*if the wins by B exceeds 5 and also the difference between wins become greater than 2,
'B' will be displayed as winner and program will end */
if(win_by_B>=5 and win_by_B-win_by_A>=2){
cout<<"B"<<endl;
exit(0);
}
}
//this line will get executes when the above loop got break that is total games will exceed 20
cout<<"Tie"<<endl;
system("pause>null");
}
Comments
Leave a comment