10 Random red blocks on a grid

Hey!
I made this 60x40 grid and I am trying to make 10 of the squares red. And it has to be 10 random squares each time I run the program. I have tried a lot but can’t figure it out…
Can anyone please tell me who to do it?

int widthGrid = 60;
int heightGrid = 40;
int block[][] = new int[widthGrid][heightGrid];

int widthBlock = 10;
int heightBlock = 10;

void setup() {
  size(600, 400);
}

void draw() { 

  for (int i = 0; i < widthGrid; i++) {
    for (int j = 0; j < heightGrid; j++) {
      int xPositionBlock = i * widthBlock;
      int yPositionBlock = j * heightBlock;

      fill(#0ff09d);
      rect(xPositionBlock, yPositionBlock, widthBlock, heightBlock);
    }
  }
}
1 Like

You could try using a different array with values of the x and y coordinates of the grid(by tens, so that it stays within the correct position on the grid, as the size of the squares are ten), and making a block class, maybe something like this;
NOTE; remember to myBlockN.display(); in void draw for all 10

int widthGrid = 60;
int heightGrid = 40;

int blockX[] = {
0,10,20,30,40,50
};
int blockY[] = {
0,10,20,30
};

int widthBlock = 10;
int heightBlock = 10;

class Block() {
color c;
int xpos;
int ypos;
Block(tempxpos, tempypos, tempc) {
xpos = tempxpos;
ypos = tempypos;
c = tempc;}

void display() {
fill(c);
rect(xpos,ypos,10,10);
}
}

block myBlock1;
block myBlock2;
block myBlock3;
block myBlock4;
block myBlock5;
block myBlock6;
block myBlock7;
block myBlock8;
block myBlock9;
block myBlock10;

void setup() {
size(600,400);
myBlock1 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock2 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock3 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock4 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock5 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock6 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock7 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock8 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock9 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
myBlock10 = new Block(blockX[random(6)],blockY[random(4)],#0ff09d);
}
2 Likes

Thank you!
I changed it up a little since I don’t want to use class. But it works now!
Sometimes if I run the program there are two red blocks on the same position so I only see 9 blocks…
It doesn’t happen often but if I increase the number of red blocks to 30 it does happen a lot.
I tried to fix that with an ‘if-statement’ but I can’t get it to work…

int widthGrid = 60;
int heightGrid = 40;
int block[][] = new int[widthGrid][heightGrid];
int blockX[] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60
};
int blockY[] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
};
int widthBlock = 10;
int heightBlock = 10;

int positionBlockY;
int positionBlockX;

void setup() {
  size(600, 400);
}

void draw() { 

  for (int i = 0; i < widthGrid; i++) {
    for (int j = 0; j < heightGrid; j++) {
      int xPositionBlock = i * widthBlock;
      int yPositionBlock = j * heightBlock;
      fill(#0ff09d);
      rect(xPositionBlock, yPositionBlock, widthBlock, heightBlock);
    }
  }
  noLoop();

  for (int p = 0; p < 10; p++) {
    positionBlockX = blockX[(int)random(60)] * 10;
    positionBlockY = blockY[(int)random(40)] * 10;
    println(positionBlockX, positionBlockY);
    fill(#ff0000);
    rect(positionBlockX, positionBlockY, widthBlock, heightBlock);
  }

}

Maybe you could add something that detects whether or not a space is taken, and do something similar to
positionBlockX = blockX[(int)random(60),!=*taken blocks] * 10;
?

2 Likes

Please use the following way to set arrays with more than a couple values (if it‘s possible) :

int[] blockX;

void setup() {
   blockX = new int[61];
   for (int i = 0; i < blockX.length; i++) {
      blockX = i;
   }
}
1 Like

Haha… Thanks :slight_smile:

1 Like

If you are picking things at random (and then checking to see if you have already picked them) it can get very inefficient. You might want to pick 9 unique values out of 10, but on average you will have to make 19 random selections before you get 9 unique ones.

One way around this is to shuffle, then read n numbers from the top of the shuffled list. Another way is to create a list, then pop random values off as you pick each one. Either of these approaches can be done with IntList:

https://processing.org/reference/IntList.html

1 Like