Friday, March 16, 2012

4) Card Shuffling and Dealing: Five-card poker Hand Dealer's Simulation

Simulate the dealer in five-card poker hand.
The dealer's five-card hand is dealt "face down" so the player can't see it.The program will evaluate the dealer's hand, and based on the quality of the hand, the dealer will automatically draw one, two, or three more cards to replace the corresponding number of unneeded cards in the original hands.Then the program will re-evaluate the dealer's hand.
/*
 *  Pointers 714 : Simulate Dealers Hand
 *
 *  Created on: Feb 28, 2012
 *      Author: aeriqusyairi
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
 
//main functions
void shuffle( int wDeck[][ 13 ] ); 
void deal( const int wDeck[][ 13 ], const char *wStoreFace[], const char *wStoreSuit[] );
void initialDisplay( const char *wPFace[], const char *wPSuit[], const char *wDFace[], const char *wDSuit[] );

int main(){
   //initialize suit array
   const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
    
   //initalize 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 };
    
   //seed random number generator
   srand(time(0));
    
   shuffle( deck );//shuffle the deck
   deal( deck, face, suit );//deal two five-hand card and evaluate
    
   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 *wStoreFace[], const char *wStoreSuit[] ){
   //operational functions
   void aPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] );
   void twoPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] );
   void threeKind( const char *wfFace[], const char *wPDSuit[], int result[], int wPair[] );
   void fourKind( const char *wfFace[], const char *wPDSuit[], int result[], int wPair[] );
   void flush( const char *wPDFace[], const char *wPDSuit[], int result[] );
   void straight( const char *wPDFace[], const char *wPDSuit[], const char *wStoreFace[], int result[] );
   
   //sub-operational functions
   void draw( const int wDeck[][ 13 ], char *wDFace[], char *wDSuit[], const char *wStoreFace[], const char *wStoreSuit[], int wPair[] );
   void evalDisplay( const char *wPDFace[], const char *wPDSuit[], const int wPair[], const int wType );
   void final( const int wPResult[], const int wDResult[] );
   
   //variable
	int card, row, column, i, j = 0;
	int pair[ 5 ] = { 0 };
	
	//storage for two five-hand card
	//result boolean: 0:pair 1:two pair 2:three of a kind 3:four of a kind 4:flush 5:straight
	
	//player
        char *PSuit[ 5 ] ;//= { "Hearts", "Hearts", "Hearts", "Hearts", "Hearts" };
        char *PFace[ 5 ] ;//= { "Ace", "Deuce", "Seven", "Three", "Four" };
        int PResult[ 6 ] = { 0 };
        //dealer
        char *DSuit[ 5 ] ;//= { "Spades", "Hearts", "Hearts", "Hearts", "Hearts" };
        char *DFace[ 5 ] ;//= { "Ace", "Ace", "Three", "Six", "Five" };
        int DResult[ 6 ] = { 0 };

	//deal 5-card poker hand 
	for(card = 1, i = 0; card <= 10; card++, i++){
		for(row = 0; row < 4; row++){
			for(column = 0; column < 13; column++){
				if(wDeck[ row ][ column ] == card && i < 5){
               PSuit[ i ] = wStoreSuit[ row ];
               PFace[ i ] = wStoreFace[ column ];
	         }else if(wDeck[ row ][ column ] == card && i > 4){
               DSuit[ j ] = wStoreSuit[ row ];
               DFace[ j ] = wStoreFace[ column ];
               j++;      
            }
			}
		}
	}

	//display two five-card poker hand
	initialDisplay( PFace, PSuit, DFace, DSuit );
	
	//dealer modification
	//debug
	//printf("\nDealer modify\n***********\n\n");
	
	straight( DFace, DSuit, wStoreFace, DResult ); 
        //printf("\n  DResult[ 5 ] = %d\n", DResult[ 5 ] );
        if(DResult[ 5 ] == 0){
           flush( DFace, DSuit, DResult );
	   //printf("\n  DResult[ 4 ] = %d\n", DResult[ 4 ] );
	   if(DResult[ 4 ] == 0){
               fourKind( DFace, DSuit, DResult, pair );
	      //printf("\n  DResult[ 3 ] = %d\n", DResult[ 3 ] );
	      if(DResult[ 3 ] == 0){
                 threeKind( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 2 ] = %d\n", DResult[ 2 ] );
	         if(DResult[ 2 ] == 0){
                 twoPair( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 1 ] = %d\n", DResult[ 1 ] );
	            if(DResult[ 1 ] == 0){
                       aPair( DFace, DSuit, DResult, pair );
	               //printf("\n  DResult[ 0 ] = %d\n", DResult[ 0 ] );
	               if(DResult[ 0 ] == 0){
                          //debug
                          //printf("Very weak!\n");
                     
                          draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair ); 
                       }else if(DResult[ 0 ] == 1){
                          draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );         
                       }
                    }else if(DResult[ 1 ] == 1){
                        draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );        
                    }
                }else if(DResult[ 2 ] == 1){
                   draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );        
                }
            }else if(DResult[ 3 ] == 1){
               draw( wDeck, DFace, DSuit, wStoreFace, wStoreSuit, pair );       
            }
         }
       }
	
	//evaluate
	printf("\n******************\nEvaluate both hand\n******************\n\nPlayer hand\n**********\n\n");
	straight( PFace, PSuit, wStoreFace, PResult ); 
        //printf("\n  PResult[ 5 ] = %d\n", PResult[ 5 ] );
        if(PResult[ 5 ] == 0){
           flush( PFace, PSuit, PResult );
	   //printf("\n  PResult[ 4 ] = %d\n", PResult[ 4 ] );
	   if(PResult[ 4 ] == 0){
	      fourKind( PFace, PSuit, PResult, pair );
	      //printf("\n  PResult[ 3 ] = %d\n", PResult[ 3 ] );
	      if(PResult[ 3 ] == 0){
	         threeKind( PFace, PSuit, PResult, pair );
	         //printf("\n  PResult[ 2 ] = %d\n", PResult[ 2 ] );
	         if(PResult[ 2 ] == 0){
	            twoPair( PFace, PSuit, PResult, pair );
	            //printf("\n  PResult[ 1 ] = %d\n", PResult[ 1 ] );
	            if(PResult[ 1 ] == 0){
	               aPair( PFace, PSuit, PResult, pair );
	               //printf("\n  PResult[ 0 ] = %d\n", PResult[ 0 ] );
	               if(PResult[ 0 ] == 0){
                     printf("Very weak!\n");
                  }else if(PResult[ 0 ] == 1){
                     evalDisplay( PFace, PSuit, pair, 0 );         
                  }
               }else if(PResult[ 1 ] == 1){
                  evalDisplay( PFace, PSuit, pair, 1 );        
               }
            }else if(PResult[ 2 ] == 1){
               evalDisplay( PFace, PSuit, pair, 2 );        
            }
         }else if(PResult[ 3 ] == 1){
            evalDisplay( PFace, PSuit, pair, 3 );        
         }
      }
   }
	
	printf("\nDealer hand\n***********\n\n");
	straight( DFace, DSuit, wStoreFace, DResult ); 
        //printf("\n  DResult[ 5 ] = %d\n", DResult[ 5 ] );
        if(DResult[ 5 ] == 0){
           flush( DFace, DSuit, DResult );
	   //printf("\n  DResult[ 4 ] = %d\n", DResult[ 4 ] );
	   if(DResult[ 4 ] == 0){
              fourKind( DFace, DSuit, DResult, pair );
	      //printf("\n  DResult[ 3 ] = %d\n", DResult[ 3 ] );
	      if(DResult[ 3 ] == 0){
                 threeKind( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 2 ] = %d\n", DResult[ 2 ] );
	         if(DResult[ 2 ] == 0){
                 twoPair( DFace, DSuit, DResult, pair );
	         //printf("\n  DResult[ 1 ] = %d\n", DResult[ 1 ] );
	            if(DResult[ 1 ] == 0){
                       aPair( DFace, DSuit, DResult, pair );
	               //printf("\n  DResult[ 0 ] = %d\n", DResult[ 0 ] );
	               if(DResult[ 0 ] == 0){
                     printf("Very weak!\n");
                  }else if(DResult[ 0 ] == 1){
                     evalDisplay( DFace, DSuit, pair, 0 );         
                  }
               }else if(DResult[ 1 ] == 1){
                  evalDisplay( DFace, DSuit, pair, 1 );        
               }
            }else if(DResult[ 2 ] == 1){
               evalDisplay( DFace, DSuit, pair, 2 );        
            }
         }else if(DResult[ 3 ] == 1){
            evalDisplay( DFace, DSuit, pair, 3 );        
         }
      }
   }
	
	//result
	final( PResult, DResult );
	
}

void draw( const int wDeck[][ 13 ], char *wDFace[], char *wDSuit[], const char *wStoreFace[], const char *wStoreSuit[], int wPair[] ){
   int i, j, zero = 0, card, row, column;
   
   //find how many zero
   for(i = 0 ; i < 5 ; i++){
      if(wPair[ i ] == 0)
         zero++;         
   }    
   if(zero == 5){
      zero = 2;
   }else{
      zero -= 1;
   }
   
   //debug
   //printf("zero = %d\n", zero); 
    
   
   //draw new card respect to number of zero
   for(card = 11; card <= (11 + zero); card++){
		for(row = 0; row < 4; row++){
			for(column = 0; column < 13; column++){
				if(wDeck[ row ][ column ] == card){
               //printf("%5d\n", card);             
               for(j = 0; j < 5; j++){
                  if(wPair[ j ] == 0){
                     wDSuit[ j ] = wStoreSuit[ row ];
                     wDFace[ j ] = wStoreFace[ column ];
                     wPair[ j ] = 1;
                     break;
                  }
               }
	         }
			}
		}
	}
	
	//debug
	/*for(i = 0; i < 5; i++){
		printf("%10s of %-8s\n", wDFace[ i ], wDSuit[ i ] );
	}*/
}

