0

I need to make a blackjack program. The thing is, as you can see in the code, there's a loop for generating cards without repetition..

If I hit a key it'll print a line like: "5-diamond" then hit another key and for example it prints: "8-clover"

So how do I add those two together without messing up with the code. Since I want to check the value of the sum of those two. What do I need to do?

int cards()
{
    int card[51];
    int used[51];

    int x = 0;
    int playerhand = 0;
    int dealerhand = 0;
    int sum = 0;

    while(!kbhit())
        x++;

    srand(x % 100000);

    for(int i = 0; i <= 51; i++)
        used[i] = 0;

    for(;;)
    {
        int w;
        do
        {
            w = rand() % 52;
        }
        while(used[w] == 1);

        used[w] = 1;

        int value = w % 13 + 1;

        if(value >= 2 && value <= 10)
            printf("%d-", value);
        else
        {
            if(value == 1)
                printf("Ace ");
            if(value == 11)
                printf("Jack ");
            if(value == 12)
                printf("Queen ");
            if(value == 13)
                printf("King ");
        }

        int suit = (int)(w / 13);

        if(suit == 0)
            printf("Clover");
        if(suit == 1)
            printf("Spade");
        if(suit == 2)
            printf("Heart");
        if(suit == 3)
            printf("Diamond");

        printf("\n");
        getch();
    }
}
4
  • Explain your desired output by giving examples. Commented Apr 18, 2015 at 8:03
  • 2
    The first for loop should be for(int i=0;i<51;i++), not for(int i=0;i<=51;i++) and w=rand()%52; should be w=rand()%51;. Commented Apr 18, 2015 at 8:19
  • 3
    @Cool Guy: that's correct from a programming standpoint, but as I never saw a blackjack game with only 51 cards, it's probably more appropriate to advise on extending the card arrays to 52 elements and leave the rest as it was... Commented Apr 18, 2015 at 8:38
  • I just need to know how to add two values if the program prints two values from the card randomizer. Like what I wrote as an example. Commented Apr 18, 2015 at 23:52

1 Answer 1

1

You should compute the sum of the values and the count of Aces.

If a card is an Ace, add 11, if it is a King, Queen or Jack, add 10, otherwise add value.

If the sum is greater than 21 and you saw Aces, deduct 10 for each Ace until you fall back below 22.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.