Why does my for loop create cells in one specific area instead of in a grid?

alright I made this code to try and get a grid of cells across the screen to try and make a cellular automata as I am quite new to programming but when I run this all of my cells are in one place near the bottom center on my screen (I know this because I set them to always display as white to check where they where as it wasn’t working. you may notice a logic function that generally isn’t being called but I just wanted to see if there was something I was missing that is obvious.

import java.util.ArrayList;


boolean paused = false;
int x = -10;
int y = -10;
final static ArrayList<cell> cells = new ArrayList();
class cell
{
  boolean state;
  float xpos, ypos; 
  float age;
  int identify;
  cell (float x, float y, int ident) {
    xpos = x;
    ypos = y;
    identify = ident;
  }
  void colour()
  {
    if (state == true)
    {
      fill(255);
    } else
    {
      fill(0);
    }
  }
  void DrawAndPress()
  {
    if (mouseX > x && mouseX < x+10 && mouseY > y && mouseY < y+10 && mousePressed)
    {
      state = !state;
    }
    rect(x, y, 10, 10);
  }
  void Logic()
  {
  }
}



void setup()
{

  fullScreen(); 
  background(0);
  noStroke();
  for (int i = 0; i < (width*height)/10000; i = i+1)
  {

    x = x+10;
    if (x > width)
    {
      y = y+10;
      x = 0;
    }
    cells.add( new cell(x, y, i));
  }
}



void draw()
{
  for (cell c : cells)   c.colour();
  for (cell c : cells)  c.DrawAndPress();
  if (!paused)
  {
    for (cell c : cells)  c.Logic();
  }
}
1 Like

-a- try follow your setup logic with printing

int x = -10;
int y = 0;//-10;
fullScreen();
//size(500,500);
int limit = (width*height)/1000;  //10000 ?
println("limit "+limit);
for (int i = 0; i < limit; i = i+1) {
  x = x+10;
  if (x > width) {
    y = y+10;
    x = 0;
  }
  println(x,y);
  rect(x,y,10,10);
}


-b- you are using what variables in

void DrawAndPress()
  

instead x y possibly use xpos ypos

1 Like