// my problem is in the playerscore and computerscore always gives me 0 or 1
// i cant know who finally won plz i need ur help
//at the end of the program it should gives me the score of computer and the user out of 5 of the total rounds
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void toss(int & x)
//0=heads 1=tails
x=rand()%2;
int result(int a, int b, int c)
{
int count=0;
if(a==0)
count=count+1;
if(b==0)
count=count+1;
if(c==0)
count=count+1;
return count;
}
int main ()
{
srand(time(0));
for(int i=0;i<20;i++)
{
cout<<"GAME212"<<endl;
system("CLS");
}
int playagain=1;
do
{
int play;
cout<<"Enter 1 to play or 0 to quit: ";
cin>>play;
if(play==0)
break;
if(play==1)
{ int turn;
for(turn=1;turn<=10;turn++)
{
int coin1, coin2, coin3, times, playerscore=0, computerscore=0, playerwins=0, computerwins=0;
if(turn%2==1)
{
cout<<"\n\nHow many times do you want to toss the three coins together?";
cin>>times;
playerwins=0;
computerwins=0;
for(int j=1;j<=times && playerscore<5 && computerscore<5;j++)
{
toss(coin1);
toss(coin2);
toss(coin3);
if(coin1==0)
cout<<"\nCoin1: Heads"<<endl;
else
cout<<"\nCoin1: Tails"<<endl;
if(coin2==0)
cout<<"Coin2: Heads"<<endl;
else
cout<<"Coin2: Tails"<<endl;
if(coin3==0)
cout<<"Coin3: Heads\n"<<endl;
else
cout<<"Coin3: Tails\n"<<endl;
int countheads = result(coin1,coin2,coin3);
if(countheads>1)
{
playerwins=playerwins+1;
if(playerwins>times/2)
playerscore=playerscore+1;
if(computerwins>times/2)
computerscore=computerscore+1;
cout<<"You win this round"<<endl;
cout<<"Your round score: "<<playerwins<<"/"<<times<<endl;
cout<<"Computer round score: "<<computerwins<<"/"<<times<<endl;
cout<<"Your Score: "<<playerscore<<"/5"<<endl;
cout<<"Computer Score: "<<computerscore<<"/5"<<endl;
}
else
{
computerwins=computerwins+1;
if(playerwins>times/2)
playerscore=playerscore+1;
if(computerwins>times/2)
computerscore=computerscore+1;
cout<<"You lose this round"<<endl;
cout<<"Your round score: "<<playerwins<<"/"<<times<<endl;
cout<<"Computer round score: "<<computerwins<<"/"<<times<<endl;
cout<<"Your Score: "<<playerscore<<"/5"<<endl;
cout<<"Computer Score: "<<computerscore<<"/5"<<endl;
}
}
}
if(turn%2==0)
{
//I chose to give it a limit of 8 times to toss the coins because it would be rediculous to toss it 1000 times for example
times=rand()%8;
cout<<"\n\nThe computer chose to toss the three coins "<<times<<" times"<<endl;
playerwins=0;
computerwins=0;
for(int j=1;j<=times && playerscore<5 && computerscore<5;j++)
{
toss(coin1);
toss(coin2);
toss(coin3);
if(coin1==0)
cout<<"\nCoin1: Heads"<<endl;
else
cout<<"\nCoin1: Tails"<<endl;
if(coin2==0)
cout<<"Coin2: Heads"<<endl;
else
cout<<"Coin2: Tails"<<endl;
if(coin3==0)
cout<<"Coin3: Heads\n"<<endl;
else
cout<<"Coin3: Tails\n"<<endl;
int countheads = result(coin1,coin2,coin3);
if(countheads>1)
{
playerwins=playerwins+1;
if(playerwins>times/2)
playerscore=playerscore+1;
if(computerwins>times/2)
computerscore=computerscore+1;
cout<<"You win this round"<<endl;
cout<<"Your round score: "<<playerwins<<"/"<<times<<endl;
cout<<"Computer round score: "<<computerwins<<"/"<<times<<endl;
cout<<"Your Score: "<<playerscore<<"/5"<<endl;
cout<<"Computer Score: "<<computerscore<<"/5"<<endl;
}
else
{
cout<<"You lose this round"<<endl;
computerwins=computerwins+1;
if(playerwins>times/2)
playerscore=playerscore+1;
if(computerwins>times/2)
computerscore=computerscore+1;
cout<<"Your round score: "<<playerwins<<"/"<<times<<endl;
cout<<"Computer round score: "<<computerwins<<"/"<<times<<endl;
cout<<"Your Score: "<<playerscore<<"/5"<<endl;
cout<<"Computer Score: "<<computerscore<<"/5"<<endl;
}
}
}
}
cout<<"Enter 1 to play again or 2 to exit: ";
cin>>playagain;
if(playagain==1);
}
}
while(playagain==1);
return 0;
}
samer.aboufakher.3 0 Newbie Poster
gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven
samer.aboufakher.3 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.