Skip to main content



PROGRAMMING WITH C


7.12     (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing
function deals a five-card poker hand. Then write the following additional functions:
a) Determine whether the hand contains a pair.
b) Determine whether the hand contains two pairs.
c) Determine whether the hand contains three of a kind (e.g., three jacks).
d) Determine whether the hand contains four of a kind (e.g., four aces).
e) Determine whether the hand contains a flush (i.e., all five cards of the same suit).
f) Determine whether the hand contains a straight (i.e., five cards of consecutive face values)



ANSWER
7.12
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

//main functions
void shuffle( int wDeck[][ 13 ] );
void deal( const int wDeck[][ 13 ], const char *wface[], const char *wSuit[], char *wfSuit[], char *wfFace[] );

//operation functions
void pair( const char *wfSuit[], const char *wfFace[]);
void twoPair( const char *wfSuit[], const char *wfFace[] );
void threeKind( const char *wfSuit[], const char *wfFace[] );
void fourKind( const char *wfSuit[], const char *wfFace[] );
void flush( const char *wfSuit[], const char *wfFace[] );
void straight( const char *wfSuit[], const char *wfFace[], const char *wFace[] );

//sub-operation functions
void result( const int compare, const int compareTo, const char *wfFace[], const char *wfSuit[], const char statement[] );

int main(void){
 //initilize 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 suit and face array for storing five-hand card
 char *fSuit[ 5 ] ;// Hearts
  char *fFace[ 5 ] ;// Seven, Ace, Ace, Four, Five

 //initialize deck array
 int deck[ 4 ][ 13 ] = { 0 };

 //seed random number generator
 srand( time( 0 ) );

 shuffle( deck );//shuffle the deck
 deal( deck, face, suit, fSuit, fFace );//deal five-hand card
 pair( fSuit, fFace );//compute for pair
 twoPair( fSuit, fFace );//compute for two-pair
 threeKind( fSuit, fFace );//compute for three of a kind
 fourKind( fSuit, fFace );//compute for four of the kind
 flush( fSuit, fFace );//compute for flush
 straight( fSuit, fFace, face );//compute for straight

   system("pause");
 return 0;
}

void shuffle( int wDeck[][ 13 ] ){
 int row, column, card;

 for(card = 1; card <= 52; card++){
  do{
   row = rand() % 4;
   column = rand() % 13;
  }
  while(wDeck[ row ][ column ] != 0);

  wDeck[ row ][ column ] = card;
 }
}

void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[], char *wfSuit[], char *wfFace[] ){
 int card, row, column, i;

 //deal 5-card poker hand
 for(card = 1, i = 0; card <= 5; card++, i++){
  for(row = 0; row < 4; row++){
   for(column = 0; column < 13; column++){
    if(wDeck[ row ][ column ] == card){
               wfSuit[ i ] = wSuit[ row ];
               wfFace[ i ] = wFace[ column ];
          }
   }
  }
 }

 //display 5-card poker hand
 printf("Your five-card poker hand...\n\n");
 for(i = 0; i < 5; i++){
  printf("%10s of %-8s\n", wfFace[ i ], wfSuit[ i ]);
 }
}

