Thread: Making a Poker Game

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    Making a Poker Game

    I need some help in writing this program that plays 10 hands of Poker and outputs to the user what his hand is.

    The problem I'm having is trying to find out how to use suitsInHand[SUITS] and facesInHand[FACES] as counters for suits and faces in a hand.

    suitsInHand is 4 counters that represents how many hearts, clubs, diamonds, spades are in the hand. These 4 counters must add up to 5. For example if you have 5 hearts in the hand of cards, the array would have the values 5,0,0,0

    facesInHand is 13 counters, that represent how many two’s, three’s, etc. you have in the hand. This must also total 5. For example, if you have a pair of 3’s, and three Kings’s, this array would have the values 0,2,0,0,0,0,0,0,0,0,3,0

    While dealing a hand of cards, keep a count of the suitsInHand, and facesInHand.
    Code:
    Points of interest for applying suitsInHand and facesInHand as counters for suits and faces of a hand
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define SUITS 4 // suits of cards
    #define FACES 13 // two - ace
    #define AVAILABLE 0 // card not drawn from deck
    #define TAKEN 1 // card has been drawn from deck
    #define SIZE 5
    #define TRUE 1
    #define FALSE 0
    
    void dealACard(char *suits[], char *faces[], int deck[][FACES]);
    void dealAHand(char *suits[], char *faces[], int deck[][FACES]);
    void analyzeHand(int suitsInHand[], int facesInHand[]); //igrone this for now
    
    typedef int bool;
    bool straight, flush, four, three;
    int pairs;   /* can be 0, 1, or 2 */
    
    main(){
    
    	char *suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
    	char *faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace" };
    	int deck[4][13] = { AVAILABLE }; // using array to make sure the same card is drawn from a single deck
    	int i;
    	int suitsInHand[SUITS], facesInHand[FACES];
    
    	srand(time(NULL));
    
    	for(i = 0; i < 10; i++) {
    		dealAHand(suits, faces, deck);
    
     		analyzeHand(suitsInHand, facesInHand); // igrone for now
    	}
    
    	system("pause");
    }
    
    void dealAHand(char *suits[], char *faces[], int deck[][FACES]){
    	int i;
    	for(i = 0; i < 5; i++)
    		dealACard(suits, faces, deck);
    	printf("\n");
    
    }
    
    void dealACard(char *suits[], char *faces[], int deck[][FACES]){
    
    	int suitIndex, faceIndex;
    
    	suitIndex = rand() % 4;
    	faceIndex = rand() % 13;
    
    	while (deck[suitIndex][faceIndex] == TAKEN){
    		suitIndex = rand() % 4;
    		faceIndex = rand() % 13;
    	}
    
    	deck[suitIndex][faceIndex] = TAKEN;
    
    	printf("%s of %s \n", faces[faceIndex], suits[suitIndex]);
    
    }
    
    /*
    analyzeHand: Determines whether the hand contains a 
    straight, a flush, four-of-a-kind,      
    and/or a three-of-a-kind; determines the
    number of pairs; stores the results into 
    the external variables straight, flush,  
    four, three, and pairs.                  
    */
    
    void analyzeHand(int suitsInHand[], int facesInHand[]){
    
    	int num_consec = 0;
    	int rank, suit;
    
    	straight = FALSE;
    	flush = FALSE;
    	four = FALSE;
    	three = FALSE;
    	pairs = 0;
    
    	/* check for flush */
    	for (suit = 0; suit < SUITS; suit++)
    		if (suitsInHand[suit] == 5)
    			flush = TRUE;
    
    	/* check for straight */
    	rank = 0;
    	while (facesInHand[rank] == 0) 
    		rank++;
    
    	for (; rank < FACES && facesInHand[rank]; rank++)
    		num_consec++;
    
    	if (num_consec == 5) {
    		straight = TRUE;
    		return;
    	}
    
    	/* check for 4-of-a-kind, 3-of-a-kind, and pairs */
    	for (rank = 0; rank < FACES; rank++) {
    		if (facesInHand[rank] == 4) 
    			four = TRUE;
    		if (facesInHand[rank] == 3) 
    			three = TRUE;
    		if (facesInHand[rank] == 2) 
    			pairs++;
    	}
    
    	if (straight && flush) 
    		printf("Straight flush\n\n");
    	else if (four)         
    		printf("Four of a kind\n\n");
    	else if (three && pairs == 1)   
    		printf("Full house\n\n");
    	else if (flush)        
    		printf("Flush\n\n");
    	else if (straight)     
    		printf("Straight\n\n");
    	else if (three)        
    		printf("Three of a kind\n\n");
    	else if (pairs == 2)   
    		printf("Two pairs\n\n");
    	else if (pairs == 1)   
    		printf("Pair\n\n");
    	else                   
    		printf("High card\n\n");
    
    }
    Last edited by krazyxazn; 04-07-2009 at 05:44 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    May not be what you want, but I'd suggest using a card struct to include value, face, etc., and then make an array of those structs up.

    That way, each card carries it's characteristics, with it. Sort of like OOP, without the pita part of OOPS.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I prefer to represent cards by a simple number from 0-51. There are thirteen cards per suit, which gives us the following:
    Code:
    suit = card / 13;
    value = card % 13;
    And if you really need to set a spot as unused, simply set it to 52.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    If you let the card be a floating point then the ace of hearts can be 13.4 and the ace of clubs can be 13.1

    that way cards of the same face value can be compared easily (13.4 > 13.1)
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Amusing, yes. But I can pack a card deck into a byte using 0-51.
    Code:
    #define SAMEFACE(c1, c2) ((c1)%13 == (c2)%13)
    ...
    if( SAMEFACE( c1, c2 ) )
        printf( "Cards are of the same face value.\n" );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  3. Making a game
    By KneeGrow in forum Game Programming
    Replies: 1
    Last Post: 04-16-2004, 12:40 PM
  4. Lets Play Money Making Game
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-04-2001, 08:36 PM