// **************************************************
// 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;
    char hand[2] = {card1, card2};

    //check for two Aces
    if(hand[0] =='A' && hand[1] =='A')
    {
         handValue = 12;

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

              //Assigns value of tens and face cards as 10 
              else if(hand[i] =='T'||hand[i] =='J'||hand[i] =='Q'||hand[i] =='K')
              {
                  handValue += 10;
              }// else if
    
              //Assigns value of Aces as 11
              else if(hand[i] =='A')
              {
                  handValue += 11;
              }//else if

              //catches invalid entries and returns -1 and error message
              else
              {
                   //error message for invalid entry and returns -1
                   printf("\nInvalid card");
                   return -1;
              }//else
     
         }//for
    }//else

    //message to display score
    printf("\nThe score is %d", handValue);

    //if hand is valid returns point value
    return handValue;

}//blackJackValue
