Why doesn't this poker C program work?

This is a discussion on Why doesn't this poker C program work? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <time.h> void shuffle(int [][13]); //shuffle function prototype void deal(int [][13], char *[], char *[]); ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    5

    Why doesn't this poker C program work?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle(int [][13]); //shuffle function prototype
    void deal(int [][13], char *[], char *[]); //deal function prototype
    
    void main()
    {
    		char *suit[4]={"Hearts", "Diamonds", "Clubs", "Spades"}; //suit pointer array
    		char *face[13]={"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; //face pointer array
    
    		int deck[4][13] = {0};
    		
    		srand(time(0));
    		
    		shuffle(deck); //shuffle function call
    		deal(deck, face, suit); //deal function call
    }
    
    void shuffle(int workdeck[][13]) //shuffle function
    {
    	int card, row, column;
    	for(card = 1; card <= 52; card++)
    	{
    		row = rand() % 4;
    		column = rand() % 13;
    		while(workdeck[row][column] != 0)
    		{
    			row = rand() % 4;
    			column = rand() % 13;
    		}
    
    		workdeck[row][column] = card;
    	}
    }
    
    void deal(int workdeck2[][13], char *workface[], char *worksuit[])
    {
    	int card, row, column;
    	int pairs = 0;
    	int count = 0;
    	
    	for(card = 1; card <= 5; card++)
    		for(row = 0; row <= 3; row++)
    			for(column = 0; column <= 12; column++)
    				if(workdeck2[row][column] == card)
    				{ 
    					printf("%5s of %-8s", workface[column], worksuit[row]); 
    					if (card % 2 == 0)
    						puts("");
    					else
    						printf("\t");
    				}
    	
    	workface[column]++;
    	worksuit[row]++;
    	
    	for(column = 0; column <= 12; column++)
    	{
        	if(*workface[column] == 4)
        		printf("Four of a kind.");
        	if(*workface[column] == 3)
        		printf("Three of a kind.");
        	if(*workface[column] == 2)
        		pairs++;
    		if(pairs == 1)
    			printf("Pair.");
    		if(pairs == 2)
    			printf("Two pairs.");
    	}
    	
    	column = 0;
    	while(*workface[column] == 0) 
    	{
    		column++;
    		for(; column <= 12 && *workface[column]; column++)
    			count++;
    		if(count == 5)
    			printf("Straight.");	
    	}
    			 
    	for(row = 0; row <= 3; row++) 
    		if(*worksuit[row] == 5)
    			printf("Flush");
    }

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    5
    I can get the program to deal out the cards, but I can't get it to determine the hand.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    All this part of the deal function doesn't look correct
    Code:
        	workface[column]++;
    	worksuit[row]++;
    	
    	for(column = 0; column <= 12; column++)
    	{
        	if(*workface[column] == 4)
        		printf("Four of a kind.");
        	if(*workface[column] == 3)
        		printf("Three of a kind.");
        	if(*workface[column] == 2)
        		pairs++;
    		if(pairs == 1)
    			printf("Pair.");
    		if(pairs == 2)
    			printf("Two pairs.");
    	}
    	
    	column = 0;
    	while(*workface[column] == 0) 
    	{
    		column++;
    		for(; column <= 12 && *workface[column]; column++)
    			count++;
    		if(count == 5)
    			printf("Straight.");	
    	}
    			 
    	for(row = 0; row <= 3; row++) 
    		if(*worksuit[row] == 5)
    			printf("Flush");
    Don't forget how you declared your deal function:
    Code:
    void deal(int workdeck2[][13], char *workface[], char *worksuit[]);
    This means such things as:
    Code:
    workface[column]++;         // You are incrementing a (char *), which doesn't have sense in your case
    Code:
    if(*workface[column] == 4)  // You are checking if the first char of a string
                                // has the numerical value of 4, which is also non-sense
    I'll let you think about it, because you do need to rethink about how you could do such things. Maybe someone could give you some hints, too. But I'm quite sure that you see at least why it's wrong.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can simplify everything by representing a card as a single int,
    and representing the deck as a single dimensional array of ints.

    int card;
    int deck[52];

    Initially load the deck with the values 0 to 51.
    Shuffling and dealing should be a lot easier.

    To determine a card's suit and value:

    suit = card / 13; /* number between 0 and 3 */
    value = card &#37; 13; /* number between 0 and 12, add 1 for 1 to 13 */

  5. #5
    isa
    isa is offline
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    Problem

    Do you have the final answer to this program?? I'm having some troubles trying to programing the same program.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > Do you have the final answer to this program?
    Post your specific attempt in a new thread.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. threaded program doesn't work?
    By OcTO_VIII in forum Linux Programming
    Replies: 1
    Last Post: 12-11-2003, 11:37 AM
  2. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 10:05 AM
  3. Please Help w/ Messy Poker Program
    By ray in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2002, 07:59 PM
  4. Can't get program to work
    By theirishrover12 in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 11:10 AM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 12:48 PM

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