fork download
  1. #include <stdio.h>
  2. int blackjack_hand_value(char card1, char card2) {
  3. /**
  4.   * Calculate the point total of a two-card blackjack hand.
  5.   *
  6.   * Parameters:
  7.   * - card1: The first card in the hand ('2'-'9', 'T', 'K', 'Q', 'J', 'A').
  8.   * - card2: The second card in the hand ('2'-'9', 'T', 'K', 'Q', 'J', 'A').
  9.   *
  10.   * Returns:
  11.   * - int: The point total of the hand.
  12.   * - -1: If invalid input is provided.
  13.   */
  14.  
  15. // Helper function to get the value of a single card
  16. int get_card_value(char card) {
  17. // Convert to uppercase for consistency
  18. if (card >= '2' && card <= '9') {
  19. return card - '0'; // Convert char '2'-'9' to integer 2-9
  20. } else if (card == 'T' || card == 'K' || card == 'Q' || card == 'J') {
  21. return 10; // Face cards are worth 10 points
  22. } else if (card == 'A') {
  23. return 11; // Aces are worth 11 points by default
  24. } else {
  25. return -1; // Invalid card
  26. }
  27. }
  28.  
  29. // Get the values of the two cards
  30. int value1 = get_card_value(card1);
  31. int value2 = get_card_value(card2);
  32. int total;
  33.  
  34. // Check for invalid input
  35. if (value1 == -1 || value2 == -1)
  36. {
  37. return -1; // Return -1 to indicate an error
  38. }
  39.  
  40. // Adjust for multiple aces (if both cards are aces)
  41. if (value1 == 'A' || 'a' && value2 == 'A' || 'a')
  42. {
  43. total = 12; // One ace is worth 11, the other is worth 1
  44. }
  45.  
  46. // Calculate the total value of the hand
  47. total = value1 + value2;
  48.  
  49. return total;
  50. }
  51.  
  52. int main() {
  53. // Example usage
  54. char card1, card2;
  55.  
  56. printf("Enter the first card (2-9, T, K, Q, J, A): ");
  57. scanf(" %c", &card1);
  58.  
  59. printf("Enter the second card (2-9, T, K, Q, J, A): ");
  60. scanf(" %c", &card2);
  61.  
  62. int total = blackjack_hand_value(card1, card2);
  63.  
  64. if (total == -1) {
  65. printf("Invalid card input. Please enter valid cards.\n");
  66. } else {
  67. printf("The total value of the hand is: %d\n", total);
  68. }
  69.  
  70. return 0;
  71. }
Success #stdin #stdout 0s 5272KB
stdin
q7
stdout
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.