Help with Poker Game

This is a discussion on Help with Poker Game within the C Programming forums, part of the General Programming Boards category; Hey guys I'm having a little trouble figuring out how I can break a tie by the highest card face ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    16

    Help with Poker Game

    Hey guys I'm having a little trouble figuring out how I can break a tie by the highest card face in a hand. If anyone has any suggestions that would be great!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    #define FALSE 0
    #define TRUE  1
    #define SUITS 4
    #define FACES 13
    #define AVAILABLE 0
    #define TAKEN 1
    
    
    void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[]); 
    void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand []);
    void dealAHand2(char *suits[], char *faces[], int deck[][FACES], int suitsInHand2[], int facesInHand2[]);
    void analyzeHand(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[], int *hand1);
    void analyzeHand2(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[], int *hand2);
    
    
    main () {
    	char *suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
    	char *faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven", 
    		"Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
    	char *win[9] = {"High Card", "Pair", "Two Pairs", "Three of a Kind", "Straight", "Flush", "Full House", "Four of a Kind", "Straight Flush"};
    	int deck[4][13] = {AVAILABLE};
    	int suitsInHand[4] = {AVAILABLE};
    	int facesInHand[13] = {AVAILABLE};
    	int suitsInHand2[4] = {AVAILABLE};
    	int facesInHand2[13] = {AVAILABLE};
    	int hand1 = 0, hand2 = 0;
    
    
    	srand(time(NULL));
    
    
    	dealAHand(suits, faces, deck, suitsInHand, facesInHand);
    	analyzeHand(suits, faces, deck, suitsInHand, facesInHand, &hand1);
    
    
    
    	dealAHand2(suits, faces, deck, suitsInHand2, facesInHand2);
    	analyzeHand2(suits, faces, deck, suitsInHand2, facesInHand2, &hand2);
    
    	if (hand1 > hand2)
    		printf("Player one wins with a %s! \n", win[hand1]);
    	else if (hand1 < hand2)
    		printf("Player two wins with a %s! \n", win[hand2]);
    
    	
    		system("pause");
    	}
    
    	void dealAHand(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[]){
    		int i;
    		for (i = 0; i < 5; i++)
    			dealACard(suits, faces, deck, suitsInHand, facesInHand);
    		printf("\n");
    	}
    
    	void dealAHand2(char *suits[], char *faces[], int deck[][FACES], int suitsInHand2[], int facesInHand2[]){
    		int i;
    		for (i = 0; i < 5; i++)
    			dealACard(suits, faces, deck, suitsInHand2, facesInHand2);
    		printf("\n");
    	}
    
    	void dealACard(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[]){
    		int suitsIndex, facesIndex;
    
    		suitsIndex = rand() % 4;
    		facesIndex = rand() % 13;
    
    		while (deck[suitsIndex][facesIndex] == TAKEN){
    			suitsIndex = rand() % 4;
    			facesIndex = rand() % 13;
    
    		}
    		suitsInHand[suitsIndex] += 1;
    		facesInHand[facesIndex] += 1;
    		deck[suitsIndex][facesIndex] = TAKEN;
    
    		printf("%s of %s \n", faces[facesIndex], suits[suitsIndex]);
    	}
    
    
    	void analyzeHand(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[], int *hand1)
    	{
    		int num_consec = 0;
    		int rank, suit;  // the rank of a card is 0 – 12 (representing cards 2 – Ace)
    
    		int straight = FALSE;
    		int flush = FALSE;
    		int four = FALSE;
    		int three = FALSE;
    		int pairs = 0;
    
    
    
    		// check for flush – 5 cards of the same suit
    		for (suit = 0; suit < SUITS; suit++)
    			if (suitsInHand[suit] == 5)
    				flush = TRUE;
    
    		// check for straight – eg. One each of 5,6,7,8,9
    		//    locate the first card
    		rank = 0;
    		while (facesInHand[rank] == 0) 
    			rank++;
    
    		// count the consecutive non-zero faces
    		//  the loop above initializes the rank variable as the first non-zero counter
    
    		for (; rank < FACES && facesInHand[rank]; rank++)
    			num_consec++;
    
    		if (num_consec == 5) {
    			straight = TRUE;
    			return;  // don’t bother to check anything else if there’s a straight
    		}
    
    
    		/* 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");
    			*hand1 = 8;}
    		else if (four) {
    			printf("Four of a kind\n\n");
    			*hand1 = 7;}
    		else if (three && pairs == 1)  { 
    			printf("Full house\n\n");
    			*hand1 = 6;}
    		else if (flush){        
    			printf("Flush\n\n");
    			*hand1 = 5;}
    		else if (straight) {
    			printf("Straight\n\n");
    			*hand1 = 4;}
    		else if (three){        
    			printf("Three of a kind\n\n");
    			*hand1 = 3;}
    		else if (pairs == 2) {  
    			printf("Two pairs\n\n");
    			*hand1 = 2;}
    		else if (pairs == 1){   
    			printf("Pair\n\n");
    			*hand1 = 1;}
    		else {          
    			printf("High card\n\n");
    			*hand1 = 0;}
    	}
    
    	void analyzeHand2(char *suits[], char *faces[], int deck[][FACES], int suitsInHand[], int facesInHand[], int *hand2)
    	{
    		int num_consec = 0;
    		int rank, suit;  // the rank of a card is 0 – 12 (representing cards 2 – Ace)
    
    		int straight = FALSE;
    		int flush = FALSE;
    		int four = FALSE;
    		int three = FALSE;
    		int pairs = 0;
    
    
    
    		// check for flush – 5 cards of the same suit
    		for (suit = 0; suit < SUITS; suit++)
    			if (suitsInHand[suit] == 5)
    				flush = TRUE;
    
    		// check for straight – eg. One each of 5,6,7,8,9
    		//    locate the first card
    		rank = 0;
    		while (facesInHand[rank] == 0) 
    			rank++;
    
    		// count the consecutive non-zero faces
    		//  the loop above initializes the rank variable as the first non-zero counter
    
    		for (; rank < FACES && facesInHand[rank]; rank++)
    			num_consec++;
    
    		if (num_consec == 5) {
    			straight = TRUE;
    			return;  // don’t bother to check anything else if there’s a straight
    		}
    
    
    		/* 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");
    			*hand2 = 8;}
    		else if (four) {
    			printf("Four of a kind\n\n");
    			*hand2 = 7;}
    		else if (three && pairs == 1)  { 
    			printf("Full house\n\n");
    			*hand2 = 6;}
    		else if (flush){        
    			printf("Flush\n\n");
    			*hand2 = 5;}
    		else if (straight) {
    			printf("Straight\n\n");
    			*hand2 = 4;}
    		else if (three){        
    			printf("Three of a kind\n\n");
    			*hand2 = 3;}
    		else if (pairs == 2) {  
    			printf("Two pairs\n\n");
    			*hand2 = 2;}
    		else if (pairs == 1){   
    			printf("Pair\n\n");
    			*hand2 = 1;}
    		else {          
    			printf("High card\n\n");
    			*hand2 = 0;}
    	}

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    16
    I think I figured it out a little bit but need help getting there

    if in the debugger my array looks like this

    facesInHand
    [0] 0
    [1] 0
    [2] 1
    [3] 0
    [4] 1
    [5] 0
    [6] 1
    [7] 0
    [8] 0
    [9] 1
    [10] 0
    [11] 0
    [12] 1

    I need to figure out how I read that the highest value in the array is in the 12th spot, or give the address to another array/int.

    I tried passing it through a for loop but all I got was the values in the array, so it turned out useless.

    If anyone had any help that would be great thanks!

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    16
    Ok I was waaay overthinking it...

    Code:
        for (i = 0; i < FACES; i++)
            if (facesInHand[i] > 0)
            highest = i;
    That should do the trick!

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
        
    for (i = 0; i < FACES; i++)
        if (facesInHand[i] > 0)
            highest = i;
    How about starting the loop with the highest face and search backwards?
    Code:
    i = FACES - 1;
    while (facesInHand[i] == 0)
        i--;
    highest = i;
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poker Game need help...
    By boyracer88 in forum Game Programming
    Replies: 1
    Last Post: 11-25-2005, 01:32 PM
  2. Poker Game
    By egomaster69 in forum C Programming
    Replies: 18
    Last Post: 01-03-2005, 06:24 PM
  3. poker game
    By b00l34n in forum Game Programming
    Replies: 14
    Last Post: 01-03-2005, 12:21 PM
  4. Poker Game
    By Smiley**123 in forum Game Programming
    Replies: 6
    Last Post: 12-14-2003, 09:48 AM
  5. poker game help
    By datainjector in forum C Programming
    Replies: 5
    Last Post: 09-06-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21