If statements in array

Hi all,

First of all I am very inexperienced with processing and this is a first attempt to create a simple set of rules. My goal is to transfer between various states (A,B,C,D) and finally make the program terminate itself in the desired state. Now I’ve set up a few array’s that refer to a state that could either be 1 or 0.

I want to use if statements to make them progress further. So A (random 1 or 0) → A1 goes to state D1 and D1 goes to state A0 (example). Whenever I want to use the array to specify this, it referes to int not matching with boolean. I’m a bit lost, below my code.

int A1 = 1; //values for the states.
int A0 = 0;
int B1 = 1;
int B0 = 0;
int C1 = 1;
int C0 = 0;
int D1 = 1;
int D0 = 0;

//READ, REWRITE, MOVE

int[] A = {A0, A1}; //array with the values A0 and A1
int[] B = {B0, B1}; //array with the values B0 and B1
int[] C = {C0, C1}; //array with the values C0 and C1
int[] D = {D0, D1}; //array with the values D0 and D1

void setup() {

int randomA = (int)random(A.length); // choose a random number from the A possibilities.
println(A[randomA]);

int randomB = (int)random(B.length); // choose a random number from the B possibilities.
println(B[randomB]);

int randomC = (int)random(C.length); // choose a random number from the C possibilities.
println(C[randomC]);

int randomD = (int)random(D.length); // choose a random number from the D possibilities.
println(D[randomD]);
}

void draw() {

if (A == 0) {
//fill (0, 90, 10);
//rect (25, 25, 25, 25);
} else if (A == 1) {
fill (80, 90, 10);
rect (25, 25, 25, 25);
}
}

Thanks for the help!

Hi @Jimma, I checked your code and one thing that you missed is that you were not saving the random value of your array in a variable. So I created some for you.

int A_picked;
int B_picked;
int C_picked;
int D_picked;

And in the draw function you were actually comparing a array with a int which is not possible, so I update the array with the new variable I created. Here is the code. I hope it helps.

int A1 = 1; //values for the states.
int A0 = 0;
int B1 = 1;
int B0 = 0;
int C1 = 1;
int C0 = 0;
int D1 = 1;
int D0 = 0;

//READ, REWRITE, MOVE

int[] A = {A0, A1}; //array with the values A0 and A1
int[] B = {B0, B1}; //array with the values B0 and B1
int[] C = {C0, C1}; //array with the values C0 and C1
int[] D = {D0, D1}; //array with the values D0 and D1

int A_picked;
int B_picked;
int C_picked;
int D_picked;

void setup() {

  int randomA = (int)random(A.length); // choose a random number from the A possibilities.
  A_picked = A[randomA];
  println(A[randomA]);
  
  int randomB = (int)random(B.length); // choose a random number from the B possibilities.
  B_picked = B[randomB];
  println(B[randomB]);
  
  int randomC = (int)random(C.length); // choose a random number from the C possibilities.
  C_picked = C[randomC];
  println(C[randomC]);
  
  int randomD = (int)random(D.length); // choose a random number from the D possibilities.
  D_picked = D[randomD];
  println(D[randomD]);
  
}

void draw() {

  if (A_picked == 0) {
    fill (0, 90, 10);
    rect (25, 25, 25, 25);
  } else if (A_picked == 1) {
    fill (80, 90, 10);
    rect (25, 25, 25, 25);
  }
  
}
2 Likes

Thank you so much for your help! And sorry for the late response. I did not receive a notification unfortunately. This solves my problem of having to create multiple integers for each new state the program executed.

Many thanks!

2 Likes