# How would you shuffle a number of set x-y values and have them not overlap?

• 12-23-2011, 05:45 AM
stealthiskarl
How would you shuffle a number of set x-y values and have them not overlap?
I am making a program that displays boxes. Each box has a set x value and a set y value. I need to shuffle all the x values of the boxes (there happen to only be 2) and all the y values (there are 5) and then make sure they don't overlap. It should look like this:
[] []
[] []
[] []
[] []
[] []

When you hit a button it will take those boxes and just reorganize them into different spots. Thats what I need. PLEASE help, I've been working on this for hours. Its quite anoying

My current code shuffles them, but it also has boxes that overlap. How do I fix this? Here is my Shuffle() function

Code:

```if(buttonPressed){ int i = 0; while (i < 10) { int[] q = { 155, 85, 355, 85, 155, 215, 355, 215, 155, 345, 355, 345, 155, 475, 355, 475, 155, 605, 355, 605 }; int z = ((int)((Math.random()*20)/2)*2); while (q[z] == -999) { z = ((int)((Math.random()*20)/2))*2; } Brain.xPos[i] = q[z]; Brain.yPos[i] = q[z+1]; q[z] = -999; System.out.println(Brain.xPos[i] + " " + q[z+1]);  //For debugging purposes only i++; } }```
• 12-23-2011, 06:05 AM
stealthiskarl
Re: How would you shuffle a number of set x-y values and have them not overlap?
Never mind. The problem was I initialized q[] inside of the outer while Loop. I just had to take it out.