0

I can't seem to get this code to work. What I am doing is populating an array with all possible combinations of 2 digits (each of these digits representing a shape and color, respectively). Then, I am trying to use this array to populate a 2d array in which i create all possible combinations of the elements housed in the previous array. For some reason, my 2d array is filled with all '21' instead of any sort of combination.

I can post the rest of the code in the class if needed, but it's rather long. The final loop in this method is just used to print them for testing and will be deleted afterwards.

public void combinations()
{
    combinations = new int[numShapes*numColors];
    int index = 0;
    for(int l = 1; l <= numShapes; l++)
        for(int h = 1; h <= numColors; h++)
            if(index + 1 != combinations.length + 1)
                combinations[index++] = (l*10) + h;
            else
                break;

    int[][] combs = new int[(int)Math.pow((numShapes*numColors),numPositions)][numPositions];

    //Fills the array with all '21' , fix this
    int ind = 0;
    for(int f = 0; f < 16; f++)
        for(int i = 0; i < numShapes; i++)
            for(int j = 0; j < numColors; j++)
                if(ind != combs.length+1){
                    combs[ind] = new int[]{combinations[numShapes], combinations[numShapes], combinations[numColors]};
                    ind++;
                }
                else
                    break;

    for(int p = 0; p < 2; p++){
        for(int g = 0; g < 3; g++){
            System.out.print(testFormat(combs[p][g]/10, combs[p][g]%10) + " ");
        }
        System.out.println();
    }
}
  • It's a little bit unclear what the combinations should be. `I can't seem to get this code to work. What I am doing is populating an array with all possible combinations of 2 digits`. Do you mean filling an array with 00-99? – Cruncher Sep 07 '13 at 17:17
  • I had the problem once maybe that will help you: http://stackoverflow.com/questions/11343848/java-permutation-of-arraylist-elements-integer-cant-get-it-to-work-proper – Barbe Rouge Sep 07 '13 at 17:19
  • I am programming the game of grand mastermind. For this game, a user writes down a n-length code of shape and color combinations. I am limiting the n to 5 and the number of shapes and colors to 5 as well. I have represent the colors with digits 1-5 and the shapes with digits 1-5. For instance, 11 in my program is 'Circle Red'. I am attempting to populate an array with all possible combinations of color/shape/position given the user input on this. This method is part of a 4 class program, so I didn't want to post a massive wall of text, sorry haha. – user2757330 Sep 07 '13 at 17:22
  • Okay, so your combinations array is going to be: 11, 12, .., 15, 21, ..., 55? – Cruncher Sep 07 '13 at 17:24
  • What exactly does the 2d array need to be? – Cruncher Sep 07 '13 at 17:25
  • Yes, the 1D array will be populated like that. Then the 2D array should contain all the combinations of the elements held within the 1D array. – user2757330 Sep 07 '13 at 17:26
  • @user2757330 How do you intend to "contain all combinations"? The first step, is how do you store a SINGLE combination? You need some kind of object to store both numbers, or do some math like you did for the 1d array. – Cruncher Sep 07 '13 at 17:28
  • Storing a single combination in an array of length numPositions. Then storing each of those arrays within the 2D array. – user2757330 Sep 07 '13 at 17:29
  • To store a single combination you need only 2 numbers, or a 2 element array – Cruncher Sep 07 '13 at 17:31

1 Answers1

0
    int[][] combs = combinationsOf(combinations, 5);

    for (int i = 0 ; i < combs.length ; i++)
    {
        for(int j = 0 ; j < combs[i].length ; j++)
        {
            System.out.print(combs[i][j] + ", ");
        }
        System.out.println("");
    }


public static int[][] combinationsOf(int[] colorShape, int numPositions)
{
    int[][] combs = new int[(int)(Math.pow(colorShape.length, numPositions))][numPositions];

    int[] holding = new int[numPositions];
    for(int i = 0 ; i < numPositions ; i++)
    {
        holding[i]=0;
    }

    for(int i = 0 ; i < combs.length ; i++)
    {
        for(int j = 0 ; j < numPositions ; j++)
        {
            combs[i][j] = colorShape[holding[j]];   
        }
        incrementHolding(holding, colorShape.length);
    }
    return combs;
}

public static boolean incrementHolding(int[] holding, int max)
{
    for(int i = holding.length-1 ; i >= 0 ; i--)
    {
        if(holding[i]+1 == max)
        {
            if(i==0)
                return false;
            holding[i]=0;
        }
        else
        {
            holding[i]++;
            return true;
        }
    }
    return true;
}

This works for me now. It's a lot of code though. And it takes a while to run.

NOTE: At 5 positions the array has 9,765,625 elements.

Cruncher
  • 7,641
  • 1
  • 31
  • 65
  • Thanks, but the number of positions could range up to 5. Wouldn't this code only work for positions == 2? – user2757330 Sep 07 '13 at 17:42
  • You mean you could have more than just colour and shape? If you replace all 2's here with any number it will work. Except for printing. But I'm sure you can handle printing. – Cruncher Sep 07 '13 at 17:43
  • Ahh, I see. Let me test it really quick to see if I can get it to work. – user2757330 Sep 07 '13 at 17:45
  • Nevermind, I think I know what you mean by positions now. It will only work for 2 – Cruncher Sep 07 '13 at 17:45
  • To do what you want, the array is going the be ENORMOUS. I don't see the value in storing all combination. The best way I can think of is recursion now. – Cruncher Sep 07 '13 at 17:47
  • @user2757330 Updated the answer with a more general solution – Cruncher Sep 07 '13 at 18:07
  • Thank you very much! I wish there was a more elegant way for me to store this information (or a way to get around storing it at all), but I can't think of any at the moment. However, this works like a charm! – user2757330 Sep 07 '13 at 18:37
  • Pardon my asking, but could you please explain how the two helper methods work? I'm not sure I completely understand what's going on there. Thanks! – user2757330 Sep 07 '13 at 20:48
  • @user2757330 I may be able to cut the code a little to make it more elegant and understandable. I'll write an explanation of the code tonight. – Cruncher Sep 09 '13 at 13:29