Originally Posted by
laserlight
You seem to have forgotten to finish implementing cmpface.
oops posted old version, this one is the one i wanted to post:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define DECK_SIZE 52
typedef struct
{
char *face, *suit;
}Card;
void fillDeck(Card * const, char *[], char *[]);
void shuffle(Card * const);
void deal(const Card * const);
void qsort(void *base, size_t nitems, size_t size, int (*compr)(const void *, const void*));
//need to adjust the comparator function to sort by faces and if they have same face then they use suit to sort the double faces.
int cmpface(const void *p_a, const void *p_b)
{
char **a = (char **)p_a;
char **b = (char **)p_b;
return strcmp(*a, *b);
}
int main()
{
Card deck[DECK_SIZE];
char *face[] ={"Ace","King","Queen","Jack","Ten","Nine","Eight","Seven","Six","Five","Four","Three","Two"};
char *suit[] ={"Clubs","Diamonds","Hearts", "Spades"};
srand(time(NULL));
fillDeck(deck, face, suit);
shuffle(deck);
qsort(face, sizeof(face)/sizeof(char *), sizeof(char *), cmpface);
deal(deck);
return 0;
}
void fillDeck(Card * const wDeck, char * wFace[], char * wSuit[])
{
int i;
for(i=0; i<=DECK_SIZE-1; i++)
{
wDeck[i].face = wFace[i%13];
wDeck[i].suit = wSuit[i/13];
}
}
void shuffle(Card * const wDeck)
{
int i,j;
Card temp;
for(i=0; i<=DECK_SIZE-1; i++)
{
j=rand()%DECK_SIZE;
temp = wDeck[i];
wDeck[i] = wDeck[j];
wDeck[j] = temp;
}
}
void deal (const Card * const wDeck)
{
int i;
printf("\nHere is a 10-card hand that was dealt randomly by the machine:\n\n");
for(i=0; i<=10; i++)
{
printf("\t%5s of %-8s\n", wDeck[i].face, wDeck[i].suit);
}
putchar('\n');
}