Recursion leading to additional unwanted data

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){
//          System.out.print("\n(looping on "+index+")\n");
            for(int k=1;k<=3;k++){
//              System.out.print("setting i"+index+" to "+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){
//          System.out.print("\n(looping on "+index+")\n");
            for(int k=1;k<=3;k++){
//              System.out.print("setting i"+index+" to "+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();
    }
}
+3
3

, , , , :

public class DiceRolls {
    static void recurse(int diceNumber, int[] values, final int MAX) {
        if (diceNumber == values.length) {
            System.out.println(java.util.Arrays.toString(values));
        } else {
            for (int v = 1; v <= MAX; v++) {
                values[diceNumber] = v;
                recurse(diceNumber + 1, values, MAX);
            }
        }
    }
    public static void main(String[] args) {
        recurse(0, new int[3], 4);
    }
}

. int[] List, , add(values.clone()), int[].


?

, , . , :

if we're not done yet
    trying all possibilities for this dice
       dump result so far // premature dumping!
       recurse for next dice

:

if we're not done yet
    trying all possibilities for this dice
       recurse for next dice
else, we're done, so
    dump result // timely!

, Java, dump(set); else if (index < 3).

+2

dump() index == 2.

, i list . "".:)

+1

. , .

package utils;

public class Dice {
    private static int FACES = 3;
    private static int NUMBER_OF_DICE = 3;

    public static void main(String[] args) {
        int start = createPair(1);
        int end = createPair(FACES);
        for (int i = start; i <= end; i++) {
            String combination = Integer.toString(i, FACES+1);
            if (combination.indexOf('0') < 0)
                System.out.println(combination);
        }
    }

    private static int createPair(int number) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < NUMBER_OF_DICE; i++) {
            sb.append(number);
        }
        return Integer.parseInt(sb.toString(), FACES+1);
    }
}
+1
source

Source: https://habr.com/ru/post/1748898/


All Articles