Wednesday, April 25, 2012

generate next color randomly if the color is still available

I m working on an app for android and I'm struggling at some points. One of them would be to chose the next color (out of four) but keeping in mind, that the color chosen, could be empty already, in this case the next of the four colors shall be chosen



I have two ways of doing it, but one of them is way too much code and the other one is causing a crash (I guess that happens because it can end up in an endless loop)



Thanks in advance



public void nextColor(Canvas canvas) {
Random rnd = new Random(System.currentTimeMillis());
int theNextColor = rnd.nextInt(4);
switch (theNextColor) {
case 0:
if (!blue.isEmpty()) {
currentPaint = paintBlue;
} else
nextColor(canvas);

case 1:
if (!grey.isEmpty()) {
currentPaint = paintGray;
} else
nextColor(canvas);
case 2:
if (!gold.isEmpty()) {
currentPaint = paintGold;
} else
nextColor(canvas);
case 3:
if (!red.isEmpty()) {
currentPaint = paintRed;
} else
nextColor(canvas);
}




No comments:

Post a Comment