#include #include #include #include char * Rank (int c); char * Suit (int c); int Initialize (int *D, int len, int i); int PrintDeck (int *D, int len, int i); int Shuffle (int *D,int len, int i); int main (int argc, char **argv); int main (int argc, char **argv) { int *D; return (srand(time(NULL)),D=malloc(52*sizeof(int)),Initialize (D,52,0),Shuffle(D,52,0),PrintDeck(D,52,0),free (D)); } /* This program is going to shuffle a deck of cards. Imagine a card as an integer between 0 and 51. */ int Initialize (int *D, int len, int i) { /* D is an array of int. len is length of array. i is the position we're initializing now. */ return (len==i?0:(D[i]=i,Initialize(D,len,i+1))); } int PrintDeck (int *D, int len, int i) { /* D is the deck, len is the size, i is the one I'm printing. */ return (len==i?0:(printf ("%s OF %s\n",Rank(D[i]),Suit(D[i])),PrintDeck (D,len,i+1))); } char * Rank (int c) { /* Gets a card number and returns the rank of the card (TWO through ACE) */ int r; return (r=c%13,r==0?"TWO":r==1?"THREE":r==2?"FOUR":r==3?"FIVE":r==4?"SIX":r==5?"SEVEN":r==6?"EIGHT":r==7?"NINE":r==8?"TEN":r==9?"JACK":r==10?"QUEEN":r==11?"KING":"ACE"); } char * Suit (int c) { /* Returns the suit of the card */ int s; return (s=c/13,s==0?"CLUBS":s==1?"DIAMONDS":s==2?"HEARTS":"SPADES"); } int Shuffle (int *D,int len, int i) { /*D is the deck, len is the length, is the card we're moving*/ int r, t; /* rand() returns a random nonnegative int */ return (len==i?0:(r=rand()%(i+1),t=D[r],D[r]=D[i],D[i]=t,Shuffle(D,len,i+1))); }