Example #1
0
  public static void main(String[] args) {
    card();
    hands();

    Scanner userInitiate = new Scanner(System.in);
    int userAmount = 26;
    String war = userInitiate.nextLine();
    System.out.println("Hello! Welcome to the game of war. Please type 'war' to start a round");

    boolean keepPlaying = true;
    do {

      war = war.toLowerCase();
      for (int i = 0; i < userHand.size(); i++) {
        System.out.println(
            "Your card is the "
                + userHand.get(i).getRank()
                + "of "
                + userHand.get(i).getSuit()
                + ". ");
        System.out.println(
            "Your opponents card is "
                + computerHand.get(i).getRank()
                + "of"
                + computerHand.get(i).getSuit()
                + ". ");

        if (userHand.get(i).getValue() > computerHand.get(i).getValue()) {
          userAmount++;
          userHand.add(computerHand.get(i));
        }
        if (userHand.get(i).getValue() < computerHand.get(i).getValue()) {
          userAmount--;
          computerHand.add(userHand.get(i));
        }
        if (userHand.get(i).getValue() == computerHand.get(i).getValue()) {
          if (userHand.get(i + 1).getValue() > computerHand.get(i + 1).getValue()) {
            userAmount++;
            userHand.add(computerHand.get(i));
          }
          if (userHand.get(i + 1).getValue() < computerHand.get(i + 1).getValue()) {
            userAmount--;
          }
        }
        System.out.println("You have " + userAmount + " cards.");
      }
      if (war.equals("q")) {
        keepPlaying = false;
      }

      if (userAmount == 52) {
        System.out.println("Congratulations you won!");
      } else if (userAmount == 0) {
        System.out.println("Sorry you lost.");
      }

    } while (war.equals("war") && userAmount > 0 && userAmount != 52);
  }