// Allison Obourn
// CS& 141, Winter 2021
// Lecture 8

// This program prompts the user to answer 2-5 number long addition math problems
// that include the numbers 1 - 10 until the user gets 3 wrong. The user
// gets told what they should have gotten when they are wrong.
// After 3 wrong answers it prints out the total number of correct answers.

import java.util.*;

public class AddingGame {
   public static void main(String[] args) {
      int wrong = 0;
      int points = 0;
      while(wrong < 3) {
         int correctAnswer = generateEquation();
         
         Scanner console = new Scanner(System.in);
         int answer = console.nextInt();
         
         if(correctAnswer != answer) {
            System.out.println("Wrong! The answer was " + correctAnswer);
            wrong++;
         } else {
            points++;
         }
      }
      System.out.println("You earned " + points + " points");
   }  
   
   // generates and prints out an equation of a random number
   // of operands between 2 and 5 numbers added together
   // prints these out with an = at the end
   // and returns the expected answer
   public static int generateEquation() {
      Random rand = new Random();
      int operandCount = rand.nextInt(4) + 2;
      int correctAnswer = 0;
      for(int i = 0; i < operandCount - 1; i++) {
         int operand = rand.nextInt(10) + 1;
         System.out.print(operand + " + ");
         correctAnswer += operand;
      }
      int operand = rand.nextInt(10) + 1;
      correctAnswer += operand;
      System.out.print(operand + " = ");
      return correctAnswer;
   }
   
}