void initialDisplay( const char *wPFace[], const char *wPSuit[], const char *wDFace[], const char *wDSuit[] ){
   int i;
   
   printf("*************************\nYour five-card poker hand\n*************************\n\nPlayer hand %28s\n********** %28s\n\n", "Dealer hand", "***********");
	for(i = 0; i < 5; i++){
		printf("%10s of %-8s %10s of %-8s\n", wPFace[ i ], wPSuit[ i ], "*****", "*****" );
	}  
}

void evalDisplay( const char *wPDFace[], const char *wPDSuit[], const int wPair[], const int wType ){
   int card;
   char *type[ 4 ] = { "Pair", "Two-pair", "Three of a kind", "Four of a kind" };
   
   printf("\n%s...\n", type[ wType ] );
   for(card = 0; card < 5; card++){
      if(wPair[ card ] != 0){
         printf("\n%7s of %-9s\n", wPDFace[ card ], wPDSuit[ card ] );
      }
   }             
}

void aPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;

   //compute whether the five-hand card contains a pair
   for(card = 0; card < 5; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            j++;
            wPair[ card ] = 1;
            wPair[ i ] = 1;
  	 	   }
 	   }
   }
    
   //assign result
   if(j == 1){
      result[ 0 ] = 1;
   }    
}

void twoPair( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether the five-hand card contains two pair
   for(card = 0; card < 5; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            j++;
            wPair[ card ] = 1;
            wPair[ i ] = 1;
 	 	   }  
      }
   }
    
   //assign result
   if(j == 2){
      result[ 1 ] = 1;
   }  
}

