// **************************************************
// Function: blackJackValue
//
// Description: Calculates the blackjack value of two cards
//
// Parameters: card1 - first card (char)
//             card2 - second card (char)
//
// Returns: total hand value
//          -1 if invalid card
//
// **************************************************

#include <stdio.h>

// function prototype
int blackJackValue(char card1, char card2);

int main()
{
    char card1, card2;
    int result;

    // prompt user for cards
    printf("Enter first card (2-9, T, J, Q, K, A): ");
    scanf(" %c", &card1);

    printf("Enter second card (2-9, T, J, Q, K, A): ");
    scanf(" %c", &card2);

    // call function
    result = blackJackValue(card1, card2);

    // check for invalid result
    if(result != -1)
    {
        printf("\nFinal Hand Value: %d\n", result);
    }

    return 0;
}

int blackJackValue(char card1, char card2)
{
    int handValue = 0;   // MUST initialize
    char hand[2] = {card1, card2};

    // check for two Aces
    if(hand[0] == 'A' && hand[1] == 'A')
    {
        handValue = 12;
        printf("\nThe score is %d", handValue);
        return handValue;
    }

    // loop to determine hand value
    for(int i = 0; i < 2; ++i)
    {
        // number cards
        if(hand[i] >= '2' && hand[i] <= '9')
        {
            handValue += hand[i] - '0';
        }

        // face cards
        else if(hand[i] == 'T' || hand[i] == 'J' || hand[i] == 'Q' || hand[i] == 'K')
        {
            handValue += 10;
        }

        // Ace
        else if(hand[i] == 'A')
        {
            handValue += 11;
        }

        // invalid
        else
        {
            printf("\nInvalid card");
            return -1;
        }
    }

    printf("\nThe score is %d", handValue);
    return handValue;
}