forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiceThrower.java
More file actions
113 lines (98 loc) · 3.51 KB
/
DiceThrower.java
File metadata and controls
113 lines (98 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.thealgorithms.recursion;
import java.util.ArrayList;
import java.util.List;
/**
* DiceThrower - Generates all possible dice roll combinations that sum to a target
*
* This algorithm uses recursive backtracking to find all combinations of dice rolls
* (faces 1-6) that sum to a given target value.
*
* Example: If target = 4, possible combinations include:
* - "1111" (1+1+1+1 = 4)
* - "13" (1+3 = 4)
* - "22" (2+2 = 4)
* - "4" (4 = 4)
*
* @author BEASTSHRIRAM
* @see <a href="https://en.wikipedia.org/wiki/Backtracking">Backtracking Algorithm</a>
*/
public final class DiceThrower {
private DiceThrower() {
// Utility class
}
/**
* Returns all possible dice roll combinations that sum to the target
*
* @param target the target sum to achieve with dice rolls
* @return list of all possible combinations as strings
*/
public static List<String> getDiceCombinations(int target) {
if (target < 0) {
throw new IllegalArgumentException("Target must be non-negative");
}
return generateCombinations("", target);
}
/**
* Prints all possible dice roll combinations that sum to the target
*
* @param target the target sum to achieve with dice rolls
*/
public static void printDiceCombinations(int target) {
if (target < 0) {
throw new IllegalArgumentException("Target must be non-negative");
}
printCombinations("", target);
}
/**
* Recursive helper method to generate all combinations
*
* @param current the current combination being built
* @param remaining the remaining sum needed
* @return list of all combinations from this state
*/
private static List<String> generateCombinations(String current, int remaining) {
List<String> combinations = new ArrayList<>();
// Base case: if remaining sum is 0, we found a valid combination
if (remaining == 0) {
combinations.add(current);
return combinations;
}
// Try all possible dice faces (1-6), but not more than remaining sum
for (int face = 1; face <= 6 && face <= remaining; face++) {
List<String> subCombinations = generateCombinations(current + face, remaining - face);
combinations.addAll(subCombinations);
}
return combinations;
}
/**
* Recursive helper method to print all combinations
*
* @param current the current combination being built
* @param remaining the remaining sum needed
*/
private static void printCombinations(String current, int remaining) {
// Base case: if remaining sum is 0, we found a valid combination
if (remaining == 0) {
System.out.println(current);
return;
}
// Try all possible dice faces (1-6), but not more than remaining sum
for (int face = 1; face <= 6 && face <= remaining; face++) {
printCombinations(current + face, remaining - face);
}
}
/**
* Demo method to show usage
*
* @param args command line arguments
*/
public static void main(String[] args) {
int target = 4;
System.out.println("All dice combinations that sum to " + target + ":");
List<String> combinations = getDiceCombinations(target);
for (String combination : combinations) {
System.out.println(combination);
}
System.out.println("\nTotal combinations: " + combinations.size());
}
}