Thread: Card Dealing function

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    6

    Card Dealing function

    So I have to write a simple Blackjack game for school. The only issue I have is making a card dealing function that only deals one card at a time from an array and moves on to the next element in that array for the next time its called.


    Code:
    int cardDeal ()
    {    
        int deal = 51;
        card = deck[deal--];
        printf("\n%d\n", card);
    
    
        
        return 0;
    }
    If I run this function twice in my main (), it deals the same element every instead of moving from element 51 to 50 and so on.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Every time that function is called, a new variable called deal is created and initialized to 51.

    One option is to make the deal variable a static variable, meaning that it will reuse the same variable on successive calls.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    6
    Quote Originally Posted by rags_to_riches View Post
    Every time that function is called, a new variable called deal is created and initialized to 51.

    One option is to make the deal variable a static variable, meaning that it will reuse the same variable on successive calls.
    I appreciate the solution. In my class we have yet to learn about static variables. So I'm trying to solve the issue using only what we've covered in class. Not that helps with anybody trying to help me :/
    Do you happen to have any other solutions to this issue?

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    6
    So here's what I have so far. Basically down at the bottom is my card dealing function. I have it to where it draws the element from the last slot of the array and prints that as the card dealt. But I need a way to where if I use that function again it prints the next element before "51". I can't figure out how to code it that way. Sorry, I've just begun learning to code a few months ago and as this is for school I'm limited to using only the things we've covered in class. But any suggestions would be greatly appreciated!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define DECKSIZE 52
    
    
    int deck[DECKSIZE];
    int i;
    int playerHand;
    int dealerHand;
    
    
    
    
    
    
    int dealCard ();
    int cardDeal ();
    
    
    int main ()
    {
    
    
    
    
    dealerHand = deck[i];
    playerHand = deck[i];
    
    
        dealCard ();
        cardDeal ();
        cardDeal ();
        return 0;
    }
    
    
    int dealCard ()
    {
    
    
    int randomNumber;
    srand(time(NULL));    
    int numberPrinted = 0;
        
        for(i = 0; i < DECKSIZE; i++)
        {        
        randomNumber = rand() %11 + 1;        
        deck[i] = randomNumber;
        printf("%d\t", deck[i]);
        if (numberPrinted < 3)
        numberPrinted ++;
        else
        {
        printf("\n");
        numberPrinted = 0;
        }
        }
        return 0;
    }
    
    
    int cardDeal ()
    {    
        
        printf("\n%d\n", deck[DECKSIZE - 1]);
        
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    DECKSIZE expands to a value 52. That is fixed.

    Your function prints out the value of deck[DECKSIZE-1], so will always print the same element of deck. If you want different values to be printed, either you need to shuffle the deck after accessing every card, or find a way to update the index of the element obtained.



    Better to avoid static variables entirely, and pass all the information needed (the deck, the size of the deck if that can vary, the index to be retrieved). If your function needs to change the index to be retrieved, pass a pointer to it (which means the caller will be able to see the change of index).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Dec 2014
    Posts
    6

    Resolved!

    I fixed the code, which was actually a really stupid easy fix. I made the deal variable global

    Here is my updated code. The next thing I'm doing is adding an if-then statement to see if the playerHandTotal > 21. I'm not sure where I should plug in the if-then? I'm thinking at the beginning of the playerChoice function? Before it runs the switch?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define DECKSIZE 52
    
    
    int deck[DECKSIZE];
    
    
    int card;
    int playerHand;
    int dealerHand;
    int playerHandTotal;
    int dealerHandTotal;
    int deal = 51;
    
    
    
    
    
    
    int shuffle ();
    int dealCardDealer ();
    int dealCardPlayer ();
    int playerChoice ();
    
    
    int main ()
    {
    	shuffle ();
    	dealCardDealer ();
    	printf("Dealer has %d", dealerHandTotal);
    	dealCardPlayer ();
    	dealCardPlayer ();
    	printf("Player has %d", playerHandTotal);
    	playerChoice ();
    	
    	
    }
    
    
    int shuffle ()
    {
    int i;
    int randomNumber;
    srand(time(NULL));	
    int numberPrinted = 0;
    	
    	for(i = 0; i < DECKSIZE; i++)
    	{		
    	randomNumber = rand() %11 + 1;		
    	deck[i] = randomNumber;
    	printf("%d\t", deck[i]);
    	if (numberPrinted < 3)
    	numberPrinted ++;
    	else
    	{
    	printf("\n");
    	numberPrinted = 0;
    	}
    	}
    	return 0;
    }
    
    
    
    
    
    
    int dealCardDealer ()
    {    
        
        	int card = deck[deal--];
    	dealerHand = card;
        	printf("\nCard Dealt: %d\n", dealerHand);
    	dealerHandTotal += dealerHand; 
     
     
         
        	return 0;
    }
    
    
    int dealCardPlayer ()
    {    
        
        	int card = deck[deal--];
    	playerHand = card;
        	printf("\nCard Dealt: %d\n", playerHand);
    	playerHandTotal += playerHand;
     
     
         
       	 return 0;
    }
    
    
    int playerChoice ()
    {
    	char choice;
    	do
    	{
    		choice = 0;
    		while (choice < 1)
    		{	
    			printf("Would you like to hit (H) or stand (S)?");
    			scanf(" %c", &choice);
    		}
    		
    		switch(choice)
    		{
    			case 'H':
    			dealCardPlayer ();
    			printf("Player has %d", playerHandTotal);
    			break;
    			case 'S':
    			break;
    			default:
    			printf("\nINVALID SELCETION\n");
    		}
    	}
    	while (choice != 'S');
    	printf("Player has %d", playerHandTotal);
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with Card Dealing Program
    By Izzy123 in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2011, 01:39 PM
  2. Need help with a card dealing program using classes
    By nick753 in forum C++ Programming
    Replies: 25
    Last Post: 11-24-2010, 10:25 PM
  3. Card Dealing Problem
    By curlious in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2003, 06:47 PM
  4. Card Dealing program help
    By Randoon in forum C Programming
    Replies: 1
    Last Post: 11-13-2002, 08:27 PM
  5. card shuffling dealing question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2002, 08:37 PM