PDA

View Full Version : using randomize in C++ for dealing cards



el bob
05-14-01, 05: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

newbie69
05-14-01, 08:06 PM
nice code

el bob
05-14-01, 09:04 PM
Uh...hmmmm...thanks

Zero_Satisfaction
05-15-01, 08:59 AM
Put "randomize();" up by your "const int"s.

Orphious
05-16-01, 09:00 PM
Are you trying to use the randomize function to use the random number generator? If so why not just use
#include <time.h>

srand(time(0));//seed the generator, just make sure you dont seed it in a loop as the results may be non-random
then call the rand function
rcard2=rand()%52+1; //you need to add one so the generator doesnt generate 0 to 51. adding one will do between 1 and 52

If memory servers me, if you dont seed the generator (srand()) the sequence would always start with the same number. This would be a problem with your card game. Using the srand() uses the comps internal clock to return a value to the srand function.
The error is due to randomize() isnt a standard functions the the lib's you included and you didnt define it.

Stu
05-17-01, 02:30 AM
el bob:

Seems like you are doing WAY to much work to build and shuffle the deck. The game logic is fairly straight forward, so I'll skip that and go straight to building and shuffling the deck in the sample program below.




#include <iostream>
#include <vector>
#include <utility> // pair template
#include <algorithm> // random_shuffle template
#include <ctime> // C time function

// Class to get the seed
// for the random shuffle..
class TheRandomGenerator {
public:
unsigned long operator() (unsigned long n_ );
};

unsigned long TheRandomGenerator::operator() (unsigned long n_ ){
srand (time(NULL));
return rand() % n_;
}

// Type to hold one [suit, value] pair...
typedef pair< char, char > Card;

int main (int argc, char *argv[]){

// Variables...
vector< char > suits;
vector< char >::iterator suit_iter;
vector< char > values;
vector< char >::iterator value_iter;
vector< Card > deck;
vector< Card >::iterator deck_iter;
Card one_card;
TheRandomGenerator r; // Seeds the random_shuffle

// Initialize the suits...
suits.push_back('s');
suits.push_back('d');
suits.push_back('c');
suits.push_back('h');

// Initialize the values...
values.push_back('1');
values.push_back('2');
values.push_back('3');
values.push_back('4');
values.push_back('5');
values.push_back('6');
values.push_back('7');
values.push_back('8');
values.push_back('9');
values.push_back('t');
values.push_back('j');
values.push_back('q');
values.push_back('k');

// Loop through the values
// and suits to build the deck...
for (suit_iter = suits.begin(); suit_iter < suits.end(); suit_iter++){

for (value_iter = values.begin(); value_iter < values.end(); value_iter++){

one_card.first = *suit_iter;
one_card.second = *value_iter;
deck.push_back( one_card );

} // End for

} // End for

// Print the deck...
for (deck_iter = deck.begin(); deck_iter < deck.end(); deck_iter++){

one_card = *deck_iter;

cout << one_card.first
<< ' '
<< one_card.second
<< '\n'
<< ends;

} // End for

cout << "------------\n" << ends;

// Shuffle the deck...
random_shuffle(deck.begin(), deck.end(), r);

// Print the deck...
for (deck_iter = deck.begin(); deck_iter < deck.end(); deck_iter++){

one_card = *deck_iter;

cout << one_card.first
<< ' '
<< one_card.second
<< '\n'
<< ends;

} // End for*/

cout << "------------\n" << ends;

// Print the first 4 cards; array style...
for (int i = 0; i < 4; i++){

cout << deck[i].first
<< ' '
<< deck[i].second
<< '\n'
<< ends;

} // End for

} // End main()




[ 05-17-2001: Message edited by: Stu ]

Immortal
05-22-01, 09:36 PM
hey guys... can I steal that program and hand it in as mine? :D

puhleaseee...

el bob
05-23-01, 01:20 PM
thanks for the code guys, forgot to check this thread in a while

