Randomly distribute objects on grid

Instead of having two arrays that record the X and Y positions of the hidden objects, have one 2D array that knows if there is a thing in every location, and also what that thing is.

And if you come up with a clever numbering scheme, you can also encode if the things are hidden or revealed.

Example:

int[][] items = new int[20][20];

void setup() {
  size(400, 400);
  for ( int i = 0; i < 20; i++) {
    for ( int j = 0; j < 20; j++) {
      items[i][j] = int(random(-5, -1));
    }
  }
}

void draw() {
  background(0);
  for ( int i = 0; i < 20; i++) {
    for ( int j = 0; j < 20; j++) {
      int t = items[i][j];
      if ( t < 0 ) {
        fill(128);
        stroke(0);
        rect(20*i, 20*j, 20, 20);
      } else if ( t == 2 ) {
        fill(0, 0, 200);
        stroke(0);
        rect(20*i+2, 20*j+2, 16, 16);
      } else if ( t == 3 ) {
        fill(200, 0, 0);
        stroke(0);
        ellipse(20*i+10, 20*j+10, 16, 16);
      } else if ( t == 4 ) {
        fill(0, 200, 0);
        stroke(0);
        triangle( 
          20*i+10, 20*j+2, 
          20*i+2, 20*j+18,
          20*i+18, 20*j+18
        );
      }
      
      
    }
  }
}

void mousePressed() {
  int x = int( mouseX / 20 );
  int y = int( mouseY / 20 );
  if ( x >= 0 && y >= 0 && x < 20 && y < 20 ) {
    items[x][y] = abs(items[x][y]);
  }
}

2 Likes