C++ CODE. How do you keep count of score of the game and show the final winer an
ID: 3569292 • Letter: C
Question
C++ CODE. How do you keep count of score of the game and show the final winer and final result for this code?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <windows.h>
using namespace std;
void displayCoinshape();
void game();
void advance_coinspinner();
void download();
void finalCount();
//moved globally because you define it in main and in game()
string player1,player2;
string type;
int main()
{
//COLOR
system("COLOR F8");
//declare variables
//string player1,player2;
int player =1;
int time1=30000;
int win1,win2;
int count=0;
char guess;
char toss[]= {'h','t'};
char tos;
int n = 0;
int a[1];
char choice = 'Y';
//COIN
displayCoinshape();
//Game display prompt user his/her name and start or quit
//**** you don't capture the return type in game so I made type global too.*/
game();
//loop up to play 20 games
//**************Use your functions to do the work you have too many loops here:::::
//for(n=0;n<=1;n+=2)
srand(time(NULL));
do
{
if(player == 1)
{
cout<<"Player 1"<< "-"<<player1<< " throws the toss. ";
tos=toss[rand()%2];//generating random toss
player = 0;
cout<<"Player 2"<< "-"<<player2<< " guess the toss: ";
cout <<"Do you guess Heads (h) or Tails (t)? ";
cin>>guess;
advance_coinspinner();
// time=30000;
while (time1)
{
advance_coinspinner();
Sleep(100);
time1=time1-2000;
}
// download();
cout<<"Tossed coin is: "<<tos;
cout<<" Guessed toss is: "<<guess<<endl;
if(guess==tos)
{
cout<<player2<<" won! ";
win2++;//increment when player2 wins
}
else
{
cout<<player1<< " won! ";
win1++;//increment when player1 wins
}
}
else
{
cout<<"Player 2"<< "-"<<player2<< " throws the toss. ";
tos=toss[rand()%2];//generating random toss
player = 1;
cout<<"Player 1"<< "-"<<player1<< " guess the toss: ";
cout <<"Do you guess Heads (h) or Tails (t)? ";
cin>>guess;
if(guess==tos)
{
cout<<player1<<" won! ";
win2++;//increment when player2 wins
}
else
{
cout<<player2<< " won! ";
win1++;//increment when player1 wins
}
advance_coinspinner();
// time=30000;
while(time1)
{
advance_coinspinner();
Sleep(100);
time1=time1-2000;
}
//download();
cout<<"Tossed coin is: "<<tos;
cout<<" Guessed toss is: "<<guess<<endl;
}
} while (choice == 'Y' && n < 20);
}
void displayCoinshape()
{
cout << " **###### ###### ###***"<<endl
<< " **## ##***"<<endl
<< " **## || ##**" <<endl
<<" **## ||||| ##**" << endl
<< " **## || ##**" << endl
<< " **## || ##**" << endl
<< " **## || ##**" << endl
<< " **## || ##**" << endl
<< " **## || ##**" << endl
<< " **## ||||||||||||| ##**"<< endl
<< " **## ##**"<< endl
<<" **###### ###### ####*** " << endl
<< "::::::::::::::::::::::INSTRUCTIONS:::::::::::::::::::::::::" << endl
<< " " << endl
<< " " << endl
<< " WELCOME TO THE PENNIE GAME " <<endl
<< " READY TO PLAY - A - GAME? " << endl
<< " ` " << endl
<< " " << endl
<< ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" << endl;
}
void game()
{
char choice ='a';
//prompt user to enter Yes to start the game.
cout<<"Enter (Y) to play a two player game:====>";
//user entered yes
//cin>>yes;
cin >> choice;
choice = toupper(choice);
if (choice != 'Y')
{
cout << "Good Bye" << endl;
exit(0);
}
//*****let's capture upper and lower case and move it to upper so you don't have to error check later
// yes = toupper(yes);
//prompt user to enter an alias
cout<<"Player 1 - What is your alias? ";
//user entered an alias
cin>>player1;
cout<<endl;
//prompt second user to enter an alias
cout<<"Player 2 - What is your alias? ";
//user entered his/her alias
cin>>player2;
//prompt user to enter yes to start or no to quit the game
cout <<player1<< ", Press [Y] to start the game or [N] quit.";
cin >> choice;
choice = toupper(choice); //make choice upper case all the time.
if (choice != 'Y')
{
cout << "Good Bye" << endl;
exit(0);
}
}
void download()
{
//download will start value increment in 25 x50x75x100%
int val = 25;
for(int i = 0; i < 100; ++i){
cout << "=";
if(i % val == 0){
cout << i <<" % downloaded";
//Timer speed
Sleep(300);
// j is for the length of "=" download displayed in screen.
for(int j = 0; j < 38; ++j){
cout << "";
}
}
//at the end of 100% display this message:
if(i == 99)
{
cout << "100 % downloaded "<<endl;
}
}
return;
}
//
void advance_coinspinner()
{
static char coin[] = { 'O', '-', 'O', '|' };
static int nbars = sizeof(coin) / sizeof(char);
static int pos = 0;
printf(" %c ",coin[pos]);
fflush(stdout);
pos = (pos + 1) % nbars;
}
Explanation / Answer
here you go :)
comment if you have any doubts
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <windows.h>
using namespace std;
void displayCoinshape();
void game();
void advance_coinspinner();
void download();
void finalCount();
//moved globally because you define it in main and in game()
string player1,player2;
string type;
int main()
{
//COLOR
system("COLOR F8");
//declare variables
//string player1,player2;
int player =1;
int time1=30000;
int win1,win2;
int count=0;
char guess;
char toss[]= {'h','t'};
char tos;
int n = 0;
int a[1];
char choice = 'Y';
//COIN
displayCoinshape();
//Game display prompt user his/her name and start or quit
//**** you don't capture the return type in game so I made type global too.*/
game();
//loop up to play 20 games
//**************Use your functions to do the work you have too many loops here:::::
//for(n=0;n<=1;n+=2)
srand(time(NULL));
do
{
if(player == 1)
{
cout<<"Player 1"<< "-"<<player1<< " throws the toss. ";
tos=toss[rand()%2];//generating random toss
player = 0;
cout<<"Player 2"<< "-"<<player2<< " guess the toss: ";
cout <<"Do you guess Heads (h) or Tails (t)? ";
cin>>guess;
advance_coinspinner();
// time=30000;
while (time1)
{
advance_coinspinner();
Sleep(100);
time1=time1-2000;
}
// download();
cout<<"Tossed coin is: "<<tos;
cout<<" Guessed toss is: "<<guess<<endl;
if(guess==tos)
{
cout<<player2<<" won! ";
win2++;//increment when player2 wins
}
else
{
cout<<player1<< " won! ";
win1++;//increment when player1 wins
}
}
else
{
cout<<"Player 2"<< "-"<<player2<< " throws the toss. ";
tos=toss[rand()%2];//generating random toss
player = 1;
cout<<"Player 1"<< "-"<<player1<< " guess the toss: ";
cout <<"Do you guess Heads (h) or Tails (t)? ";
cin>>guess;
if(guess==tos)
{
cout<<player1<<" won! ";
win2++;//increment when player2 wins
}
else
{
cout<<player2<< " won! ";
win1++;//increment when player1 wins
}
advance_coinspinner();
// time=30000;
while(time1)
{
advance_coinspinner();
Sleep(100);
time1=time1-2000;
}
//download();
cout<<"Tossed coin is: "<<tos;
cout<<" Guessed toss is: "<<guess<<endl;
}
} while (choice == 'Y' && n < 20);
}
void displayCoinshape()
{
cout << " **###### ###### ###***"<<endl
<< " **## ##***"<<endl
<< " **## || ##**" <<endl
<<" **## ||||| ##**" << endl
<< " **## || ##**" << endl
<< " **## || ##**" << endl
<< " **## || ##**" << endl
<< " **## || ##**" << endl
<< " **## || ##**" << endl
<< " **## ||||||||||||| ##**"<< endl
<< " **## ##**"<< endl
<<" **###### ###### ####*** " << endl
<< "::::::::::::::::::::::INSTRUCTIONS:::::::::::::::::::::::::" << endl
<< " " << endl
<< " " << endl
<< " WELCOME TO THE PENNIE GAME " <<endl
<< " READY TO PLAY - A - GAME? " << endl
<< " ` " << endl
<< " " << endl
<< ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::" << endl;
}
void game()
{
char choice ='a';
//prompt user to enter Yes to start the game.
cout<<"Enter (Y) to play a two player game:====>";
//user entered yes
//cin>>yes;
cin >> choice;
choice = toupper(choice);
if (choice != 'Y')
{
cout << "Good Bye" << endl;
exit(0);
}
//*****let's capture upper and lower case and move it to upper so you don't have to error check later
// yes = toupper(yes);
//prompt user to enter an alias
cout<<"Player 1 - What is your alias? ";
//user entered an alias
cin>>player1;
cout<<endl;
//prompt second user to enter an alias
cout<<"Player 2 - What is your alias? ";
//user entered his/her alias
cin>>player2;
//prompt user to enter yes to start or no to quit the game
cout <<player1<< ", Press [Y] to start the game or [N] quit.";
cin >> choice;
choice = toupper(choice); //make choice upper case all the time.
if (choice != 'Y')
{
cout<< "Scores are: "<<player1 << ": "<<win1<<" and "<<player2<<" = "<<win2<<endl;
if(win1>win2)cout<<player1<<" wins!!"<<endl;
else if(win1<win2)cout<<player2<<" wins!!"<<endl;
else cout<<"Its a draw!!"<<endl;
cout << "Good Bye" << endl;
exit(0);
}
}
void download()
{
//download will start value increment in 25 x50x75x100%
int val = 25;
for(int i = 0; i < 100; ++i){
cout << "=";
if(i % val == 0){
cout << i <<" % downloaded";
//Timer speed
Sleep(300);
// j is for the length of "=" download displayed in screen.
for(int j = 0; j < 38; ++j){
cout << "";
}
}
//at the end of 100% display this message:
if(i == 99)
{
cout << "100 % downloaded "<<endl;
}
}
return;
}
//
void advance_coinspinner()
{
static char coin[] = { 'O', '-', 'O', '|' };
static int nbars = sizeof(coin) / sizeof(char);
static int pos = 0;
printf(" %c ",coin[pos]);
fflush(stdout);
pos = (pos + 1) % nbars;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.