void pair(const char *wfSuit[], const char *wfFace[]){
    int card, i, j = 0, k;
    int pair[ 5 ] = { 0 };

    //compute whether the five-hand card contains a pair
    printf("\nPair...\n");
    for(card = 0; card < 5; card++){
       for(i = card + 1; i < 5; i++){
          if(wfFace[ card ] == wfFace[ i ]){
             j++;
             pair[ card ] = i;
        }
      }
    }
    
    //display result
    if(j == 1){
      for(card = 0; card < 5; card++){
         if(pair[ card ] != 0){
            k = pair[ card ];
            printf("\n%10s of %-8s and %5s of %-8s\n", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
         }
      }
   }
   else{
      printf("\n  Your five-hand card doesn't contains any pair.\n");     
   } 
}

void twoPair( const char *wfSuit[], const char *wfFace[] ){
    int card, i, j = 0, k;
    int pair[ 5 ] = { 0 };
    
    //compute whether the five-hand card contains two pair
    printf("\nTwo-pair...\n");
    for(card = 0; card < 5; card++){
        for(i = card + 1; i < 5; i++){
           if(wfFace[ card ] == wfFace[ i ]){
             j++;
             pair[ card ] = i;
         } 
        }
    }
    
    //display result
    if(j == 2){
       for(card = 0; card < 5; card++){
          if(pair[ card ] != 0){
             k = pair[ card ];
             printf("\n%10s of %-8s and %5s of %-8s\n", wfFace[ card ], wfSuit[ card ], wfFace[ k ], wfSuit[ k ] );
          }
       }
    }
    else{
       printf("\n  Your five-hand card doesn't contains two pair.\n");     
    }
}

void threeKind( const char *wfSuit[], const char *wfFace[] ){
    int card, i, j = 0, k, x;
    int pair[ 3 ] = { 0 };
    
    //compute whether the five-hand card contains three of a kind
    printf("\nThree of a kind...\n");
    for(card = 0; card < 3; card++){
        for(i = card + 1; i < 5; i++){
           if(wfFace[ card ] == wfFace[ i ]){
              for(x = i + 1; x < 5; x++){
                 if(wfFace[ i ] == wfFace[ x ]){
                    j++;
                    pair[ 0 ] = card;
                    pair[ 1 ] = i;
                    pair[ 2 ] = x;         
                 }     
              }
         } 
        }
    }
    
    //display result
    if(j == 1){
       for(card = 0; card < 3; card++){
             k = pair[ card ];
             printf("\n%10s of %-8s\n", wfFace[ k ], wfSuit[ k ] );
       }
    }
    else{
       printf("\n  Your five-hand card doesn't contains three of a kind.\n");     
    }     
}

void fourKind( const char *wfSuit[], const char *wfFace[] ){
    int card, i, indi = 0, pair[ 5 ] = { 0 };
    
    //compute whether your five-hand card contains four-of-a-kind
    printf("\nFour of a kind...\n");
    for(card = 0; card < 2; card++){
       for(i = card + 1; i < 5; i++){
          if(wfFace[ card ] == wfFace[ i ]){ 
             indi++;
             if(indi == 1)
                pair[ i - 1 ] = 1;
             pair[ i ] = 1;   
             
          }
       }        
       if(indi == 3)
          break;
       else if(indi > 3)
          break;
       else{
          indi = 0;
          for(i = 0; i < 5; i++)
             pair[ i ] = 0;
       }
          
    }    
    
    //display result
    if(indi == 3){
       for(i = 0; i < 5; i++){
         if( pair[ i ] == 1)
            printf("\n%5s of %-9s\n", wfFace[ i ], wfSuit[ i ] );
       }     
       printf("\n");
    }else{
       printf("\n  Your five-hand card doesn't contains four-of-a-kind.\n");     
    }
}

void flush ( const char *wfSuit[], const char *wfFace[] ){
   int i, j = 0;
   
   //compute whether your five-hand card contains a flush
   printf("\nFlush...\n");
   for(i = 1; i < 5; i++){
      if(wfSuit[ 0 ] == wfSuit[ i ] )
         j++;
   }
   
   //display result
   result( j, 0, wfFace, wfSuit, "\n  Not flush!\n" );
}

void straight( const char *wfSuit[], const char *wfFace[], const char *wFace[] ){
   int i, j, k, pass, count, hold, check = 1;
   int faceValue[ 5 ] = { 0 };
   
   printf("\nStraight...\n");
   //locate face value and store in an array
   for(i = 0 ; i < 5; i++){
      for(j = 0 ; j < 13; j++){
         if(wfFace[ i ] == wFace[ j ]){
            faceValue[ i ] = j;          
         }     
      }
   }
   
   //sort face value in ascending order using bubble sort
   for(pass = 0; pass < 4; pass++){
      for(count = 0; count < 4; count++){
         if(faceValue[ count ] > faceValue[ count + 1 ]){
            //swap
            hold = faceValue[ count ];
            faceValue[ count ] = faceValue[ count + 1 ];
            faceValue[ count + 1 ] = hold;              
         }         
      }        
   }
   
   //check if the hand contains a straight
   for(i = 0; i < 4; i++){
      if(faceValue[ i ] + 1 != faceValue[ i + 1 ]){
         check = 0;             
      }     
   }
   
   //display result
   result( check, 1, wfFace, wfSuit, "\n  Not a straight hand.\n" );
      
   printf("\n");
}

//sub-operation functions
void result( const int compare, const int compareTo, const char *wfFace[], const char *wfSuit[], const char statement[] ){
   int i;
   
   if(compare == compareTo){
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wfFace[ i ], wfSuit[ i ] );
      }
   }
   else{
      printf("%s", statement );  
   }
}

 Contact Adda Samuel

Comments

Popular posts from this blog

Hei guys ! Watch out for more on c plus plus

HOW TO MOVE TO CANADA EASILY WITHOUT HUSTLE

Moving to Canada is a significant decision, and the process can be complex. Here is a general guide to help you get started on your journey to move to Canada:   1. Determine Your Eligibility: Canada has several immigration programs, each with its own eligibility requirements. Determine which program is most suitable for you. Common options include Express Entry, Provincial Nominee Programs (PNPs), family sponsorship, and study/work permits.   2. Research Canada's Provinces and Territories: Canada is a vast country with diverse regions. Research the provinces and territories to find the one that best suits your needs, taking into account factors like job opportunities, climate, and lifestyle.  3. Gather Required Documents: Depending on the immigration program you choose, you will need specific documents such as passports, language proficiency tests (e.g., IELTS for English, TEF for French), educational credentials, and work experience letters.  4. Create an Exp...