Hi! i’m working with a code, and i need to remove the color once it has been used.
is a walker who takes on a color each time he begins his path. I need to make sure that the color not to repeat
This is the class Paleta, i just separate the cold colors of the warm colors, also an array with both of them.
Thanks!
color [] colores = {#d25b17, #e7bc4b, #942720,#1e2744, #304e34, #68494f};
//ARRAY WARM
color [] calidos = {#d25b17, #e7bc4b, #942720};
//ARRAY COLD
color [] frios = {#1e2744, #304e34, #68494f};
//PALETA FONDO
color [] fondo = {#c63435, #ec603c, #ffc13c ,#4c6045 ,#264468 , #41415b};
class Paleta {
PImage imagen;
Paleta( ) {
}
color darColor() {
return colores[int(random(6))];
}
color darCalido() { //RETORNA UN COLOR AL AZAR DEL ARREGLO
return calidos[int(random(3))];
}
color darFrio() { //RETORNA UN COLOR AL AZAR DEL ARREGLO
return frios[int(random(3))];
}
color darFondo() {
return fondo[int(random(6))];
}
I suggest to create a couple of twin arrays, then checking if the new color is already in the list and, if so, discard it. Here’s my approach based on arrays of integers - just change int to color and it should work:
//an array of six numbers
int[] colores = {1, 2, 3, 4, 5, 6};
//a sub-array of three numbers
int[] calidos = {1, 2, 3};
//and its copy of the same lentgh
int[] calidosCopy = new int[calidos.length];
//a second sub-array of other three numbers
int[] frios = {4, 5, 6};
//and its copy
int[] friosCopy = new int[frios.length];
//another array of other six numbers
int[] fondo = {7, 8, 9, 10, 11, 12};
//a random number from zero to three(excluded)
int randomThree = int(random(3));
//a random number from zero to six(excluded)
int randomSix = int (random(6));
//one of the six numbers from the first array
int darColores = colores[randomSix];
//if one of the numbers of the first sub-array
//IS NOT EQUAL TO
//the one of the six numbers previously found,
//copy it to the first sub-array copy
for (int i = 0; i < calidos.length; i++) {
if (calidos[i] != darColores) {
calidosCopy[i] = calidos[i];
}
}
//then extract a random number from the first sub-array copy
int darCalidos = calidosCopy[randomThree];
//if one of the numbers of the second sub-array
//IS NOT EQUAL TO
//the one of the six numbers previously found,
//copy it to the second sub-array copy
for (int i = 0; i < frios.length; i++) {
if (frios[i] != darColores) {
friosCopy[i] = frios[i];
}
}
//then extract a random number from the second sub-array copy
int darFrios = friosCopy[randomThree];
int darFondo = fondo[randomSix];
println(darColores);
println(darCalidos);
println(darFrios);
println(darFondo);
If you use arrayList instead of array you have more flexibility with adding and removing items from an arrayList. And an arraList will resize dynamically.