Thread: basic question(card program)

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    basic question(card program)

    i have been trying to figure out how to do this for the last few days. my book doesnt really have any examples i can follow. i made a 52 card deck, shuffle, and then deal 5 cards. now it needs to say if there is a pair, 2 pair, 3 of a kind, straight, or flush. i thought i should use a pointer to somehow refer to the columns with the cards' face values in them, then compare them for the pairings and straight, and a pointer to refer to the rows with the cards' suits in them for the flush. is this even possible? i was also thinking i could store the values of the column or row into chars to be later compared, but i dont know of a way to do this without the chars being overwritten each time the funcion loops through. if anyone can try to explain to me what needs to happen, suggest a way to go about doing it, or point me to a good example (i already went through many searches for card, poker and found one that dealt with my problem but i didnt understand it), but hopefully just explain to me how the actions i need to have the program perform work. thanks in advance, here is what i have so far, nothing with comparing in it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle( int wdeck[][13]);
    void deal( const int wdeck[][13], const char *wface[],
    	 const char *wsuit[]);
    void pair( int a[]);
    	
    		  
    int main()
    {
    	const char *suit[4] = {"hearts", "diamonds", "clubs", "spades"};
    
    	const char *face[13] = {"ace", "deuce", "three", "four", "five",
    		"six", "seven", "eight", "nine", "ten", "jack", "queen", "king"};
    
    	int deck[4][13] ={0};
    
    	srand( time(0));
    
    	shuffle(deck);
    	deal(deck, face, suit);
    	pair(face);
    
    	return 0;
    
    }
    
    void shuffle(int wdeck[][13])
    {
    	int row;
    	int column;
    	int card;
    
    	for(card = 1; card <= 52; card++) {
    		do {
    			row = rand() % 4;
    			column = rand() % 13;
    		}while(wdeck [row] [column] !=0);
    
    		wdeck [row] [column] = card;
    	}
    }
    
    void deal(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    
    
    	for ( card = 1; card <= 5; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s of %-8s%c\n\n", wface[column], wsuit[row],
    					card % 1 == 0 );
    					
    				}
    
    			}
    		}
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll want to test for each possibility. There are a number of ways to do it. Any way you do it, you're still going to need to test for each case. Consider testing for the best hand first, and working backwards. Just devise a test to check for one, once you have it done, write one to test the next.

    Work through them, and who knows, maybe you'll hit on something really slick as you go along.


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

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    my problem is i dont know how to test them though. should i try and use pointers? is it possible to make a pointer to the row or column of an array? would trying to store the values in a char. even work? my most specific question, i guess, is how do i make something that points to the row/ column of the array based on the number (1-52), of the card?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you check to see what your hand is in poker, with real cards? You look at your cards one at a time, see what they are, and compare them with the known hands. You do the same thing when you turn it into code. Look at the hand a card a a time, and tally up what you have.

    Your code doesn't keep track of cards though. You aren't keeping track of the hand any place. You just "deal" it (ie: you simply display some cards). You'll have to keep track of them some place so you have something to test against the various hands.

    Work on getting your code to deal a hand. Pass the hand to another function to display it. That'll at least get you started.


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

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    3
    would you advise using a pointer to refer back to those columns and rows of the array? and regarding your previous post, are you saying that i need to store each value as it is dealt? i just cant see how i could store the values without them just being overwritten each time the function loops through. i was also wondering if there is a command like scanf that will read the values as they are printed on screen? but still, i think the stored values would just be overwritten each time...thanks for the help so far.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jimm
    would you advise using a pointer to refer back to those columns and rows of the array? and regarding your previous post, are you saying that i need to store each value as it is dealt? i just cant see how i could store the values without them just being overwritten each time the function loops through.
    Where do you store the cards when you're playing poker and the dealer deals them? You store them in your hand. Can you think of a way to make a hand in C to store your cards?


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

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

    Storing

    In this program, how do you store the values of the hand? The way the program is written, it uses pointers to be able to print the hand values to the console. However, in order to check the hand if it has a, say, pair, the program would need to store the values of each card in the hand. How would this be done (storing the values in an array)?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Check the forum rules, this thread was over 2 years old.
    Post a new thread with your actual code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  3. basic animation program
    By Noobie in forum C++ Programming
    Replies: 16
    Last Post: 02-17-2003, 06:33 PM
  4. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM