import java.util.Scanner;
import java.util.Random;
class War {
 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  Random r = new Random();
  int moneyAvailable = 50;
  int userBet = 0;
  int gameState = 0; // 0 = first round, 1 = play another round, 2 = exiting with money, 3 = played at least one game
  System.out.println("\n" + "~~~~~~~~ Welcome To WAR ~~~~~~~~");
  System.out.print("\n" + "Please enter your name: ");
  String userName = in .next();
  System.out.println("\n" + "\n" + "Welcome " + userName + ", your starting amount is " + moneyAvailable);
  while (moneyAvailable > 0) { // checking if there is money available to bet
   if (gameState == 3) { // checking if a game was done before
    System.out.println("\n" + "What would you like to do?");
    System.out.println("1. Play another round");
    System.out.println("2. Leave with the money");
    gameState = in .nextInt();
    if (gameState == 1) { // continuing game
     System.out.println("\n" + "OK let's continue!" + "\n");
     userBet = 0; // resetting bet to 0
     continue;
    } else if (gameState == 2) { // leaving game with the money
     break;
    }
   } else {
    while (((userBet > moneyAvailable) || (userBet < 1))) {
     System.out.print("Place your bet for the next round: ");
     userBet = in .nextInt();
     System.out.println("\n" + "Your bet is " + userBet);
     if (((userBet > moneyAvailable) || (userBet < 1))) { // invalid bet, exiting
      System.out.println("Your bet is not valid, it should be between 1 and " + moneyAvailable);
      System.exit(0);
     }
    }
    int userCard = r.nextInt(12) + 1;
    int computerCard = r.nextInt(12) + 1;
    System.out.println("Your card is " + userCard);
    System.out.println("Computer's card is " + computerCard + "\n");
    if (userCard < computerCard) {
     System.out.println("You lost");
     moneyAvailable = moneyAvailable - userBet;
    } else if (userCard == computerCard) {
     System.out.println("It's a draw!");
    } else {
     System.out.println("You won");
     moneyAvailable = moneyAvailable + userBet;
    }
    System.out.println("\n" + "You have " + moneyAvailable + " USD left");
   }
   gameState = 3; // rebooting game
  }
  System.out.println("Bye bye!"); // exit message!
 }
}