java - Not more than two colors of the same color -


i call method random colors 8 objects. if color same 3 objects in row, it's not valid. 1 or 2 colors besides each other of same color valid. thought code should work, still 3 objects in row of 8 sam color! have done wrong? perhaps done in better , simplier way? proposals welcome!

part of loop 8 random numbers

for (int j = 0; j < 8; j++) { // 8 objects in each column  // call method 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; } 

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; } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -