el bob
05-14-01, 06:05 PM
Here is my almost finished Blackjack program:
---------------------------------------------
#include<iostream.h>
#include<stdlib.h>
/*typedef int bool;
const int false=0;
const int true=1*/;
const int decksize=52;
const int maxhand=5;
struct card {
char value;
char suit;
bool used;
};
void dealerplay(card deckd[]);
void userplay(card decku[]);
void handhit(card hand[], int &cd, int &total);
int main ()
{
char choice=' ';
do
{
card deck[decksize];
for (int i=0; i<decksize; i++)
{
deck[i].used=false;
if ((i>=0) && (i<=12))
deck[i].suit='s';
else if ((i>=13) && (i<=25))
deck[i].suit='d';
else if ((i>=26) && (i<=38))
deck[i].suit='c';
else if ((i>=39) && (i<=51))
deck[i].suit='h';
if (i==0)
{
for (int a=0; a<=7; a++)
deck[a].value=(a+48)+2;
deck[i+8].value='t';
deck[i+9].value='j';
deck[i+10].value='q';
deck[i+11].value='k';
deck[i+12].value='a';
}
if (i==13)
{
for (int x=13; x<=20; x++)
deck[x].value=(x+48)-11;
deck[i+8].value='t';
deck[i+9].value='j';
deck[i+10].value='q';
deck[i+11].value='k';
deck[i+12].value='a';
}
if (i==26)
{
for (int z=26; z<=33; z++)
deck[z].value=(z+48)-24;
deck[i+8].value='t';
deck[i+9].value='j';
deck[i+10].value='q';
deck[i+11].value='k';
deck[i+12].value='a';
}
if (i==39)
{
for (int c=39; c<=51; c++)
deck[c].value=(c+48)-37;
deck[i+8].value='t';
deck[i+9].value='j';
deck[i+10].value='q';
deck[i+11].value='k';
deck[i+12].value='a';
}
}
dealerplay(deck);
//Print out the deck
//for (int p=0; p<decksize; p++)
//cout<<"\""<<deck[p].value<<"\""<<deck[p].suit<<endl;
userplay(deck);
cout<<endl<<endl;
cout<<"Would you like to play again? Type y for yes or anything else for no."<<endl;
cin>>choice;
}while(choice=='y');
return 0;
}
void dealerplay(card deckd[])
{
card dealerhand[maxhand];
int rcard2=0, totald=0;
//Fill the dealer's hand (5 cards)
for(int v=0; v<=4; v++)
{
do
{
randomize();
rcard2=rand()%52;
}while(deckd[rcard2].used==1);
deckd[rcard2].used=1;
dealerhand[v].value=deckd[rcard2].value;
dealerhand[v].suit=deckd[rcard2].suit;
}
for(int haha=0; haha<=1; haha++)
{
switch(dealerhand[haha].value)
{
case '2':totald+=2;break;
case '3':totald+=3;break;
case '4':totald+=4;break;
case '5':totald+=5;break;
case '6':totald+=6;break;
case '7':totald+=7;break;
case '8':totald+=8;break;
case '9':totald+=9;break;
case 't':totald+=10;break;
case 'j':totald+=10;break;
case 'q':totald+=10;break;
case 'k':totald+=10;break;
case 'a':totald+=11;break;
default:cout<<"Not a real card.\n";break;
}
}
//Print out all parameters of one random card
cout<<"\n\n-------------------------------\n\n";
cout<<deckd[rcard2].value<<" "<<deckd[rcard2].suit<<" "<<deckd[rcard2].used;
cout<<"\n\n-------------------------------\n\n";
return;
}
void userplay(card decku[])
{
card userhand[maxhand];
int rcard3=0, totalu=0, cdu=0;
char hsu=' ';
bool bu;
//Fill the user's hand (5 cards)
for(int o=0; o<=4; o++)
{
do
{
randomize();
rcard3=rand()%52;
}while(decku[rcard3].used==1);
decku[rcard3].used=1;
userhand[o].value=decku[rcard3].value;
userhand[o].suit=decku[rcard3].suit;
}
cdu+=2;
for(int haLA=0; haLA<=1; haLA++)
{
switch(userhand[haLA].value)
{
case '2':totalu+=2;break;
case '3':totalu+=3;break;
case '4':totalu+=4;break;
case '5':totalu+=5;break;
case '6':totalu+=6;break;
case '7':totalu+=7;break;
case '8':totalu+=8;break;
case '9':totalu+=9;break;
case 't':totalu+=10;break;
case 'j':totalu+=10;break;
case 'q':totalu+=10;break;
case 'k':totalu+=10;break;
case 'a':totalu+=11;break;
default:cout<<"Not a real card.\n";break;
}
}
cout<<endl;
for(int lad=0; lad<=1; lad++)
cout<<"Your "<<(lad+1)<<" card is a "<<userhand[lad].value<<" of "<<userhand[lad].suit<<"."<<endl;
cout<<"Total: "<<totalu<<endl;
do
{
cout<<"Would you like to hit (h) or stay (s)?"<<endl;
cin>>hsu;
if(hsu=='h')
{
handhit(userhand, cdu, totalu);
cout<<"Your "<<cdu<<" card is a "<<userhand[cdu].value<<" of "<<userhand[cdu].suit<<"."<<endl;
if(totalu>21)
{
bu=true;
cout<<"You busted."<<endl;
}
else if(hsu=='s')
break;
else
{
cout<<"Invalid input. Please enter an h or an s."<<endl;
continue;
}
}while(cdu<=4 && bu==false);
cout<<"Total: "<<totalu<<endl;
return;
}
void handhit(card hand[], int &cd, int &total)
{
switch(hand[cd].value)
{
case '2':total+=2;break;
case '3':total+=3;break;
case '4':total+=4;break;
case '5':total+=5;break;
case '6':total+=6;break;
case '7':total+=7;break;
case '8':total+=8;break;
case '9':total+=9;break;
case 't':total+=10;break;
case 'j':total+=10;break;
case 'q':total+=10;break;
case 'k':total+=10;break;
case 'a':total+=11;break;
default:cout<<"Not a real card.\n";break;
}
cd++;
}
---------------------------------------------
Here is the error I get.
---------------------------------------------
C:\My Documents\C++\Blackjack\Blkjck.cpp(103) : error C2065: 'randomize' : undeclared identifier
---------------------------------------------
The same randomize code works on the compiler at my school, but doesn't seem to want to work at home on my copy of MS Visual C++. What's going on here?
Thanks. -el bob
---------------------------------------------
#include<iostream.h>
#include<stdlib.h>
/*typedef int bool;
const int false=0;
const int true=1*/;
const int decksize=52;
const int maxhand=5;
struct card {
char value;
char suit;
bool used;
};
void dealerplay(card deckd[]);
void userplay(card decku[]);
void handhit(card hand[], int &cd, int &total);
int main ()
{
char choice=' ';
do
{
card deck[decksize];
for (int i=0; i<decksize; i++)
{
deck[i].used=false;
if ((i>=0) && (i<=12))
deck[i].suit='s';
else if ((i>=13) && (i<=25))
deck[i].suit='d';
else if ((i>=26) && (i<=38))
deck[i].suit='c';
else if ((i>=39) && (i<=51))
deck[i].suit='h';
if (i==0)
{
for (int a=0; a<=7; a++)
deck[a].value=(a+48)+2;
deck[i+8].value='t';
deck[i+9].value='j';
deck[i+10].value='q';
deck[i+11].value='k';
deck[i+12].value='a';
}
if (i==13)
{
for (int x=13; x<=20; x++)
deck[x].value=(x+48)-11;
deck[i+8].value='t';
deck[i+9].value='j';
deck[i+10].value='q';
deck[i+11].value='k';
deck[i+12].value='a';
}
if (i==26)
{
for (int z=26; z<=33; z++)
deck[z].value=(z+48)-24;
deck[i+8].value='t';
deck[i+9].value='j';
deck[i+10].value='q';
deck[i+11].value='k';
deck[i+12].value='a';
}
if (i==39)
{
for (int c=39; c<=51; c++)
deck[c].value=(c+48)-37;
deck[i+8].value='t';
deck[i+9].value='j';
deck[i+10].value='q';
deck[i+11].value='k';
deck[i+12].value='a';
}
}
dealerplay(deck);
//Print out the deck
//for (int p=0; p<decksize; p++)
//cout<<"\""<<deck[p].value<<"\""<<deck[p].suit<<endl;
userplay(deck);
cout<<endl<<endl;
cout<<"Would you like to play again? Type y for yes or anything else for no."<<endl;
cin>>choice;
}while(choice=='y');
return 0;
}
void dealerplay(card deckd[])
{
card dealerhand[maxhand];
int rcard2=0, totald=0;
//Fill the dealer's hand (5 cards)
for(int v=0; v<=4; v++)
{
do
{
randomize();
rcard2=rand()%52;
}while(deckd[rcard2].used==1);
deckd[rcard2].used=1;
dealerhand[v].value=deckd[rcard2].value;
dealerhand[v].suit=deckd[rcard2].suit;
}
for(int haha=0; haha<=1; haha++)
{
switch(dealerhand[haha].value)
{
case '2':totald+=2;break;
case '3':totald+=3;break;
case '4':totald+=4;break;
case '5':totald+=5;break;
case '6':totald+=6;break;
case '7':totald+=7;break;
case '8':totald+=8;break;
case '9':totald+=9;break;
case 't':totald+=10;break;
case 'j':totald+=10;break;
case 'q':totald+=10;break;
case 'k':totald+=10;break;
case 'a':totald+=11;break;
default:cout<<"Not a real card.\n";break;
}
}
//Print out all parameters of one random card
cout<<"\n\n-------------------------------\n\n";
cout<<deckd[rcard2].value<<" "<<deckd[rcard2].suit<<" "<<deckd[rcard2].used;
cout<<"\n\n-------------------------------\n\n";
return;
}
void userplay(card decku[])
{
card userhand[maxhand];
int rcard3=0, totalu=0, cdu=0;
char hsu=' ';
bool bu;
//Fill the user's hand (5 cards)
for(int o=0; o<=4; o++)
{
do
{
randomize();
rcard3=rand()%52;
}while(decku[rcard3].used==1);
decku[rcard3].used=1;
userhand[o].value=decku[rcard3].value;
userhand[o].suit=decku[rcard3].suit;
}
cdu+=2;
for(int haLA=0; haLA<=1; haLA++)
{
switch(userhand[haLA].value)
{
case '2':totalu+=2;break;
case '3':totalu+=3;break;
case '4':totalu+=4;break;
case '5':totalu+=5;break;
case '6':totalu+=6;break;
case '7':totalu+=7;break;
case '8':totalu+=8;break;
case '9':totalu+=9;break;
case 't':totalu+=10;break;
case 'j':totalu+=10;break;
case 'q':totalu+=10;break;
case 'k':totalu+=10;break;
case 'a':totalu+=11;break;
default:cout<<"Not a real card.\n";break;
}
}
cout<<endl;
for(int lad=0; lad<=1; lad++)
cout<<"Your "<<(lad+1)<<" card is a "<<userhand[lad].value<<" of "<<userhand[lad].suit<<"."<<endl;
cout<<"Total: "<<totalu<<endl;
do
{
cout<<"Would you like to hit (h) or stay (s)?"<<endl;
cin>>hsu;
if(hsu=='h')
{
handhit(userhand, cdu, totalu);
cout<<"Your "<<cdu<<" card is a "<<userhand[cdu].value<<" of "<<userhand[cdu].suit<<"."<<endl;
if(totalu>21)
{
bu=true;
cout<<"You busted."<<endl;
}
else if(hsu=='s')
break;
else
{
cout<<"Invalid input. Please enter an h or an s."<<endl;
continue;
}
}while(cdu<=4 && bu==false);
cout<<"Total: "<<totalu<<endl;
return;
}
void handhit(card hand[], int &cd, int &total)
{
switch(hand[cd].value)
{
case '2':total+=2;break;
case '3':total+=3;break;
case '4':total+=4;break;
case '5':total+=5;break;
case '6':total+=6;break;
case '7':total+=7;break;
case '8':total+=8;break;
case '9':total+=9;break;
case 't':total+=10;break;
case 'j':total+=10;break;
case 'q':total+=10;break;
case 'k':total+=10;break;
case 'a':total+=11;break;
default:cout<<"Not a real card.\n";break;
}
cd++;
}
---------------------------------------------
Here is the error I get.
---------------------------------------------
C:\My Documents\C++\Blackjack\Blkjck.cpp(103) : error C2065: 'randomize' : undeclared identifier
---------------------------------------------
The same randomize code works on the compiler at my school, but doesn't seem to want to work at home on my copy of MS Visual C++. What's going on here?
Thanks. -el bob