/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

import java.util.*;
import java.math.*;
import java.lang.reflect.Array;

interface Rollable
{
    public int roll();
}

public class Main
{
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        diceRoll d  = new diceRoll(6,20);
        
    }
}
class diceRoll implements Rollable
{
    Set <String> s  = new HashSet<>();
    Map<Integer, String> m = new HashMap<>();
    
    private int numberDice;
    private int total;
    private int totalRolled;
    
    public int roll()
    {
        
        int[] diceValue = new int[numberDice];
        
        int count;
        
        Random rnd = new Random();
        
        int numberDiceRolls=250000;
        
        System.out.println("\nNumber dice rolls: " + numberDiceRolls + " for    " + numberDice + " dice");
        System.out.println("Total: " + total);
        
        for (int i=0;i<numberDiceRolls;i++)  // this is being used since not sure how to code for permutation and combination in Java
        //it is done and hoping to ensure all possible are covered
        {
            StringJoiner sj = new StringJoiner(",  ");
            totalRolled=0;
            //System.out.println("\ni is: " + i);
            
            for (int j=0;j<numberDice;j++)
            {
                diceValue[j]=(rnd.nextInt(6)+1);
                totalRolled=totalRolled+diceValue[j];
                
                sj.add("Die " + (j+1)+": " + Integer.toString(diceValue[j]));
                
                //System.out.println("dice roll: " + diceValue[j]);
                
            }
            
            if (totalRolled==total)
            {
                //System.out.println("This is total rolled:" + totalRolled);
                //m.put(i,sj.toString());
                s.add(sj.toString());
            }
            
            
        }
        
        count=0;
        
        //System.out.println("\n");
        
        //for (Map.Entry<Integer, String> e : m.entrySet())
        for (String m: s)
        {
            // Printing key-value pairs
            System.out.println("\n"+ m);
            //System.out.println(e.getKey() + " "
              //                 + e.getValue() + "\n");
                               
                               count++;
        
        
            
                          
        }
        
    System.out.println("\nTotal number possibilites: " + count);
    
    return count;
    
    }
    
    public diceRoll(int numberDice, int total)
    {
        this.numberDice=numberDice;
        this.total=total;
        
        if (total<numberDice)
        {
            System.out.println("Increase total or reduce number dice");
            System.exit(0);
        }
        
        if (total>(6*numberDice))
        {
            System.out.println("The total is too high for: " + numberDice + " dice");
        }
        
        roll();
        
    }
    
}
