-
bool functions
ok. I am having a problem with a function that calls a boolean. Here is my code. any comments appreciated!
bool showcomp(char isgameover);
void showcomphand(bool isgameover)
{
cout<<"My hand is: ";
if(!isgameover)
{
cout<<hiddencard<<" "<<endl;
}
for( int i = !isgameover; comphand[i] !=0; i++)
{
switch (comphand[i])
{
case 1: cout<<"A ";
break;
case 11: cout<<"J ";
break;
case 12: cout<<"Q ";
break;
case 13: cout<<"K ";
break;
default: cout<<comphand[i]<<" "<<endl;
break;
}
}
cout<<endl;
}
-
use code tags!
what is the problem exactly?
the if statement do look suspect tho..
is isgameover an int?
when last game is played does it really equal 0?
didnt see rest of the code so just asking..
-
here is my code
/*Epsilon Omega Gamma*/
#ifndef _epsilonomegagamma_h
#define _epsilonomegagamma_h
void dealcomp(int number);
void dealme(int number);
bool showcomp(char isgameover);
void showme();
void blackjack();
int randnumber();
int randnumber1;
void newround();
bool compturn();
bool myturn();
short comphand[11];
int comphandsize, comphandvalue, compace;
int myhand[11], myhandsize, myhandvalue, myace, cardsleft;
int i=0;
char hiddencard;
int start();
int number;
bool continuegame1;
bool continuegame2;
void blackjack()
{
cardsleft=52;
hiddencard=2;
for (i=0; i<11; i++)
{
comphand[i]=0;
myhand[i]=0;
}
}
int randnumber()
{
static bool card[52];
if(cardsleft==52)
{
for(i=0; i<52; i++)
{
card[i]=1;
}
}
randnumber1=rand()%52;
while(!card[number])
{
randnumber1=rand()%52;
}
cardsleft--;
card[number]=false;
return (randnumber1%13)+1;
}
void newround()
{
bool continue1=true, continue2=true, over=false;
compace=0, comphand[11]=0, myace=0, myhand[11]=0;
for(i=0; i<11; i++)
{
comphand[i]=0;
myhand[i]=0;
}
cardsleft=52;
dealcomp(2);
showcomp(false);
dealme(2);
showme();
if(comphand[11]==21||myhand[11]==21)
{
continuegame1=false, continuegame2=false;
}
while((continuegame1||continuegame2) && comphand[11] < 22 && myhand[11] <22)
{
continuegame1=compturn();
if(continuegame1||continuegame2)
{
continuegame2=myturn();
}
}
cout<<endl;
if(comphandvalue > 21 && myhandvalue > 21)
{
cout<<"We are both over."<<endl;
}
if(comphandvalue > 21 && myhandvalue <=21)
{
cout<<"I am over."<<endl;
over=1;
}
if(myhandvalue > 21 && comphandvalue <=21)
{
cout<<"You are over."<<endl;
over=1;
}
if(comphandvalue > myhandvalue && !over)
{
cout<<"I have "<<comphandvalue<<", which beats your "<<myhandvalue<<".";
cout<<"You lose."<<endl;
}
if(myhandvalue > comphandvalue && !over)
{
cout<<"You have "<<myhandvalue<<", which beats my "<<comphandvalue<<".";
cout<<"You win."<<endl;
}
if(myhandvalue == comphandvalue && !over)
{
cout<<"Tie. We both have "<<comphandvalue<<"."<<endl;
}
}
void dealcomp(int number)
{
for( ; number; number--)
{
int newcard = randnumber();
comphand[comphandsize] = newcard;
comphandsize++;
if(newcard == 1 && comphandvalue <=10)
{
compace++;
comphandvalue +=11;
}
else if(newcard > 10)
{
comphandvalue +=10;
}
else
{
comphandvalue += newcard;
}
if(comphandvalue > 21 && compace)
{
compace--;
comphandvalue -=10;
}
}
}
void dealme(int number)
{
for( ; number; number--)
{
int newcard=randnumber();
myhandsize++;
if(newcard == 1 && myhandvalue <=10)
{
myace++;
myhandvalue +=11;
}
else if(newcard >10)
{
myhandvalue +=10;
}
else
{
myhandvalue +=newcard;
}
if(myhandvalue >21 && myace)
{
myace--;
myhandvalue -=10;
}
}
}
bool compturn()
{
if(comphandvalue < 17 && compace || comphandvalue < 17)
{
cout<<"I will take a card."<<endl;
dealcomp(1);
showcomp(false);
return true;
}
cout<<"I'm not taking a card."<<endl;
return false;
}
bool myturn()
{
char addcard='Q';
cout<<"Do you want a card? y = yes, n = no."<<endl;
cin>>addcard;
while(1)
{
switch(addcard)
{
case 'y':
case 'Y': cout<<"You take a card: "<<endl;
dealme(1);
showme();
return true;
case 'n':
case 'N': return false;
default: cout<<endl<<"Huh? Do you want another card or not. y = yes, n = no."<<endl<<" >"<<endl;
cin>>addcard;
break;
}
}
}
void showcomphand(bool isgameover)
{
cout<<"My hand is: ";
if(!isgameover)
{
cout<<hiddencard<<" "<<endl;
}
for( int i = !isgameover; comphand[i] !=0; i++)
{
switch (comphand[i])
{
case 1: cout<<"A ";
break;
case 11: cout<<"J ";
break;
case 12: cout<<"Q ";
break;
case 13: cout<<"K ";
break;
default: cout<<comphand[i]<<" "<<endl;
break;
}
}
cout<<endl;
}
void showme()
{
cout<<"Your hand: ";
for( int i =0; myhand[i] !=0; i++)
{
switch (myhand[i])
{
case 1: cout<<"A ";
break;
case 11: cout<<" J ";
break;
case 12: cout<<"Q ";
break;
case 13: cout<<"K ";
break;
default: cout<<myhand[i]<<" "<<endl;
break;
}
}
cout<<endl;
}
int start()
{
srand(time(0));
void mygame();
char BJNewgame='q';
cout<<"Blackjack. Wanna play? y = yes, n = no."<<endl<<">";
cin>>BJNewgame;
cout<<endl;
while(BJNewgame !='n' && BJNewgame !='N')
{
switch(BJNewgame)
{
case 'y':
case 'Y': start();
break;
case 'n':
case 'N': break;
default: cout<<endl<<"Huh? Do you wanna play Blackjack? y = yes, n = no"<<endl<<">";
cin>>BJNewgame;
break;
}
}
cout<<"Have a nice day!"<<endl;
return 0;
}
#endif
-
-
Re: bool functions
Quote:
Originally posted by sentienttoaster
Code:
//open code tag here!!!
bool showcomp(char isgameover);
void showcomphand(bool isgameover)
{
cout<<"My hand is: ";
if(!isgameover)
{
cout<<hiddencard<<" "<<endl;
}
for( int i = !isgameover; comphand[i] !=0; i++)
{
switch (comphand[i])
{
case 1: cout<<"A ";
break;
case 11: cout<<"J ";
break;
case 12: cout<<"Q ";
break;
case 13: cout<<"K ";
break;
default: cout<<comphand[i]<<" "<<endl;
break;
}
}
cout<<endl;
}
//close code tag here!!!