Anzei Kazumi
Anzei Kazumi

Reputation: 11

C Programming integer

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();
    }
}

Upvotes: 0

Views: 83

Answers (1)

chqrlie
chqrlie

Reputation: 144780

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.

Upvotes: 1

Related Questions