#include <stdio.h>
int blackjack_hand_value(char card1, char card2) {
/**
* Calculate the point total of a two-card blackjack hand.
*
* Parameters:
* - card1: The first card in the hand ('2'-'9', 'T', 'K', 'Q', 'J', 'A').
* - card2: The second card in the hand ('2'-'9', 'T', 'K', 'Q', 'J', 'A').
*
* Returns:
* - int: The point total of the hand.
* - -1: If invalid input is provided.
*/
// Helper function to get the value of a single card
int get_card_value(char card) {
// Convert to uppercase for consistency
if (card >= '2' && card <= '9') {
return card - '0'; // Convert char '2'-'9' to integer 2-9
} else if (card == 'T' || card == 'K' || card == 'Q' || card == 'J') {
return 10; // Face cards are worth 10 points
} else if (card == 'A') {
return 11; // Aces are worth 11 points by default
} else {
return -1; // Invalid card
}
}
// Get the values of the two cards
int value1 = get_card_value(card1);
int value2 = get_card_value(card2);
int total;
// Check for invalid input
if (value1 == -1 || value2 == -1)
{
return -1; // Return -1 to indicate an error
}
// Adjust for multiple aces (if both cards are aces)
if (value1 == 'A' || 'a' && value2 == 'A' || 'a')
{
total = 12; // One ace is worth 11, the other is worth 1
}
// Calculate the total value of the hand
total = value1 + value2;
return total;
}
int main() {
// Example usage
char card1, card2;
printf("Enter the first card (2-9, T, K, Q, J, A): ");
printf("Enter the second card (2-9, T, K, Q, J, A): ");
int total = blackjack_hand_value(card1, card2);
if (total == -1) {
printf("Invalid card input. Please enter valid cards.\n"); } else {
printf("The total value of the hand is: %d\n", total
); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CmludCBibGFja2phY2tfaGFuZF92YWx1ZShjaGFyIGNhcmQxLCBjaGFyIGNhcmQyKSB7CiAgICAvKioKICAgICAqIENhbGN1bGF0ZSB0aGUgcG9pbnQgdG90YWwgb2YgYSB0d28tY2FyZCBibGFja2phY2sgaGFuZC4KICAgICAqCiAgICAgKiBQYXJhbWV0ZXJzOgogICAgICogLSBjYXJkMTogVGhlIGZpcnN0IGNhcmQgaW4gdGhlIGhhbmQgKCcyJy0nOScsICdUJywgJ0snLCAnUScsICdKJywg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IgdmFsaWQgY2FyZHMuXG4iKTsKICAgIH0gZWxzZSB7CiAgICAgICAgcHJpbnRmKCJUaGUgdG90YWwgdmFsdWUgb2YgdGhlIGhhbmQgaXM6ICVkXG4iLCB0b3RhbCk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0=
Enter the first card (2-9, T, K, Q, J, A): Enter the second card (2-9, T, K, Q, J, A): Invalid card input. Please enter valid cards.