Thread: Storing values from an array pointer

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    10

    Storing values from an array pointer

    I am stumped as to how to be able to store values from an array that is using pointers. Here is the code:
    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
    
    void shuffle(int [][num_faces]);
    void deal(int [][num_faces], char *[], char *[]);
    
    void main()
    {
    	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 deck[num_suits][num_faces] = {0};
            srand(time(0));
            shuffle(deck);
    	deal(deck, face, suit);
    }
    
    
    /* 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]);
    			       
    				}
    			}
    		}
    }
    The program deals out 5 cards of a poker hand and then prints the values of the 5 cards to the console. I would like to be able to store the values so that I can then use the values of the cards to determine if the hand has a pair, 3 of a kind, etc. I need to get over this problem so that I can do the rest of the program. Please advise. Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My advice would be:
    1. Decide what you mean when you say "values of the cards".
    2. Decide what data type could hold whatever you get out of question 1 above.
    3. Decide how many variables of the above type you will need to hold a hand.
    3. When dealing/printing out deal, put appropriate values into the above variables. (If we're doing this in a function, the variables may have to be passed in.)

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    The program currently will display the cards as such:

    Eight of Diamonds
    Two of Hearts
    Jack of Spades
    King of Clubs
    Seven of Diamonds

    Those would be the values of the cards (5 of them). So the program retrieves both the face value (2-9, J, Q, K, A) and the suit (Diamonds, Hearts, Spades, Club). However, using pointers, it is not stored in a format where I am able to retrieve the data and compare the cards to each other to see if there is a pair, 3 of a kind, etc. So I need to figure out how to store each of the 5 cards (face value and suit for each card) and retrieve that data so that I can do further processing ( see if there is a pair, 3 of a kind, etc).

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by newatc View Post
    However, using pointers, it is not stored in a format where I am able to retrieve the data and compare the cards to each other to see if there is a pair, 3 of a kind, etc.
    Why not? Why can't you represent each card as a pair of char *, one for the value and one for the suit?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    You may want to consider using two additional arrays as follows:

    Code:
    int numberinrank[num_faces];
    int numberinsuit[num_suits];
    
    /* 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 %d of %-8s %d",
    					workface[column], column, worksuit[row],  row);
    					 numberinrank[column]++;      				
                                             numberinsuit[row]++;
    			       
    				}
    			}
    		}
    	analyzehand(); // Is it a flush, straight, full house etc.
    	printresult(); // print the results
    }

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    All I want to be able to do is store the values. Asking questions about doing compares on char* is not the question, so you asking about that is no help. I want to be able to find out how to store the values of each hand. Is there anyone out there that knows how to do this?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by newatc View Post
    All I want to be able to do is store the values. Asking questions about doing compares on char* is not the question, so you asking about that is no help. I want to be able to find out how to store the values of each hand. Is there anyone out there that knows how to do this?
    I still don't understand why you think that the char* aren't the values of each card, but we'll leave that for now. I'll ask this instead: what do you want to store for each card? An integer? A float? A string? A pair of strings? A pair of integers? An integer and a string? The right one to pick is the one that makes the most sense to you.

    After that, I suppose you'll need to find an array, length cards_in_hand, of that type and, as you print (or instead of printing) you put the information into the array.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    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?

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    tabstop,
    It will need to store the string of whatever the card values are (face value of "two", "three", etc) and suit ("Diamonds", "Clubs", etc). That is what the program is displaying, the value of each card. After storing the values of the hand, then I should be able to continue to do the compares to see if the hand has pairs, etc.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, if you're printing the char* workface[column] and worksuit[row], those are probably the char* that you need to save then. Something like
    Code:
    handsuit[card] = worksuit[row];
    handface[card] = workface[column];
    (located where the printing happens) would store them, I should think (assuming handsuit and handface are properly set up).

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    I tried that before and it did not work. It got an error like:
    Cannot convert char* to char

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by tabstop View Post
    So, if you're printing the char* workface[column] and worksuit[row], those are probably the char* that you need to save then. Something like
    Code:
    handsuit[card] = worksuit[row];
    handface[card] = workface[column];
    (located where the printing happens) would store them, I should think (assuming handsuit and handface are properly set up).
    Quote Originally Posted by newatc View Post
    I tried that before and it did not work. It got an error like:
    Cannot convert char* to char
    Hence "properly set up". handsuit and handface have to be declared the same way as worksuit and workface: char *handsuit[].

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    That gets me nowhere. I just have what I had before, pointers. All that has been done is to duplicate pointers, and not storing the real values of the cards.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by newatc View Post
    That gets me nowhere. I just have what I had before, pointers. All that has been done is to duplicate pointers, and not storing the real values of the cards.
    Yes. Well. Two thoughts:
    1. You wanted to store the values of the card. Now you complain that you have not stored, you have "duplicated". What else did you expect -- surely you didn't want to store different values?
    2. Your belief that there is a difference between "char *" and "string" is touching. However, it has very little basis in reality.

    I also ask a favor. Make the changes in red:
    Code:
    void deal(int workdeck2[][13], char *workface[], char *worksuit[])
    { int card, row, column;
            char *handface[5], *handsuit[5];
            int i,j; 
    	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]);
    			           handface[card-1] = workface[column];
                                       handsuit[card-1] = worksuit[row];
    				}
    			}
    		}
            for (i=0; i < 4; i++)
                    for (j=i+1; j < 5; j++)
                            if (handface[i]==handface[j]) printf("Pair: %d %d\n", i, j);
    }
    and see if just having those dirty pointers does you any good. (I realize that I'm cheating by hard-coding 5 instead of cards_in_hand, but you get the idea.) (And hopefully it doesn't take too many runs until you get a pair.)

  15. #15
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    [CODE]and
    Code:
    numberinrank[column]++; 
    numberinsuit[row]++;
    Example of how this works. Let's assume that column = 8 which is a NINE. Incrementing numberinrank[8] by one indicates that there is one NINE in the hand. Another increment would indicate that there are two NINEs in the hand etc

    Same thing goes for numberinsuit. Assume row = 3, SPADES. Initially incrementing numberinsuit[3] by one indicates that there is one spade in the hand. Another increment would indicate that there are two spades in the hand. This would be used to identify a flush.

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