Create random circles in a grid

for knowing where you set a circle need some memory,

  • minimal boolean[][] array,
  • max a class.

and the grid position [cols][rows] to set
could find by the random function.

int cols = 60, rows = 40, scale = 18;
boolean[][] setc = new boolean[cols][rows];

void setup() {
  size (1200, 800);
  ellipseMode(CORNER);
  preset();
  setrandom();
}

void draw() {
  drawgrid();
}

void preset() {
  for (int i = 0; i < cols; i++)
    for (int j = 0; j < rows; j++)
      setc[i][j] = false;
}

void setrandom() {
  int i = (int)random(cols);
  int j = (int)random(rows);
  setc[i][j] = true;
}

void drawgrid() {
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      int x = i * scale;
      int y = j * scale;
      fill(#add8e6);
      stroke(0);
      rect(x, y, scale, scale);
      fill(200, 0, 0);
      noStroke();
      if ( setc[i][j] ) circle(x, y, scale);
    }
  }
}


here a different approach:
Creating Snake Game ,

1 Like