/*          Blackjack.java  -  CS 111A  -  Craig Persiko
            This program will score a hand of Blackjack.

Input:      User inputs number of cards along with the
            value of each card.

Output:     The program will calculate the total of each
            hand. For cases with aces, the best combination
            will be used.
*/

import java.util.Scanner;


class Blackjack
{
  static int total = 0; // static member variables (fields)

                       // for all static methods to share

  static Scanner keyIn = new Scanner(System.in);

  public static void main(String args[])
  {
    String again;

    System.out.println("Welcome to the Blackjack Counter");

    do
    {
      get_input();

      if (total > 21)
        System.out.println("You BUSTED with " + total + "!");
      else
        System.out.println("Your hand is: " + total);

      System.out.print("Do you wish to continue? ");
      again  = keyIn.nextLine();
    } while (Character.toLowerCase(again.charAt(0)) == 'y');

  }

  //Request input from user and set total value

  // in "total" instance variable

  static void get_input()
  {
    int num_cards=0, card_no;
    int ace_count=0;
    total = 0;

    do
    {
      System.out.print("How many cards do you have? ");
      num_cards = keyIn.nextInt();
      keyIn.nextLine(); // dispose of newline

    }while(!verifyCardNum(num_cards));

    for (card_no=1; card_no <= num_cards; card_no ++)
    {
      ace_count += get_card(card_no);
    }

    if(total <= 11 && ace_count > 0)
      //If under 21 and has aces, change an ace to count as 11 points

      total += 10;
  }

  // Show error and return false if invalid # of cards

  static boolean verifyCardNum(int num)
  {
    if(num < 2)
    {
      System.out.println("You must have at least 2 cards");
      return false;
    }
    else
      return true;
  }

  // get one card's value, adding it to total (instance variable)

  // returns 1 if card is an ace, 0 otherwise.

  static int get_card(int card_num)
  {
    String card_val;

    do
    {
      System.out.print("Enter value of card # " + card_num + ": ");
      card_val = keyIn.nextLine();

      switch (Character.toLowerCase(card_val.charAt(0)))
      {
       case 'a':
        total += 1; // count ace as 1 for now; later it may change

        return 1;

       case 'k': case 'q': case 'j':
        total += 10;
        return 0;

       case '1': case '2': case '3':
       case '4': case '5': case '6':
       case '7': case '8': case '9':
        int value = Integer.parseInt(card_val);
        if (value < 2 || value > 10)
          System.out.println(
            "Please enter a number between 2 and 10, ace, jack, queen, or king.");
        else
        {
          total += value;
          return 0;
        }
        break;

       default:
        System.out.println("Not a valid entry. Please try again.");
      }
    }while (true); // only way to exit loop is to return from function


    // return 0;

    // above line might have been required if the compiler said "return" missing.

  }
}

/*  Sample Output:

Welcome to the Blackjack Counter
How many cards do you have? 2
Enter value of card # 1: Queen
Enter value of card # 2: Ace
Your hand is: 21
Do you wish to continue? yes
How many cards do you have? 3
Enter value of card # 1: 10
Enter value of card # 2: king
Enter value of card # 3: ace
Your hand is: 21
Do you wish to continue? y
How many cards do you have? 5
Enter value of card # 1: 1
Please enter a number between 2 and 10, ace, jack, queen, or king.
Enter value of card # 1: high
Not a valid entry. Please try again.
Enter value of card # 1: ace
Enter value of card # 2: ace
Enter value of card # 3: 6
Enter value of card # 4: ace
Enter value of card # 5: ace
Your hand is: 20
Do you wish to continue? y
How many cards do you have? 1
You must have at least 2 cards
How many cards do you have? 3
Enter value of card # 1: 7
Enter value of card # 2: 6
Enter value of card # 3: 9
You BUSTED with 22!
Do you wish to continue? n

*/


syntax highlighted by Code2HTML, v. 0.9