Search code examples
javaarrayscombinationsdice

combinations of an array of array members java ( dice roll? )


really sorry for not writing where i am at, as i have no clue how to start and had no luck searching.

for example if i have

int [][]={{1,2,3},              //possible states of 1 member
          {10,20,30},           //possible states of 2 member
          {100,200,300}}        //possible states of 3 member

I need to define k - number of combination member arrays, and then get the result in the way, that i get all possible combinations of their states. So basically if the k is 2:

member(1st array)1 - member2
member1 - member3 
member2 - member3

Then get combinations of all possible states of those members, that members cannot combine with each other.

You can imagine having 3 dices (in this case all 3 sided ) and i want to get all possible combinations i get get by throwing all possible pairs of dices (dice1 + dice2 is the same as dice2 + dice1, so i don't want that). I don't know how many dices i will have and how many sides i will have.

Any pointers, starting advice or anything is much appreciated, thank you


Solution

  • To start with, you can do something like this. Just a solution, not so great in time complexity.

        for(int i=0; i < x.length; i++){  // Selects one dice
            for(int j = i + 1; j < x.length; j++){  // Select another dice
                for(int k=0; k<x[i].length; k++){  // selects face of first dice
                    for(int l=0; l<x[j].length; l++){  // select face of 2nd dice
                        System.out.print("Dice " + i + " Face " + k + " vs Dice " + j + " Face " + l);
                        System.out.println(" ==> " + x[i][k]+ " - " + x[j][l]);
                    }
                }
            }
        }