#include #include #include #define DECK 52 #define SET 13 #define PLAY 2 /* structure to hold sequence of cards */ typedef struct _card { int type; /* 0: Spade, 1: Heart, 2: Diamond, 3: Clubs */ int val; /* 0: Ace, 1-9, 10: Jack, 11: Queen, K: King */ struct _card *next; } card; /* print the card types: S for Spade, H for Hearts, D for Diamond, and C for Clubs */ void printType ( int t ) { switch ( t ) { case 0 : printf("S"); break; case 1 : printf("H"); break; case 2 : printf("D"); break; case 3 : printf("C"); break; default: printf("X"); break; /* wrong entry */ } } /* print the card values: A for Ace, 1-9, J for Jack, Q for Queen, and K for King */ void printVal ( int v ) { if ( v == 0 ) printf("A"); else if ( v == 10 ) printf("J"); else if ( v == 11 ) printf("Q"); else if ( v == 12 ) printf("K"); else if (v > 12 ) printf("Y"); /* wrong entry */ else printf("%d", v+1); } /* print one card */ void printCard ( card *c ) { printType(c->type); printVal(c->val); } /* print sequence of cards in a hand */ void printDeck ( card *head ) { card *c; if ( head == NULL ) printf(" -> EMPTY"); else { for ( c = head; c != NULL; c = c->next ) { printf(" -> "); printCard(c); } } printf("\n"); } /* create a linked list node of a card */ card *createCard ( int t, int v ) { card *c = (card *)malloc(sizeof(card)); if ( c == NULL ) printf("Malloc Error!\n"); else { c->type = t; c->val = v; c->next = NULL; } return c; } /* delete the front node and return it */ card *deleteFront ( card **phead ) { card *c = *phead; if ( *phead != NULL ) { *phead = (*phead)->next; c->next = NULL; } return c; } /* insert new node after the cur node */ void insertAfter ( card *cur, card *new ) { if ( (cur != NULL) && (new != NULL) ) { new->next = cur->next; cur->next = new; } } /* append the whole new list after the cur list */ void appendAfter ( card **pCurHead, card **pCurTail, card *newHead, card *newTail ) { if ( *pCurHead == NULL ) { *pCurHead = newHead; *pCurTail = newTail; } else { (*pCurTail)->next = newHead; *pCurTail = newTail; } } /* swap a pair of variable values using call-by-reference */ void swap ( int *x, int *y ) { int t = *x; *x = *y; *y = t; } /* random generation and arrangement of 52 shuffled numbers */ void shuffleDeck ( int *D ) { int i; for ( i=0; inext; } /* generate hand for Player-B */ Pt[1] = Ph[1] = createCard(D[i]/SET, D[i]%SET); for ( i = i+1; i < DECK; ++i ) { insertAfter(Pt[1], createCard(D[i]/SET, D[i]%SET)); Pt[1] = Pt[1]->next; } } /* print two player hands containing cards */ void printHands ( card *P[PLAY] ) { printf("[PLAYER-A]"); printDeck(P[0]); printf("[PLAYER-B]"); printDeck(P[1]); } /* game proceedings routine returning the winner player */ int playGame ( card *Ph[PLAY], card *Pt[PLAY] ) { int turn = 0; /* keep tracks of move for either Player-A (turn=0) or Player-B (turn=1) */ card *draw, *playHead, *playTail; int count=0; /* counter for number of drawn games */ do { /* starting move of the draw */ printf("\n-- Player-%c Starts --\n", 'A'+turn); playHead = deleteFront(&Ph[turn]); playTail = playHead; turn = !turn; /* next player turn generation */ if ( playHead == NULL ) break; do{ /* turn-by-turn player moves and maintain list of drawn/played cards */ draw = deleteFront(&Ph[turn]); if ( draw == NULL ) break; /* player hand empty */ insertAfter(playTail, draw); if ( playTail->type == draw->type ) { /* card color match */ playTail = playTail->next; break; } playTail = playTail->next; turn = !turn; /* round robin play generation */ } while ( 1 ); printf("** [Draw #%d]", ++count); printDeck(playHead); /* print the drawn cards */ if ( draw == NULL ) { /* empty hand player loses */ printf("-- Player-%c Empty --\n\n", 'A'+turn); turn = !turn; /* prepare for winner player */ } else { /* appending the drawn card set behind running game-winner hand */ printf("-- Player-%c Conquers --\n\n", 'A'+turn); appendAfter(&Ph[turn], &Pt[turn], playHead, playTail); } printHands(Ph); } while ( (Ph[0] != NULL) && (Ph[1] != NULL) ); /* check for empty hands */ return turn; /* winner player */ } int main () { card *phead[PLAY], *ptail[PLAY]; int deck[DECK], decide; srand((unsigned int)time(NULL)); shuffleDeck(deck); generateHands(phead, ptail, deck); printHands(phead); decide = playGame(phead, ptail); printf("\n++ Player-%c Wins Match ++\n", 'A'+decide); return 0; }