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
#include <iostream>
using namespace std;
int main(){
char match;
int i,a,b;
a=0;
b=0;
for(i=1;i<=20;++i){
cout<<"Enter a if a has won or b if b has won"<<endl;
cin>>match;
if(match=='a'){
++a;
}
if(match=='b'){
++b;
}
if(a-b>=2 && a>=5){
cout<<"a won"<<" a="<<a<<" "<<"b="<<b<<endl;
break;
}
if(b-a>=2 && b>=5){
cout<<"b won b="<<b<<" a="<<a<<endl;
break;
}
if((a-b<2) && (i==20)|| (b-a<2 ) && (i==20)) {
cout<<"draw "<<" a="<<a<<" "<<"b="<<b<<endl;
break;
}
}
return 0;
}
Comments
Leave a comment