import chapman.io.*;

// The status of the player
class PlayerStat {
  // Add one more card to the status
  public void add(PlayingCard card) {
    if (card.getValue() < 10)
      min_point += card.getValue();
    else if (card.getValue() != 14)
      min_point += 10;
    else {
      min_point++;
      has_ace = true;
    }
  }
  // Find the number of points of a particular status
  public int point() {
    if (min_point > 11 || ! has_ace)
      return min_point;
    else
      return 10+min_point;
  }
  private int min_point;
  private boolean has_ace;
}

// Play BlackJack
public class BlackJack {
  // Main engine
  public static void main(String[] args) {
    giveCard(dealer);
    playerTurn();
    if (player.point() > 21)
      System.out.println("You lose!");
    else {
      while (dealer.point() < 16)
        giveCard(dealer);
      System.out.println("I get " + dealer.point() + " points");
      System.out.println("You get " + player.point() + " points");
      if (dealer.point() > 21 || dealer.point() < player.point())
        System.out.println("You win");
      else
        System.out.println("I win");
    }
  }
  // Allow player to make decisions
  static void playerTurn() {
    StdIn in = new StdIn();
    giveCard(player);
    giveCard(player);
    System.out.print("Want more card? ");
    while (Character.toUpperCase(in.readChar()) == 'Y') {
      giveCard(player);
      if (player.point() >= 21)
        return;
      System.out.print("Want even more card? ");
    }
  }
  // Give a card to a player, show a message telling what he get
  static void giveCard(PlayerStat who) {
    if (who == player)
      System.out.print("You");
    else
      System.out.print("I");
    PlayingCard card = deck.getCard();
    System.out.println(" get " + card.toString());
    who.add(card);
  }
  static RandomDeck deck = new RandomDeck();
  static PlayerStat player = new PlayerStat(), dealer = new PlayerStat();
}

