Last active
August 22, 2021 15:49
-
-
Save aaiezza/d84dd2104ddea7e7cfaa7266635ef244 to your computer and use it in GitHub Desktop.
Produce combinations of dice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Author
aaiezza
commented
Aug 22, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment