Reputation: 9301
I call a method to get random colors to eight objects. If the color is the same for three objects in a row, then it's not valid. Only one or two colors besides each other of the same color is valid. I thought my code should work, but I still get three objects in a row of eight with the sam color! What have I done wrong? perhaps it could be done in a better and simplier way? Proposals are welcome!
Part of the loop to get eight random numbers
for (int j = 0; j < 8; j++) { // 8 objects in each column
// Call method to get random color
int color = getRandomColor(j);
The Method
public int getRandomColor(int j) {
int color = randomNumber1.nextInt(8);
colors[j] = color;
if(j>1 && colors[j-1] == color && colors[j-2] == color) {
getRandomColor(j);
}
return color;
}
Upvotes: 0
Views: 110
Reputation: 68715
Try this:
public int getRandomColor(int j){
int color = randomNumber1.nextInt(8);
colors[j] = color;
while(j>1 && colors[j-1] == color && colors[j-2] == color){
color = randomNumber1.nextInt(8);
colors[j] = color;
}
return color;
}
Upvotes: 1