# Blackjack program, having problems with the ace

• 05-06-2009
yigster
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..??
• 05-06-2009
bithub
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.
• 05-07-2009
yigster
any other suggestions ?
• 05-07-2009
matsp
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
• 05-07-2009
yigster
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
• 05-07-2009
matsp
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
• 05-07-2009
yigster
yes that is what I mean.. I'll get working on it thank you..