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

**URL:** <https://discourse.processing.org/t/why-does-my-for-loop-create-cells-in-one-specific-area-instead-of-in-a-grid/14197>\
**Category:** Coding Questions\
**Created:** [September 25, 2019, 7:40am UTC](https://discourse.processing.org/t/why-does-my-for-loop-create-cells-in-one-specific-area-instead-of-in-a-grid/14197 "2019-09-25T07:40:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haggisllama](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Haggisllama](https://discourse.processing.org/u/Haggisllama)\
**Post date:** [September 25, 2019, 7:40am UTC](https://discourse.processing.org/t/why-does-my-for-loop-create-cells-in-one-specific-area-instead-of-in-a-grid/14197/1 "2019-09-25T07:40:51Z")

</div>

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.

```auto
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();
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [September 25, 2019, 8:33am UTC](https://discourse.processing.org/t/why-does-my-for-loop-create-cells-in-one-specific-area-instead-of-in-a-grid/14197/2 "2019-09-25T08:33:53Z")

</div>

-a- try follow your setup logic with printing

```auto
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

```auto
void DrawAndPress()
  

```

instead x y possibly use xpos ypos
