hello, i been trying to make this cellular automata program, the idea is to change the step with the key, so each time i press the key "n" it goes to a step further.
The objective is when i run the program the cellullar automata is stopped and everytime i press "n" it calculates a new generation or step.
The thing is that the program doesnt work perfect, i think this is because of the KeyPressed command, for example if i have this:
Code:
if(keyPressed) {
if (key == 'n' || key == 'N') {
sube = sube + 1;
sube = sube % 2;
print(" " + sube);
}
}
I would like that each time i press the "n" key it prints 0, and if i press "n" again it prints 1, and if i print "n" again it prints 0 and so on.
But it doenst work as i expected and if i press "n" i get 1 0 , sometime i get 1 0 1 and sometimes i get 1 or 0.
here is the code
Code:
int numero_de_cubos = 13;
int mouse_x;
int mouse_y;
int sube = 0;
cubo[][] matriz_de_cubos = new cubo[numero_de_cubos][numero_de_cubos];
void setup() {
size(390,390);
frameRate(20);
smooth();
background(255, 255, 255);
for (int i = 0 ; i< numero_de_cubos ; i++ ){
for (int j = 0 ; j< numero_de_cubos ; j++ ){
matriz_de_cubos[i][j] = new cubo(i * 30, j * 30 , random(300));
}
}
}
void draw() {
background(0, 255, 255);
if(keyPressed) {
if (key == 'b' || key == 'B') {
presiona() ;
}
if (key == 'n' || key == 'N') {
sube = sube + 1;
sube = sube % 2;
print(" " + sube);
}
}
for (int i = 0 ; i< numero_de_cubos ; i++ ){
for (int j = 0 ; j< numero_de_cubos ; j++ ){
matriz_de_cubos[i][j].dibuja();
if (sube == 0) {
if(i < numero_de_cubos - 1 && i > 1 && j < numero_de_cubos - 1 && j > 1 ){
if( (matriz_de_cubos[i + 1][j].ron + matriz_de_cubos[i - 1][j].ron + matriz_de_cubos[i][j + 1].ron + matriz_de_cubos[i][j - 1].ron ) == 1 ){
matriz_de_cubos[i ][j].ron = 0;
}
else if(( matriz_de_cubos[i + 1][j].ron + matriz_de_cubos[i - 1][j].ron + matriz_de_cubos[i][j + 1].ron + matriz_de_cubos[i][j - 1].ron) < 1) {
matriz_de_cubos[i ][j].ron = 1 ;
}
else if(( matriz_de_cubos[i + 1][j].ron + matriz_de_cubos[i - 1][j].ron + matriz_de_cubos[i][j + 1].ron + matriz_de_cubos[i][j - 1].ron) > 2 ) {
matriz_de_cubos[i ][j].ron = 1;
}
}
}
}
}
sube = 1;
}
void presiona() {
for (int i = 0 ; i< numero_de_cubos ; i++ ){
for (int j = 0 ; j< numero_de_cubos ; j++ ){
matriz_de_cubos[i][j].randomiza();
matriz_de_cubos[i][j].dibuja();
}
}
}
void mousePressed() {
mouse_x = mouseX * numero_de_cubos / width ;
mouse_y = mouseY * numero_de_cubos / height ;
}
class cubo {
int xx;
int yy;
float frecuencias;
float ron = random(2);
color coloro ;
cubo(int x, int y , float valores_frecuencias ) {
xx = x;
yy = y;
frecuencias = valores_frecuencias;
ron = int(ron);
}
void dibuja(){
if (ron == 1){
coloro = color(220, 10, 60);
}
else if (ron == 0){
coloro= color(55, 5 ,255);
}
else if (ron == 2){
coloro= color(5, 225 ,25);
}
fill(coloro);
rect (xx, yy, 30, 30 );
}
void vecinos(){
}
void randomiza(){
ron = random(2) ;
ron = int(ron);
}
}