Thread: Poker

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Poker


    Hi guys im new to the forum and im learning C with the book "C Programming a Modern Approach". In chapter 10 i found this code:

    Code:
    /* Classifies a poker hand */
    
    #include <stdbool.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_RANKS 13
    #define NUM_SUITS 4
    #define NUM_CARDS 5
    
    int num_in_rank[NUM_RANKS];
    int num_in_suit[NUM_SUITS];
    bool straight, flush, four, three;
    int pairs;
    
    void read_cards(void);
    void analyze_hand(void);
    void print_result(void);
    
    int main(void)
    {
    	for (;;) {
    		read_cards();
    		analyze_hand();
    		print_result();
    	}
    }
    
    void read_cards(void)
    {
    	bool card_exists[NUM_RANKS][NUM_SUITS];
    	char ch, rank_ch, suit_ch;
    	int rank, suit;
    	bool bad_card;
    	int cards_read = 0;
    	
    	for (rank = 0; rank < NUM_RANKS; rank++) {
    		num_in_rank[rank] = 0;
    		for (suit = 0; suit < NUM_SUITS; suit++)
    			card_exists[rank][suit] = false;
    		}
    	
    	for (suit = 0; suit < NUM_SUITS; suit++)
    		num_in_suit[suit] = 0;
    	
    	while (cards_read < NUM_CARDS) {
    		bad_card = false;
    		
    		printf("Enter a card: ");
    		
    		rank_ch = getchar();
    		switch (rank_ch) {
    		case '0':			exit(EXIT_SUCCESS);
    		case '2':			rank = 0; break;
    		case '3':			rank = 1; break;
    		case '4':			rank = 2; break;
    		case '5':			rank = 3; break;
    		case '6':			rank = 4; break;
    		case '7':			rank = 5; break;
    		case '8':			rank = 6; break;
    		case '9':			rank = 7; break;
    		case 't': case 'T': rank = 8; break;
    		case 'j': case 'J': rank = 9; break;
    		case 'q': case 'Q': rank = 10; break;
    		case 'k': case 'K': rank = 11; break;
    		case 'a': case 'A': rank = 12; break;
    		default:			bad_card = true;
    		}
    	
    		suit_ch = getchar();
    		switch (suit_ch) {
    			case 'c': case 'C': suit = 0; break;
    			case 'd': case 'D': suit = 1; break;
    			case 'h': case 'H': suit = 2; break;
    			case 's': case 'S': suit = 3; break;
    			default:			bad_card = true;
    		}
    		
    		while ((ch = getchar()) != '\n')
    			if (ch != ' ') bad_card = true;
    		
    		if (bad_card)
    			printf("Bad card; ignored.\n");
    		else if (card_exists[rank][suit])
    			printf("Duplicate card; ignored.\n");
    		else {
    			num_in_rank[rank]++;
    			num_in_suit[suit]++;
    			card_exists[rank][suit] = true;
    			cards_read++;
    		}
    	}
    }
    
    void analyze_hand(void)
    {
    	int num_consec = 0;
    	int rank, suit;
    	straight = false;
    	flush = false;
    	four = false;
    	three = false;
    	pairs = 0;
    	
    	for (suit = 0; suit < NUM_SUITS; suit++)
    		if (num_in_suit[suit] == NUM_CARDS)
    			flush = true;
    	
    	rank = 0;
    	while (num_in_rank[rank] == 0) rank++;
    	for (; rank < NUM_RANKS && num_in_rank[rank] > 0; rank++)
    		num_consec++;
    	if (num_consec == NUM_CARDS) {
    		straight = true;
    		return;
    	}
    	for (rank = 0; rank < NUM_RANKS; rank++) {
    		if (num_in_rank[rank] == 4) four = true;
    		if (num_in_rank[rank] == 3) three = true;
    		if (num_in_rank[rank] == 2) pairs++;
    	}
    }
    
    void print_result(void)
    {
    	if (straight && flush) 			printf("Straight flush");
    	else if (four)					printf("Four of a kind");
    	else if (three && pairs == 1)	printf("Full house");
    	else if (flush) 				printf("Flush");
    	else if (straight)				printf("Straight");
    	else if (three)					printf("Three of a kind");
    	else if (pairs == 2)			printf("Two pairs");
    	else if (pairs == 1)			printf("Pair");
    	else							printf("High card");
    	
    	printf("\n\n");
    }
    As you can see is a program that classifies a poker hand. I can't figure out what does this line of code:

    Code:
    while ((ch = getchar()) != '\n')
             if (ch != ' ') bad_card = true;
    Can you help me to understand what it does?

    Thank you and sorry about my english :)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    19
    Code:
    while ((ch = getchar()) != '\n')
             if (ch != ' ') bad_card = true;
    ch get a single character form keyboard and wait until user press the ENTER. If it got any character except space (' ') or enter, it was a bad card.

    It means that program needs user type only 2 character (e.g. "2c") or 2 character + space and ENTER.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    I don't see the utility of doing this... Maybe is usefull to clean the buffer? Another question if i type in the program only one character like the number 2 getchar keeps waiting for introducing a second character... Is not \n a character that can be assigned to the variable that holds the getchar? I was expecting that give me a bad card since i introduced the number 2 and then pressed enter \n. Another question getchar only prompts for a character if the buffer is empty right? If the buffer is full it doesn't prompt for entering a character? And if i introduce to the program "2 c" it will be detected as a bad card because rank_ch holds the 2 value but and space is asigned to suit_ch right?

IMN logo majestic logo threadwatch logo seochat tools logo