// This program solves the dice permutation problem import java.util.*; public class Dice { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("How many dice do you want to roll? "); int num = console.nextInt(); System.out.println(); permuteDice(num); } // print out all possible permutations of num number of dice // [1, 2] is different than [2, 1] // // roll one dice, permuteDice(num - 1) // // what we need: // where are we going? (num) // where have we been (not represented here...) public static void permuteDice(int num) { // solution goes here permuteDice(num, new ArrayList()); } // where are we going? (num) // where have we been (not represented here...) // -some number of already rolled dice private static void permuteDice(int num, List chosen) { if (num == 0) { // base case, with solution if (sum(chosen) == 9) { System.out.println(chosen); } } else if (sum(chosen) < 9) { for (int i = 1; i <= 6; i++) { // choose all numbers 1 - 6 chosen.add(i); // explore permuteDice(num - 1, chosen); // unchoose chosen.remove(chosen.size() - 1); } } } private static int sum(List list) { int sum = 0; for (int num : list) { sum += num; } return sum; } }