Code:
/* Fig. 7.24: fig07_ 24. c
Card shuffling dealing program */
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# include <string.h>
/* prototypes */
void shuffle( int wDeck[][ 13 ] );
void deal( const int wDeck[][ 13 ], const char * wFace[], const char * wSuit[], int userSuit[], int userFace[], int compSuit[], int compFace[] );
void user( int userSuit[], int userFace[], int userHand );
void comp( int compSuit[], int compFace[], int compHand );
void compare( int userHand, int compHand, int userTotal, int compTotal );
/* needed for qsort used in hands function */
/********************************** MAIN **********************************/
int main( void )
{
int x = 0;
/* initialize arrays for each hand's suit and face*/
int user_hand = 0;
int user_total = 0;
int user_suit[4] = {0};
int user_face[13] = {0};
int comp_hand = 0;
int comp_total = 0;
int comp_suit[4] = {0};
int comp_face[13] = {0};
/* initialize suit array */
const char *suit[4] = { "Hearts", "Diamonds", "Clubs", "Spades" };
/* initialize face array */
const char *face[13] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
/* initialize deck array */
int deck[ 4 ][ 13 ] = { 0 };
for(x = 1; x <= 10; x++) {
shuffle( deck ); /* shuffle the deck */
deal( deck, face, suit, user_suit, user_face, comp_suit, comp_face); /* deal the deck */
user(user_suit, user_face, user_hand); /* determine the user's hand */
comp(comp_suit, comp_face, comp_hand); /* determine the computer's hand */
compare(user_hand, comp_hand, user_total, comp_total); /* compares the hands and keeps track if the winners */
}
system("pause");
return 0; /* indicates successful termination */
} /* end main */
/********************************** shuffle cards in deck **********************************/
void shuffle( int wDeck[][ 13 ] )
{
int row; /* row number */
int column; /* column number */
int card; /* counter */
/* for each of the cards, choose slot of deck randomly */
for ( card = 1; card <= 52; card++ ) {
/* choose new random location until unoccupied slot found */
do {
row = rand() % 4;
column = rand() % 13;
} while( wDeck[ row ][ column ] != 0); /*end do...while */
/* place card number in chosen slot of deck */
wDeck[ row ][ column ] = card;
} /* end for */
} /* end function shuffle */
/********************************** deal cards in deck **********************************/
void deal( const int wDeck[][ 13 ], const char *wFace[], char *wSuit[], int userSuit[], int userFace[], int compSuit[], int compFace[] )
{
int card; /* card counter */
int row; /* row counter */
int column; /* column counter */
printf("*****Your hand*****\n");
/* deal 5 cards */
for ( card = 1; card <= 10; card++ ) {
if(card == 6) {
printf("\n\n*****Computer's Hand*****\n");
}
/* loop through rows of wDeck */
for ( row = 0; row <= 3; row++ ) {
/* loop through columns of wDeck for current row */
for ( column = 0; column <= 12; column++ ) { \
/* if slot contains current card, display card */
if ( wDeck[ row ][ column ] == card ) {
printf("%5s of %8s\n", wFace[ column ], wSuit[ row ]);
/* store user info */
if(card < 6) {
userSuit[row]++;
userFace[column]++;
}
if(card > 5) {
compSuit[row]++;
compFace[column]++;
}
} /* end if contains current card */
} /* end for columns */
} /* end for rows */
} /* end for dealing */
} /* end function deal */
void user( int userSuit[], int userFace[], int userHand )
{
int i;
int trips = 0, pair = 0, straight = 0;
for(i=0; i<13; i++){ /****** determine 4 of a kind *******/
if(userFace[i] == 4) {
userHand = 6;
printf("\nYou has 4 of a kind\n");
break;
}/* if statement */
}/* 4 of a kind loop */
for(i=0; i<4; i++){ /******* determine flush *******/
if(userSuit[i] == 5) {
userHand = 5;
printf("\nYou has a Flush\n");
break;
}
}/* flush loop */
for(i=0; i<13; i++) { /******* determine straight *******/
if(userFace[i] == 1){
straight++;
if(straight == 5) {
userHand = 4;
printf("\nYou has a Straight \n");
break;
}
}
else
straight = 0;
}
for(i=0; i<13; i++){ /******* determine 3 of a kind *******/
if(userFace[i] == 3) {
userHand = 3;
printf("\nYou has 3 of a kind \n");
break;
}
}/* 3 of a kind loop */
for(i=0; i<13; i++){ /* pair loop */
if(userFace[i] == 2) {
pair++;
}
}/* end of pair loop */
if(pair == 2) { /******* determine 2 pair *******/
userHand = 2;
printf("\nYou has two pair!\n\n");
}
if(pair == 1) { /******* determine pair *******/
userHand = 1;
printf("\nYou has one pair!\n\n");
}
if(pair == 0) {
userHand = 0;
printf("\nYou have high card!\n\n");
}
}
void comp( int compSuit[], int compFace[], int compHand )
{
int i;
int trips = 0, pair = 0, straight = 0;
for(i=0; i<13; i++){ /****** determine 4 of a kind *******/
if(compFace[i] == 4) {
compHand = 6;
printf("\nYou has 4 of a kind\n");
break;
}/* if statement */
}/* 4 of a kind loop */
for(i=0; i<4; i++){ /******* determine flush *******/
if(compSuit[i] == 5) {
compHand = 5;
printf("\nYou has a Flush\n");
break;
}
}/* flush loop */
for(i=0; i<13; i++) { /******* determine straight *******/
if(compFace[i] == 1){
straight++;
if(straight == 5) {
compHand = 4;
printf("\nYou has a Straight \n");
break;
}
}
else
straight = 0;
}
for(i=0; i<13; i++){ /******* determine 3 of a kind *******/
if(compFace[i] == 3) {
compHand = 3;
printf("\nYou has 3 of a kind \n");
break;
}
}/* 3 of a kind loop */
for(i=0; i<13; i++){ /* pair loop */
if(compFace[i] == 2) {
pair++;
}
}/* end of pair loop */
if(pair == 2) { /******* determine 2 pair *******/
compHand = 2;
printf("\nYou has two pair!\n\n");
}
if(pair == 1) { /******* determine pair *******/
compHand = 1;
printf("\nYou has one pair!\n\n");
}
if(pair == 0) {
compHand = 0;
printf("\nYou have high card!\n\n");
}
}
void compare( int userHand, int compHand, int userTotal, int compTotal )
{
if(userHand > compHand) {
userTotal++;
return;
}
if(compHand > userHand) {
compTotal++;
return;
}
}