Hey guys,
Im working on a project for school where I have to randomly generate x amount of colored tiles within a grid. In my current code every tile in the grid randomly receives a red or blue color.
What do I need to change to ensure that for example only 10 red and 10 blue squares are colored in this 60x40 grid?
final int COLS = 60;
final int ROWS = 40;
int cellSize = 20;
int[][] generateItems = new int[COLS][ROWS];
void setup() {
size(1200, 900);
for ( int i = 0; i < COLS; i++) {
for ( int j = 0; j < ROWS; j++) {
generateItems[i][j] = int(random(1, 4));
println(generateItems[i][j] = int(random(1, 4)));
}
}
}
void draw() {
background(#FFFFFF);
for ( int i = 0; i < COLS; i++) {
for ( int j = 0; j < ROWS; j++) {
int t = abs(generateItems[i][j]);
if ( t < 1 ) {
} else if ( t == 2 ) {
fill(0, 0, 200);
noStroke();
rect(cellSize*i, cellSize*j, cellSize, cellSize);
} else if ( t == 3 ) {
fill(200, 0, 0);
noStroke();
rect(cellSize*i, cellSize*j, cellSize, cellSize);
}
}
}
}
void mousePressed() {
int x = int( mouseX / cellSize );
int y = int( mouseY / cellSize );
if ( x >= 0 && y >= 0 && x < 60 && y < 40 ) {
generateItems[x][y] = generateItems[x][y] = 1;
}
}
I’ve been stuck on this for a while so any help or advice is appreciated!