i have finished the basic black jack program

here is the code: (immortal, take the code if you really want to, but I would suggest writing it yourself - at least leave the deck and write your own userplay/dealerplay)


-----------------------------------------------------------------


//Blackjack program written by R. Grogan

#include<iostream.h>
#include<stdlib.h>
#include<time.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;
};

card dealerhand[maxhand];
card userhand[maxhand];

int totalu=0, totald=0;
bool bu=false, bd=false, gameover=false;

void dealerplay(card deckd[]);
void userplay(card decku[]);
void handhit(card hand[], int &cd, int &total);

int main ()
{
srand(time(0));
char choice=' ';
do
{
totalu=0;
totald=0;
bu=false;
bd=false;
gameover=false;
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';
}
}

//Print out the deck
//for (int p=0; p<decksize; p++)
//cout<<"\""<<deck[p].value<<"\""<<deck[p].suit<<endl;

dealerplay(deck);
userplay(deck);

if(bu==false && gameover==false)
{
cout<<"The dealer totaled "<<totald<<" and ";
if(bd==false && totald>=totalu)
cout<<"won!"<<endl;
else if(bd==true)
cout<<"busted! YOU WIN!!!"<<endl;
else if(bd==false && totald<=totalu)
cout<<"lost! YOU WIN!!!"<<endl;
}
else if(bu==true && gameover==false)
cout<<"The dealer won."<<endl;

cout<<endl<<endl;
cout<<"Would you like to play again? Type y for yes or anything else for no."<<endl;
cin>>choice;
cout<<endl<<endl<<"_-=+++=-_ _-=+++=-_ _-=+++=-_ _-=+++=-_"<<endl<<endl;
}while(choice=='y');

return 0;
}
//--------------------------------------------------------------------------------------------
void dealerplay(card deckd[])
{
int rcard2=0, cdd=0;
//Fill the dealer's hand (5 cards)
for(int v=0; v<=4; v++)
{
do
{
rcard2=rand()%52+1;
}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;
}
}
cdd+=2;
cout<<"The dealer is showing a "<<dealerhand[0].value<<" of "<<dealerhand[0].suit<<"."<<endl;

while(totald<17)
handhit(dealerhand, cdd, totald);

if(totald>21)
bd=true;


//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[])
{
int rcard3=0, cdu=0;
char hsu=' ';
//Fill the user's hand (5 cards)
for(int o=0; o<=4; o++)
{
do
{
rcard3=rand()%52+1;
}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;
if((userhand[0].value=='a' && userhand[1].value=='j') || (userhand[0].value=='j' && userhand[1].value=='a'))
{
gameover=true;
cout<<"You got Blackjack! Congratulations, you win!"<<endl;
cout<<"Total: "<<totalu<<endl;
return;
}
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
<<"Total: "<<totalu<<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<=3 && bu==false);
if(cdu==5 && bu==false)
{
gameover=true;
cout<<"Son of a gun! You got a five card charlie! You win!"<<endl;
}
cout<<"Total: "<<totalu<<endl;
return;
}
//----------------------------------------------------------------------------------------------
void handhit(card hand[], int &cd, int &total)
{
switch(hand[(cd+1)].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++;
return;
}

------------------------------------------------------

If anyone one of you ASCII artists could draw up a card, I could make the text in the middle of the card be the cards value using a character array.

Adios. -el bob

el bob
05-23-01, 01:23 PM
Oh yeah, one more thing. I was using a Borland compiler and it didn't include the BOOL definition file. I used a Microsoft compiler at home and it already had the BOOL definition file and it produced an error when I compiled the program with an integrated BOOL definition.

If you get an error about this, just change it to /*<BOOL stuff>*/ and the compiler won't read the integrated BOOL definition.

-el bob

Immortal
05-24-01, 06:41 AM
hehe.. I'm just kidddin.. I'm making another game thingy... heh

el bob
05-24-01, 04:08 PM
oh man, i've been played :D