I am currently working on a client/server poker program. This file I have pulled separate for the time being while I play around with it and try to get it to work. This code is made to form the deck of cards, shuffle and print a hand. What I need to do however is use these cards later for comparison, etc. for the actual poker game. Is there a way to access individual cards here so that I may do that? Or is there a way to store individual structure elements into an array? I have been modifying this code now for hours trying to figure it out, unfortunately something is slipping by me I guess. Thanks for any help anyone can provide.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct card {
const char *face;
const char *suit;
}Card;
/* typedef struct card Card; */
typedef struct {
Card myCards[52];
unsigned int topCard;
} Deck;
void fillDeck( Card * const, const char*[], const char*[] );
void shuffle( Card * const );
void initDeck (Deck * const initMe, const char *face[], const char * suit[]);
void Deal(Deck * const dealMe);
char const * getFace (Deck const * const getMyFace);
char const * getSuit (Deck const * const getMySuit);
int main()
{
const char *face[] = { "Ace", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
const char *suit[] = {"Diamonds", "Hearts", "Clubs", "Spades" };
Deck myDeck;
srand(time(NULL ) );
initDeck (&myDeck, face, suit);
Deal(&myDeck);
return 0;
}
void initDeck (Deck * const initMe, const char * face[], const char * suit[])
{
initMe -> topCard = 0;
fillDeck (initMe -> myCards, face, suit);
shuffle (initMe -> myCards);
return;
}
void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[])
{
int i;
for( i = 0; i <= 51; 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<= 51; i++ ) {
j = rand() % 52;
temp =wDeck[ i ];
wDeck[ i ] = wDeck[ j ];
wDeck[ j ] = temp;
}
}
// Takes the top card off the deck and prints it
void Deal(Deck * const dealMe)
{
int i = 0;
do {
printf ("%5s of %-8s\n", getFace (dealMe), getSuit (dealMe));
dealMe -> topCard++;
i++;
} while (i < 6);
return;
}
char const * getFace (Deck const * const getMyFace)
{
return getMyFace->myCards[getMyFace->topCard].face;
}
char const * getSuit (Deck const * const getMySuit)
{
return getMySuit->myCards[getMySuit->topCard].suit;
}