Randomly distribute objects on grid

Hello,

I’m working on a game/ assignment where I have to randomly place some objects into a grid. These objects are supposed to be hidden on the grid and pop up when a player clicks on it. I made an attempt by making 2 arrays (is a 2D array a better choice for this perhaps?), one for the X and one for the Y axis with the locations of the squares. I then used the random function to randomly pick a value from the array. It somewhat works but is there a way to make it dynamic so that changing the grid size won’t break my code? A second question is how I can loop the random function until 30% of the gird is covered with hidden objects. I thought of using a for loop but got stuck after that.

int [] coordsX = {0,30,60,90,120,150,180,210,240,270,300,330,360,390,420,450,480,510,540,570,600,630,660,690,720,750,780,810,840,870};
int [] coordsY = {0,30,60,90,120,150,180,210,240,270,300,330,360,390,420,450,480,510,540,570};

The code below creates a 30x20 grid.

    int rows = 20;
    int cols = 30;
    int size = 30;
    int xPos = 0;
    int yPos = 0;
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        fill(255);
        rect(xPos, yPos, size, size);
        xPos += size;
      }
        xPos = 0;
        yPos += size;
    };

Any help would be appreciated!

1 Like

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

Thank you, it works perfectly. The thing I still don’t really understand are 2D arrays. Are the 20’s in the array supposed to represent the amount of objects that are placed? And what exactly does using the -5 and -1 in the random function do? I’m sorry if I’m asking a lot. Programming is not my strong point.

Don’t worry about asking, fire away!

For chessboards (example) they often combine letters and numbers to indicate a tile on the board. For this situation having two separate arrays would suffice– one for the rows, the other for the columns. By using 16 values (8 + 8) you can indicate each tile’s location.

If you want to code some funky coloured chessboard where each tile has a different colour, two separate arrays might not be optimal (you could creatively find different ways to solve the issue, but let’s forget about that for now). Because you need to save a unique colour value for each tile on the board, it might be better to go for a double array:

color[][] tileColours = new color[8][8];
int size;

void setup() {
  size(400, 400);
  noStroke();
  size = width / 8;
  prepArray();
}

void draw() {
  drawArray();
}

void prepArray() {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      float r = random(255);
      float g = random(255);
      float b = random(255);
      tileColours[i][j] = color(r, g, b);
    }
  }
}

void drawArray() {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      color c = tileColours[i][j];
      float x = i * size;
      float y = j * size;
      fill(c);
      rect(x, y, size, size);
    }
  }
}

Compared to the two singular arrays which have 16 values in total, the double array in this example contains 64 values.

Does that make the added value of double arrays more clear?

2 Likes

That does clarify it for me, thanks!

1 Like