Thread: Storing values from an array pointer

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    Seems easier to me to think of the deck of cards as an array of structs, with the struct card having:

    *an int for it's value from 2 (Deuce's), to 14 (Ace's)
    *an int representing it's suit (from lowest to highest in Poker), 0 to 3
    *an int representing it's current position: 0=in deck, 1 - 8 for the player who has the card, -1=card is discarded and out of play currently.

    With the above and a small enumerated list or array for the names: "Two", - Ace". (easy if you leave element 0 and 1 blank, and use 2 - 14, only). Beyond 14 would be "High", "Pair", Three of a Kind", up to "Royal Flush". These strings are the only non-intuitive part of the data, imo.

    By looping through the array of card structs, you can derive all the info for each player's hand, and print it out right, with just the help of some strings. The problem of handling the arrays becomes much simpler, imo.

  2. #17
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Well, my thinking was that the hand analysis function would first determine whether or not the hand had a flush by using the suits array. After which, the analyis function would determine if the hand had one pair, two pair, full house etc. None of these hand combinations has any need for the suits array. I didn't want to clutter up the "hand" identification processing with anything referring to the suits since suits wasn't needed in this logic. There is a fair amount of logic in the "hand" identification and IMHO, referring to the suits in this logic would only "clutter" up the logic. Thus, the reason for two arrays, card rank and suits. The suits array used once at the beginning of the hand processing function and the rank array used throughout the hand processing function.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,866
    I understood your idea, Bob. I just didn't find the data structures being mentioned, a good fit for a card game.

    Structs ability to group all or most of the characteristics of the card, together, makes them an excellent choice for this game, imo.

    Referring to card.number, card.suit, card.position, etc., is simple, clear, and something I don't see as a "burden" to the logic of the program.

    It is the failure to use a good set of data structures, that caused the OP to have problems in the first place, imo.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Tabstop,
    I see that this does work in comparing the values to see if there is a match or not (for a pair). However, it will only return the value of the array index that matches, not the actual card values. By not storing the actual data values, there is no way to tell what card values, themselves, match, since those values are not stored in handface* and handsuit*, only the element in the array. So if a pair does occur, the result will show something like:
    Pair: 2 5
    The 2, 5 could be any of the card face values that matches, but as far as I can tell that card face value is still unknown when done this way. If done this way, I would have to be able to translate the 2 and 5 to whatever the actual face value of the card (Ace, Two, etc). That is why I was trying to find a way to actually store the card face values, themselves (Ace, Two, Three, ....etc). That way I can display, if a pair if found, something like
    Pair of Aces
    or
    Pair: Ace of Spades
    Ace of Diamonds
    That is where I am stuck and that is why I think I need to find a way to translate the pointer to move the card face values (and card suit values) into another array.

    Oh, and, yes, thanks for your help, but I am still stuck with the original problem.

  5. #20
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Tabstop,
    Made an typo on the previous note.
    There would never be a "5" in the
    Pair: 2 5
    Since it is only 0-4 (for a total of 5). So it would be more appropiate to use the example of:
    Pair: 2 4
    Sorry for the mistake.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by newatc View Post
    Tabstop,
    I see that this does work in comparing the values to see if there is a match or not (for a pair). However, it will only return the value of the array index that matches, not the actual card values. By not storing the actual data values, there is no way to tell what card values, themselves, match, since those values are not stored in handface* and handsuit*, only the element in the array. So if a pair does occur, the result will show something like:
    Pair: 2 5
    The 2, 5 could be any of the card face values that matches, but as far as I can tell that card face value is still unknown when done this way. If done this way, I would have to be able to translate the 2 and 5 to whatever the actual face value of the card (Ace, Two, etc). That is why I was trying to find a way to actually store the card face values, themselves (Ace, Two, Three, ....etc). That way I can display, if a pair if found, something like
    Pair of Aces
    or
    Pair: Ace of Spades
    Ace of Diamonds
    That is where I am stuck and that is why I think I need to find a way to translate the pointer to move the card face values (and card suit values) into another array.

    Oh, and, yes, thanks for your help, but I am still stuck with the original problem.
    Code:
    if (handface[i]==handface[j]) printf("Pair: %s of %s; %s of %s %d\n", handface[i], handsuit[i], 
        handface[j], handsuit[j]);
    You have a set-up of all the possible strings you need; handface[i] and handface[j] point to the same thing if they match (which is why they compare equal) and the thing they point to is the value of the card. Remember: there is no such thing as a string in C, just a pointer to a bunch of characters in a row.

    You can strcpy the strings into another array of char[] if you feel better about it; instead of comparing pointers as above, you can then strcmp the hand values. More complicated, and more memory being used (not that we're in any danger of running out, here, or anything).

    Edit to add: And of course, you would actually, eventually, declare handface[] and handsuit[] in your main program and pass them into your function to be "dealt into".
    Last edited by tabstop; 01-05-2008 at 02:56 PM.

  7. #22
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    I guess I am not sure how this will help. It does not appear to store the values of the cards so it cannot be referenced. You added "column" and "row" to this line:
    [code]
    workface[column], column, worksuit[row], row);
    [\code]
    and
    [code]
    numberinrank[column]++;
    numberinsuit[row]++;
    [\code]
    right after that. It does not appear that this stores the values of the face and suit of the card. Is it supposed to?
    I modified your code to show how it works. Also added another array to print out the selected cards. It's your responsibility to thoroughly test the sample.

    /
    Code:
    * Poker program */
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define num_faces 13
    #define num_suits 4
    #define cards_in_hand 5
    #define TRUE 1
    #define FALSE 0
    
    typedef int BOOL;
    
    BOOL bStraight, bFlush, bFour, bThree;
    int iPairs;   /* can be 0, 1, or 2 */
    
    char *suit[num_suits]={"Clubs", "Diamonds", "Hearts", "Spades"};
    char *face[num_faces]={"Ace", "Deuce", "Three", "Four", "Five", "Six",
    		"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    
    int num_in_rank[num_faces];
    int num_in_suit[num_suits];
    int fivecardarray[num_faces][num_suits];
    void shuffle(int [][num_faces]);
    void deal(int [][num_faces], char *[], char *[]);
    
    void PrintSelectedCards(void)
    {
    	int iRow  = 0, iColumn = 0;
    	int iTempRow, iRowCount;
    	if ((bThree &&  iPairs == 1) || (bStraight || bFlush))   
    	{
    		for(iColumn = 0; iColumn < num_faces; iColumn++)
    		{
    			for(iRow = 0; iRow < num_suits; iRow++)
    			{
    				if(fivecardarray[iColumn][iRow] == 1)
    				{
    					printf("%s of %s ", face[iColumn] ,suit[iRow]);
    					fivecardarray[iColumn][iRow] = 0;    
    				}
    			}
    		}
    	}   
    	else if (bThree || bFour)   
    	{
    		for(iColumn = 0; iColumn < num_faces; iColumn++)
    		{
    			iRowCount = 0;
    			for(iRow = 0; iRow < num_suits; iRow++)
    			{
    				if(fivecardarray[iColumn][iRow] == 1)
    					++iRowCount;
    				if(iRowCount == 3)
    				{
    					for(iTempRow = 0; iTempRow  < num_suits; iTempRow++ )
    					{
    						if(fivecardarray[iColumn][iTempRow] == 1)
    						{
    							printf("%s of %s ", face[iColumn] ,suit[iTempRow]);
    							fivecardarray[iColumn][iTempRow] = 0;
    						}
    					}
    					if(bThree)
    						break;
    				}
    			}        
    		}
    	}
    	else if (iPairs == 2  || iPairs == 1)
    	{
    		for(iColumn = 0; iColumn < num_faces; iColumn++)
    		{
    			iRowCount = 0;
    			for(iRow = 0; iRow < num_suits; iRow++)
    			{
    				if(fivecardarray[iColumn][iRow] == 1)
    					++iRowCount;
    				if(iRowCount > 1)
    				{
    					for(iTempRow = 0; iTempRow  < num_suits; iTempRow++ )
    					{
    						if(fivecardarray[iColumn][iTempRow] == 1)
    						{
    							printf("%s of %s ", face[iColumn] ,suit[iTempRow]);
    							fivecardarray[iColumn][iTempRow] = 0;
    						}
    					}
    					if(iPairs == 1)
    						break;
    				}
    			}        
    		}
    	}
    	else
    	{
    		// Check for ace first;
    		for(iRow  = 0; iRow < num_suits; iRow++)
    			if(fivecardarray[iColumn][iRow] == 1)
    			{
    				printf("%s of %s\n", face[iColumn] ,suit[iRow]);
    				return;
    			}
    		for(iColumn = 12; iColumn > 0; iColumn--)
    			for(iRow = 0; iRow < num_suits; iRow++)
    				if(fivecardarray[iColumn][iRow] == 1)
    				{
    					printf("%s of %s\n", face[iColumn] ,suit[iRow]);
    					return;
    				}           
    	}
    }
    
    void AnalyzeHand(void)
    {
    	int iNumberConsecutive = 0;
    	int iRank, iSuit;
    
    	bStraight = FALSE;
    	bFlush = FALSE;
    	bFour = FALSE;
    	bThree = FALSE;
    	iPairs = 0;
    	/* Flush? */
    	for (iSuit = 0; iSuit < num_suits; iSuit++)
    		if (num_in_suit[iSuit] == cards_in_hand)
    			bFlush = TRUE;
    	/* Straight */
    	iRank = 0;
    	while (num_in_rank[iRank] == 0) iRank++;
    	for (; iRank < num_faces && num_in_rank[iRank]; iRank++)
    		iNumberConsecutive++;
    	if (iNumberConsecutive == cards_in_hand) {
    		bStraight = TRUE;
    		return;
    	}
    	/*  4 of a kind, 3 of a kind or pairs ? */
    	for (iRank = 0; iRank < num_faces; iRank++) {
    		if (num_in_rank[iRank] == 4) bFour = TRUE;
    		if (num_in_rank[iRank] == 3) bThree = TRUE;
    		if (num_in_rank[iRank] == 2) iPairs++;
    	}
    }
    
    void PrintHand(void)
    {
    	printf("\n");
    	if (bStraight && bFlush) printf("Straight Flush\n\n");
    	else if (bFour)         printf("Four of a kind\n\n");
    	else if (bThree &&
    		iPairs == 1)   printf("Full house\n\n");
    	else if (bFlush)        printf("Flush\n\n");
    	else if (bStraight)     printf("Straight\n\n");
    	else if (bThree)        printf("Three of a kind\n\n");
    	else if (iPairs == 2)   printf("Two pairs\n\n");
    	else if (iPairs == 1)   printf("Pair\n\n");
    	else
    		printf("High card\n\n");
    	PrintSelectedCards();
    }
    
    int main(void)
    {
    	int deck[num_suits][num_faces] = {0};
    	srand(time(0));
    	shuffle(deck);
    	deal(deck, face, suit);
    	return 0;
    }
    
    /* Function to use random generator to get random suit and random card values */
    void shuffle(int workdeck[][13])
    
    {
    	int card, row, column;
    	for(card = 1; card <= num_suits * num_faces; card++)
    	{
    		row = rand() % num_suits;
    		column = rand() % num_faces;
    		while(workdeck[row][column] != 0)
    		{
    			row = rand() % num_suits;
    			column = rand() % num_faces;
    		}
    		workdeck[row][column] = card;
    	}
    }
    
    /* Function to output to console (print) suit and card values */
    void deal(int workdeck2[][13], char *workface[], char *worksuit[])
    {
    	int card, row, column;
    	for (card = 1; card <= cards_in_hand; card++)
    	{
    		for (row = 0; row <= 3; row++)
    		{
    			for (column = 0; column <= 12; column++)
    				if (workdeck2[row][column] == card)
    				{ 
    					printf("\n%5s of %-8s ",
    						workface[column], worksuit[row]);
    					fivecardarray[column][row] = 1;
    					num_in_rank[column]++;
    					num_in_suit[row]++;
    				}
    		}
    	}
    	AnalyzeHand();
    	PrintHand();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM