# Thread: Card poker game simulation

1. ## Card poker game simulation

I am trying to make a card game.Aparent, i fill the deck, shuffle it.But am having trouble with sorting it and dealing the hands.let alone counting the pairs and declaring the winner
Pairs are cards with the same value.)e.g Ace of Hearts & Ace of Spades make a pair.
I then count those pairs. hand with highest pairs wins.

My expected results:

Hand 1:
Seven of Diamonds, is Red
Ten of Hearts, is Red
Number of pairs: 0

Hand 2:
Five of Diamonds, is Red
Five of Clubs, is Black
Nine of Diamonds, is Red
Queen of Diamonds, is Red
Number of pairs: 1
Highest pair is: Five

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct card {
const char *face;
const char *suit;
const char *color;
};

typedef struct card Card;
typedef unsigned char pairs;

void fillDeck( Card * const, const char *[], const char *[] ,const char *[]);
void shuffle( Card * const );
void print( const Card * const );
pairs findpairs(card *hand);  /* finds any pairs in a hand */

int main()
{
int hand,cd,winner;
card hands[5][5],handssorted[5][5];
pairs numpairs[5],highest;
Card deck[52];
const char *face[] = { "Ace", "Two", "Three", "Four", "Five","Six", "Seven",
"Eight", "Nine", "Ten","Jack", "Queen", "King"};
const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
const char *color[]= {"Black","Red"};
srand( time( NULL ) );
fillDeck( deck, face, suit, color );
print( deck );
printf("\n ----------------------------------------------------------\n");
shuffle( deck );
print( deck );
int i=0;
for(cd=0;cd<5;cd++)
{
for(hand=0;hand<5;hand++)
{
hands[hand][cd]=deck[i];
i++;
/* deal the hands here */
}
}

for(hand=0;hand<5;hand++)
{
/* sort the hands here */
//qsort(hands[hand],5,sizeof(card),compareface());
numpairs[hand]=findpairs(handssorted[hand]);
printf("Hand %i:\n",hand+1);
/* print the hands here */
/* print the number and value of any pairs here */
}

/* determine the winner and print it */
system("pause");
return 0;

}
//------------------------------------------------------------------------------------------------------
void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[], const char * wColor[])
{
int i;
for ( i = 0; i <= 51; i++ ) {
wDeck[i].face  = wFace[ i % 13 ];
wDeck[i].suit  = wSuit[ i / 13 ];
wDeck[i].color = wColor[i%2];
//    if ()
//		      wDeck[i].suit = wSuit[ i / 13 ];
}
}
//-----------------------------------------------------------------------------------------------------
void shuffle( Card * const wDeck )
{
int i, j;
Card temp;
for ( i = 0; i <= 51; i++ ) {
j = rand() % 52;
temp = wDeck[ i ];
wDeck[ i ] = wDeck[ j ];
wDeck[ j ] = temp;
}
}
//-----------------------------------------------------------------------------------------------------------
void print( const Card * const wDeck )
{
int i;
for ( i = 0; i <= 51; i++ ){
printf( "\t%s\t of \t%-8s is \t%s \n \t", wDeck[i].face,
wDeck[i].suit,wDeck[i].color,
( i + 1 ) % 2 ? '\t' : '\n' );}
}
//------------------------------------------------------------------------------------------------------------
pairs findpairs(card *hand)
{
pairs numpairs=0;
for ( int i = 0; i <= 5; i++ ){
//if (hand[i].face == )

}

return numpairs;

}
//-----------------------------------------------------------------------------
int compareface(const void* c1, const void *c2)
{
/* This function extracts the two cards face values
and returns 1 if cd1 > cd2, 0 if cd1 == cd2, and
-1 otherwise. The weird argument types are for
compatibility with qsort(), the first two lines
decode the arguments back into "card".
*/
card cd1,cd2;

cd1=*((card*) c1);
cd2=*((card*) c2);

//cd1= (cd1&0x3c)>>2;
//cd2= (cd2&0x3c)>>2;

//if(cd1>cd2)
//return 1;
//if(cd1==cd2)
//return 0;

//return -1;
}```

2. ## looking for assistance

Sure I have cross posted my program, due to desparation. After 5 day and cant make it work.and no one was coming to my rescure.\

Apparently the ERROR IS : no match for 'operator&' in 'cd1 & 60'

as I try to compare the faces.

Code:
```int compareface(const void* c1, const void *c2)
{

card cd1,cd2;

cd1=*((card*) c1);
cd2=*((card*) c2);

cd1= (cd1&0x3c)>>2;
cd2= (cd2&0x3c)>>2;

if(cd1>cd2)
return 1;
if(cd1==cd2)
return 0;

return -1;
}```

3. I would have never used the below structure; when I was learning program it would have been beyond me. And, now I would not do it for efficiency reasons. The SQL Coder in me just see the in-efficiency more than the C coder part does.

Tim S.

Code:
```struct card {
const char *face;
const char *suit;
const char *color;
};```
I would use something like below.

Code:
```struct card {
char face_id; // values 1 to 13 1 = Ace 13 = King
char suit_id; //  Values 1 to 4
};```

4. Could that make the program any different in performance.Aparent my problem is to get it runing so that I can deal the card. but am failing to access the members in the structure.

5. IMO do NOT code for bit operations UNLESS you really have to. Intuitive, they are not (unless quite simple), and they can eat up time in debugging, like nobody's business.

I wouldn't use qsort() for sorting a hand of cards, or even the whole deck. Use a simple insertion or substitution sort - fact is, for very small quantities like this, they are significantly faster than Quicksort. That's why we optimize Quicksort with Insertion sort for small sub-array's, for instance.

Here's a simple Substitution sort I have used since T-Rex was tops in the food chain:

Code:
```for(i=0;i<ElementsToBeSorted-1; i++) {
for(j=i+1;j<ElementsToBeSorted; j++) {
if(array[i] > array[j]) {    //reverse > for descending order
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}```
Well, almost!

If you decide to be "clever" with your code, like using bit shifting for a card game, you owe it to yourself to at least make some comments in the code, why this is being done.

Like to help, but your code is too clever for me.

Number based programs will substantially outperform string based programs, when working on the same algorithm (generally speaking).