void threeKind( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, j = 0, k, x;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether the five-hand card contains three of a kind
   for(card = 0; card < 3; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){
            for(x = i + 1; x < 5; x++){
               if(wPDFace[ i ] == wPDFace[ x ]){
                  j++;
                  wPair[ card ] = 1;
                  wPair[ i ] = 1;
                  wPair[ x ] = 1;          
               }      
            }
    	   }  
      }
   }
    
   //assign result
   if(j == 1){
      result[ 2 ] = 1;
   }          
}

void fourKind( const char *wPDFace[], const char *wPDSuit[], int result[], int wPair[] ){
   int card, i, indi = 0;
   
   //initalize wPair
   for(i = 0; i < 5; i++)
      wPair[ i ] = 0;
    
   //compute whether your five-hand card contains four-of-a-kind
   for(card = 0; card < 2; card++){
      for(i = card + 1; i < 5; i++){
         if(wPDFace[ card ] == wPDFace[ i ]){  
            indi++;
            if(indi == 1)
               wPair[ card ] = 1;
            wPair[ i ] = 1;    
             
         }
      }         
      if(indi == 3)
         break;
      else if(indi > 3)
         break;
      else{
         indi = 0;
         for(i = 0; i < 5; i++)
            wPair[ i ] = 0;
      }
          
   }     
    
   //assign result
   if(indi == 3){
      result[ 3 ] = 1;
   } 
}

void flush( const char *wPDFace[], const char *wPDSuit[], int result[] ){
   int i, j = 0;
   
   //compute whether your five-hand card contains a flush
   for(i = 1; i < 5; i++){
      if(wPDSuit[ 0 ] == wPDSuit[ i ] )
         j++;
   }
   
   //display result
   if(j == 4){
      result[ 4 ] = 1;
      printf("\nFlush...\n");
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wPDFace[ i ], wPDSuit[ i ] );
      }
   }   
}

void straight( const char *wPDFace[], const char *wPDSuit[], const char *wStoreFace[], int result[] ){
   int i, j, k, pass, count, hold, check = 1;
   int faceValue[ 5 ] = { 0 };
   
   //locate face value and store in an array
   for(i = 0 ; i < 5; i++){
      for(j = 0 ; j < 13; j++){
         if(wPDFace[ i ] == wStoreFace[ 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
   if(check == 1){
      result[ 5 ] = 1;
      printf("\nStraight...\n");
      for(i = 0; i < 5; i++){
         printf("\n  %5s of %-8s\n", wPDFace[ i ], wPDSuit[ i ] );
      }
   }    
}

void final( const int wPResult[], const int wDResult[] ){
   int i, x = 0, y = 0;
   
   printf("\n******\nResult\n******\n\n");
   
   //evaluate both hand
   for(i = 0; i < 6; i++){
      if(wPResult[ i ] == 1)
         x = i + 1;
      if(wDResult[ i ] == 1)
         y = i + 1;      
   }
   
   //debug
   //printf("x = %d  y = %5d\n\n", x, y);
   
   if(x > y)
      printf("Players win!\n\n");
   else if(y > x)
      printf("Dealers win!\n\n");
   else if(x == y)
      printf("Its a tie!\n\n");
   else
      printf("Unable to evaluate!\n\n");
}

No comments:

Post a Comment