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!