Blackjack program, having problems with the ace

This is a discussion on Blackjack program, having problems with the ace within the C Programming forums, part of the General Programming Boards category; The program works fine instead for the ace.. I am going to post partial parts of the program here so ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    33

    Blackjack program, having problems with the ace

    The program works fine instead for the ace.. I am going to post partial parts of the program here so you get the main idea how the program works. If you need the full code I will PM it to you...

    Now as you know (or may not) in blackjack ace can be counted as 1 or 11 according to your total score. If your total score, with the ace dealt, is smaller than 21 it is counted as 11, if it is bigger than 21 it is counted as 1.

    Code:
    #define CARD_VALUES 13
    
    void ShuffleDeck(void);
    int DealTwoCards(void);
    int DealCard(void);
    int PlayersTurn(int);
    int DealersTurn(int);
    
    int aces, twos, threes, fours, fives, sixes, sevens, eights, nines, tens, jacks, queens, kings;
    int initPlayerScore, initDealerScore, newPlayerScore, newDealerScore;
    I am aware I have to change card_values 13 to card_values 14...

    Code:
    void ShuffleDeck(void)
    {
            aces = 4;     twos = 4;      threes = 4;
    	    fours = 4;    fives = 4;     sixes = 4;  
            sevens = 4;   eights = 4;    nines = 4;
    	    tens = 4;     jacks = 4;     queens = 4;
    	    kings = 4;
    }
    
    
    int DealTwoCards(void)
    {
            int addedScore;
    
            addedScore = DealCard() + DealCard();
            
    	return addedScore;
    }
    no problem here..

    Code:
    int DealCard()
    {
            int card, cardValue;
                    
            card = 1 + (int) rand() % CARD_VALUES;
    
                    
    		if (card == 1)
    		{
         		cardValue = 1;
    			printf("\t dealt an ace\n");
    			aces--;
               	
    		}
    		else if (card == 2)			
    		{
    		 	cardValue = 2;
    			printf("\t dealt a two\n");
    			twos--;
    		}
    .
    .
    .
    .
    return cardValue;
    }

    So this is how I have written it.. I hope it is understandable..
    How on the hell am I going to make ace take two values..??

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    DealCard() should return a card, not a value. In other words, it should return ACE, or KING, etc. You calculate the value based on what the user has in their hand. Your functions should look something like:

    Code:
    typedef int CARD;
    CARD deck[52]; /* 52 cards in a deck */
    struct PLAYER_HAND
    {
        CARD cards[MAX_CARDS_PER_HAND];
        int nCardsInHand;
    };
    
    CARD DealCard(void)
    {
        rand_val = 0 to 51;
        CARD card = deck[rand_val];
        deck[rand_val] = 0; /* make zero to indicate the card has been dealt, but is there a better way of doing this...? */
        return card;
    }
    
    int GetHandValue(struct PLAYER_HAND*);
    This is by no means complete code, but hopefully it gets you thinking in the right direction.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    any other suggestions ?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not quite sure I understand what the problem you are trying to solve is... But I can assure you that your approach using a count of each value of card seems very awkward.

    Using a structure like suggested above seems much better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    I am trying to solve the issue with the ace.. The program works fine but I need ace to have two values, instead of one

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean, when you sum up the cards in the hand, choose between 1 or 11? You do that in the "sumUpHand" function, which would do the appropriate checking of the current hands value, and then use 11 or 1 accordingly (add all non-ace values first!)


    --
    Mats
    Last edited by matsp; 05-07-2009 at 06:33 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    yes that is what I mean.. I'll get working on it thank you..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 02:00 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. having problems with my card program
    By mac025 in forum C Programming
    Replies: 4
    Last Post: 01-31-2006, 04:26 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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