Skip to content

Instantly share code, notes, and snippets.

@aaiezza
Last active August 22, 2021 15:49
Show Gist options
  • Save aaiezza/d84dd2104ddea7e7cfaa7266635ef244 to your computer and use it in GitHub Desktop.
Save aaiezza/d84dd2104ddea7e7cfaa7266635ef244 to your computer and use it in GitHub Desktop.
Produce combinations of dice
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CombinationsOfDice {
public List<int[]> execute(final int numberOfDice, final int numberOfSidesPerDice) {
final List<int[]> list = new ArrayList<>();
final int[] combination = new int[numberOfDice];
int index;
Arrays.fill(combination, 1);
index = numberOfDice - 1;
combination[index] = 0;
while (true) {
if (combination[index] == numberOfSidesPerDice) {
index--;
if (index < 0) break;
} else {
combination[index]++;
while (index < numberOfDice - 1) {
index++;
combination[index] = 1;
}
list.add(Arrays.copyOf(combination, numberOfDice));
}
}
return list;
}
}
@aaiezza
Copy link
Author

aaiezza commented Aug 22, 2021

DCE3B695-B63C-4B4C-92EB-EF3B98A614DB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment