import java.util.Random;
import java.util.Scanner;

/**
 * CS 149, Spring 2017, PA4: Math Dice Driver (Loops and Arrays).
 * Based on http://www.thinkfun.com/products/math-dice/
 *
 * @author Alvin Chao
 * @version 02/21/2017
 */
public class MathDiceDriver {
    
    /**
     * This driver will generate a Math Dice problem of the week using
     * the MathDice class and test to make sure it is solvable.
     *
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        // roll target die
        System.out.println("Rolling target die.");
        Random random = new Random(System.nanoTime());
        int target = random.nextInt(20) + 1;
        System.out.println("Target = " + target);
        
        // get solution dice
        System.out.print("How many solution dice are you using? ");
        int solutionDiceNum = input.nextInt();
        int[] solutionDice = MathDice.rollDice(solutionDiceNum,
                                               System.nanoTime());
        
        // Test examples:
        //   target = 8, 3 dice 5,6,3 - solution 5+6-3
        //   target = 3, 3 dice 4,5,6 - solution 4-6+5
        // Forced solutionDice:
        //   solutionDice[0] = 5;
        //   solutionDice[1] = 6;
        //   solutionDice[2] = 3;
        
        // display the dice
        System.out.print("Dice: [ ");
        for (int die: solutionDice) {
            System.out.print(die + " ");
        }
        System.out.println("]");
        
        // find a solution
        int[] operArray = MathDice.solve(target, solutionDice);
        if (operArray != null && operArray.length != 0) {
            System.out.print("Operators are: [ ");
            for (int j = 0; j < operArray.length; j++) {
                String opName = "";
                switch (operArray[j]) {
                    case MathDice.PLUS:
                        opName = "+";
                        break;
                    case MathDice.MINUS:
                        opName = "-";
                        break;
                    case MathDice.TIMES:
                        opName = "*";
                        break;
                    case MathDice.DIVIDE:
                        opName = "/";
                        break;
                    case MathDice.MODULO:
                        opName = "%";
                        break;
                    default:
                        opName = "INVALID";
                }
                System.out.print(opName + " ");
            }
            System.out.println("]");
        }
        
        // print the solution
        if (operArray == null || operArray.length == 0) {
            System.out.println("Solution is not reachable.");
        } else {
            String str = "" + solutionDice[0];
            for (int i = 0; i < operArray.length; i++) {
                if (operArray[i] == MathDice.PLUS) {
                    str += " + " + solutionDice[i + 1];
                }
                if (operArray[i] == MathDice.MINUS) {
                    str += " - " + solutionDice[i + 1];
                }
                if (operArray[i] == MathDice.TIMES) {
                    str += " * " + solutionDice[i + 1];
                }
                if (operArray[i] == MathDice.DIVIDE) {
                    str += " / " + solutionDice[i + 1];
                }
                if (operArray[i] == MathDice.MODULO) {
                    str += " % " + solutionDice[i + 1];
                }
            }
            str += " = " + target;
            System.out.println("Solution is: " + str);
        }
        
        // show the difficulty
        double level = MathDice.difficulty(solutionDice);
        System.out.printf("The difficulty of this problem is: %.2f\n", level);
    }
    
}
