#include <stdio.h>
#include <ctype.h>
// Function to get the value of a single card
int get_card_value(char card) {
card
= toupper(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
}
}
// Function to calculate the total value of a two-card blackjack hand
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.
*/
// Get the values of the two cards
{
int value1 = get_card_value(card1);
int value2 = get_card_value(card2);
// Check for invalid input
if (value1 == -1 || value2 == -1) {
return -1; // Return -1 to indicate an error
}
// Calculate the total value of the hand
int total = value1 + value2;
if (total == 22)
{
total = 11;
}
// Adjust for multiple aces (if both cards are aces)
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+CiNpbmNsdWRlIDxjdHlwZS5oPgoKLy8gRnVuY3Rpb24gdG8gZ2V0IHRoZSB2YWx1ZSBvZiBhIHNpbmdsZSBjYXJkCmludCBnZXRfY2FyZF92YWx1ZShjaGFyIGNhcmQpIHsKICAgIGNhcmQgPSB0b3VwcGVyKGNhcmQpOyAvLyBDb252ZXJ0IHRvIHVwcGVyY2FzZSBmb3IgY29uc2lzdGVuY3kKICAgIGlmIChjYXJkID49ICcyJyAmJiBjYXJkIDw9ICc5JykgewogICAgICAg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gfSBlbHNlIHsKICAgICAgICBwcmludGYoIlRoZSB0b3RhbCB2YWx1ZSBvZiB0aGUgaGFuZCBpczogJWRcbiIsIHRvdGFsKTsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=
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.