I am writing a module for processing cubes. Given the x die from the y sides, I'm trying to make a list of all possible roll combinations.
This code assumes 3 die, each with 3 sides, labeled 1, 2 and 3. (I understand that I am using "magic numbers", but this is just an attempt to simplify and make the base code work.)
int[] set = { 1, 1, 1 };
list = diceroll.recurse(0,0, list, set);
...
public ArrayList<Integer> recurse(int index, int i, ArrayList<Integer> list, int[] set){
if(index < 3){
for(int k=1;k<=3;k++){
set[index] = k;
dump(set);
recurse(index+1, i, list, set);
}
}
return list;
}
(dump () is a simple method for simply displaying the contents of a list []. Currently, the variable i is not used.)
What I'm trying to do is increase the index by one, going along the entire length of the list and increasing as we move.
This is my "best try" code. Here is the result:
- , . , . ( , . , x y .)
[1] [1] [1] [1] [1] [1]
[1] [1] [1] [1] [1] [2] [1] [1] [3] [1] [2] [3]
[1] [2] [1] [1] [2] [2] [1] [2] [3] [1] [3] [3]
[1] [3] [1] [1] [3] [2] [1] [3] [3] [2] [3] [3] [2] [ 1] [3]
<2 > [2] [1] [1] [2][2] [2] [1] [2] [2] [2] [2] [2] [3] [2] [3] [3]
<3 > [3] [3] [3] [3] 1] [3][3] [1] [1] [3] [1] [2] [3] [1] [3] [3] [2] [3]
[3] [2] [1] [3] [2] [2] [3] [2] [3] [3] [3] [3]
<3 > [3] [3] [3]
, .
. ( - , .:)
edit: , , - .
edit2:
, " ". , .
package code.testing;
import java.util.ArrayList;
public class CodeTesting {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
int[] set = { 1, 1, 1 };
list = recurse(0,0, list, set);
}
public static ArrayList<Integer> recurse(int index, int i, ArrayList<Integer> list, int[] set){
if(index < 3){
for(int k=1;k<=3;k++){
set[index] = k;
if (index==2){
dump(set);
}
recurse(index+1, i, list, set);
}
}
return list;
}
static void dump(int[] arr) {
for (int s : arr) {
System.out.format("[%s]", s);
}
System.out.println();
}
}