Problem with Game of Life

Hi there,
I’ve decided to try programing the game of life with a little added covid implementation. I somehow have an issue which I think is in the “locating neighbors”. It seems to be working but somehow everything is symmetric along the diagonal and I can’t seem to fix it. I’ve tried relooking at the code but if someone can help I’d greatly appreciate it. I can’t seem to see where I’m wrong.
</
int spacer = 5;
int cols;
int rows;
int covid;
int initialCarrier = 100;
int chanceCured = 8;
int chanceSick = 4;
color fillCol = color(255, 255, 255);
boolean grid;

void setup() {
size(800, 800);
rows = width/spacer;
cols = height/spacer;
covid = 2;
grid = new boolean[rows][cols][covid];
for (int y = 0; y < cols; y++) {
for (int x = 0; x < rows; x++) {
grid[y][1] = false;

  int rnd = floor(random(2));
  if (rnd == 1) {
    grid[x][y][0] = true;
  }

  if (grid[x][y][0] ) {
    int infect = floor(random(initialCarrier));
    if (infect == 1) {
      grid[x][y][1] = true;
    };
  }
}

}
}

void draw() {
background(0);
frameRate(5);
noStroke();
for (int y = 0; y < cols; y ++) {
for (int x = 0; x < rows; x ++) {

  // cured
  if (grid[x][y][1]) {
    int cured = floor(random(chanceCured));
    if (cured == 1) {
      grid[x][y][1] = false;
    };
    if (cured == 2) {
      grid[x][y][1] = false;
      grid[x][y][0] = false;
    };
  }

  /// if contaminated
  if (grid[x][y][1] && grid[x][y][0]) {
    fillCol = color(0, 255, 0);
    fill(fillCol);
  }


  // not contaminated
  else if (grid[x][y][0] && !grid[x][y][1]) {

    int species = 0;
    // previous
    //for ( int i= -1; i<2; i++) {
    //  for ( int j= -1; j<2; j++) {
    //    int mody = (y + j + cols) % cols;
    //    int modx = (x + i + rows) % rows;
    //    if (grid[modx][mody][0]) {
    //      species +=1;
    //    }
    //  }
    //}


    // new basic calc
    for (int i = -1; i < 2; i++) {
      for (int j = -1; j < 2; j++) {
        int nX = x+j;
        int nY = y+i;

        if (nX <= 0) {
          nX = 0;
        }
        if (nY <= 0) {
          nY = 0;
        }
        if (nX >= cols-1) {
          nX = cols-1;
        }
        if (nY >= rows-1) {
          nY = rows-1;
        }
        if (grid[nX][nY][0]) {
          species += 1;
        }
      }
    }
      species -= 1;
    if (species > 6) {
      fillCol = color(255, 255, 255);
    } else if (species > 4) {
      fillCol = color(255, 0, 0);
    } else if (species > 2) {
      fillCol = color(0, 0, 255);
    } else {
      fillCol = color(0, 0, 100);
    }

    fill(fillCol);
  } else {
    fill(0, 0, 0);
  };

  rect(x*spacer, y*spacer, spacer, spacer);
}

}
updateGrid();
}

void updateGrid() {
boolean child = grid;

for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
if (!grid[y][1]) {
boolean alive = grid[y][0];

    int species = 0;

    // check neighbour v2
    for (int i = -1; i < 2; i++) {
      for (int j = -1; j < 2; j++) {
        int nX = x+i;
        int nY = y+j;

        if (nX <= 0) {
          nX = 0;
        }
        if (nY <= 0) {
          nY = 0;
        }
        if (nX >= cols-1) {
          nX = cols-1;
        }
        if (nY >= rows-1) {
          nY = rows-1;
        }
        if (grid[nY][nX][0]) {
          species += 1;
        }

        //if neighbour contagious
        if (grid[nX][nY][1]) {
          int contagious = floor(random(chanceSick));
          if (contagious < 2 && alive) {
            child[x][y][1] = true;
          }
        }
      }
    }

    // check neighbour
    //for ( int i= -1; i<2; i++) {
    //  for ( int j= -1; j<2; j++) {
    //    int modi = (x + i + cols) % cols;
    //    int modj = (y + j + rows) % rows;

    //    // if neighbour contagious
    //    if (grid[modj][modi][1]) {
    //        int contagious = floor(random(chanceSick));
    //        if (contagious < 2 && alive) {
    //          child[x][y][1] = true;
    //        }
    //      }

    //      // add neighbour
    //      if (grid[modj][modi][0]) {
    //        species +=1;
    //      }
    //    }
    //  }

    if (alive) {
      species -= 1;
    }

    if (!alive && species == 3) {
      child[x][y][0] = true;
    } else if (alive && (species < 2 || species > 3)) {
      child[x][y][0] = false;
      child[x][y][1] = false;
    }

  }
}

}
grid = child;
}

In processing ide if you press Ctrl+t you can get your code indented. Much easier to read, especially inside a code block

int spacer = 5;
int cols;
int rows;
int covid;
int initialCarrier = 100;
int chanceCured = 8;
int chanceSick = 4;
color fillCol = color(255, 255, 255);
boolean[][][] grid;

void setup() {
  size(800, 800);
  rows = width/spacer;
  cols = height/spacer;
  covid = 2;
  grid = new boolean[rows][cols][covid];
  for (int y = 0; y < cols; y++) {
    for (int x = 0; x < rows; x++) {
      grid[x][y][1] = false;

      int rnd = floor(random(2));
      if (rnd == 1) {
        grid[x][y][0] = true;
      }

      if (grid[x][y][0] ) {
        int infect = floor(random(initialCarrier));
        if (infect == 1) {
          grid[x][y][1] = true;
        };
      }
    }
  }
}

void draw() {
  background(0);
  frameRate(5);
  noStroke();
  for (int y = 0; y < cols; y ++) {
    for (int x = 0; x < rows; x ++) {

      // cured
      if (grid[x][y][1]) {
        int cured = floor(random(chanceCured));
        if (cured == 1) {
          grid[x][y][1] = false;
        };
        if (cured == 2) {
          grid[x][y][1] = false;
          grid[x][y][0] = false;
        };
      }

      /// if contaminated
      if (grid[x][y][1] && grid[x][y][0]) {
        fillCol = color(0, 255, 0);
        fill(fillCol);
      }


      // not contaminated
      else if (grid[x][y][0] && !grid[x][y][1]) {

        int species = 0;
        // previous
        //for ( int i= -1; i<2; i++) {
        //  for ( int j= -1; j<2; j++) {
        //    int mody = (y + j + cols) % cols;
        //    int modx = (x + i + rows) % rows;
        //    if (grid[modx][mody][0]) {
        //      species +=1;
        //    }
        //  }
        //}


        // new basic calc
        for (int i = -1; i < 2; i++) {
          for (int j = -1; j < 2; j++) {
            int nX = x+j;
            int nY = y+i;

            if (nX <= 0) {
              nX = 0;
            }
            if (nY <= 0) {
              nY = 0;
            }
            if (nX >= cols-1) {
              nX = cols-1;
            }
            if (nY >= rows-1) {
              nY = rows-1;
            }
            if (grid[nX][nY][0]) {
              species += 1;
            }
          }
        }
        species -= 1;
        if (species > 6) {
          fillCol = color(255, 255, 255);
        } else if (species > 4) {
          fillCol = color(255, 0, 0);
        } else if (species > 2) {
          fillCol = color(0, 0, 255);
        } else {
          fillCol = color(0, 0, 100);
        }

        fill(fillCol);
      } else {
        fill(0, 0, 0);
      };

      rect(x*spacer, y*spacer, spacer, spacer);
    }
  }
  updateGrid();
}

void updateGrid() {
  boolean[][][] child = grid;

  for (int y = 0; y < rows; y++) {
    for (int x = 0; x < cols; x++) {
      if (!grid[x][y][1]) {
        boolean alive = grid[x][y][0];

        int species = 0;

        // check neighbour v2
        for (int i = -1; i < 2; i++) {
          for (int j = -1; j < 2; j++) {
            int nX = x+i;
            int nY = y+j;

            if (nX <= 0) {
              nX = 0;
            }
            if (nY <= 0) {
              nY = 0;
            }
            if (nX >= cols-1) {
              nX = cols-1;
            }
            if (nY >= rows-1) {
              nY = rows-1;
            }
            if (grid[nY][nX][0]) {
              species += 1;
            }

            //if neighbour contagious
            if (grid[nX][nY][1]) {
              int contagious = floor(random(chanceSick));
              if (contagious < 2 && alive) {
                child[x][y][1] = true;
              }
            }
          }
        }

        // check neighbour
        //for ( int i= -1; i<2; i++) {
        //  for ( int j= -1; j<2; j++) {
        //    int modi = (x + i + cols) % cols;
        //    int modj = (y + j + rows) % rows;

        //    // if neighbour contagious
        //    if (grid[modj][modi][1]) {
        //        int contagious = floor(random(chanceSick));
        //        if (contagious < 2 && alive) {
        //          child[x][y][1] = true;
        //        }
        //      }

        //      // add neighbour
        //      if (grid[modj][modi][0]) {
        //        species +=1;
        //      }
        //    }
        //  }

        if (alive) {
          species -= 1;
        }

        if (!alive && species == 3) {
          child[x][y][0] = true;
        } else if (alive && (species < 2 || species > 3)) {
          child[x][y][0] = false;
          child[x][y][1] = false;
        }
      }
    }
  }
  grid = child;
}

In the last if statement you have mixed nY and nX. It could be an explanation. Also checks on the border creates double counting. There’s always eight neighbours counted, but if the cell is on the border border cells get counted twice. When nY is -1 you change it to 0 and when it is zero it’s counted again.

A yes true … thanks. probably need to figure out a new way for this. It was the second counting version I tried to come up with as I had the same issue with the previous method using mods (which I think I didn’t fully understand)

for instance where I know I have a problem is if I make the screen into a rectangle it breaks down. It’ll only accept a square. This indicates to me that somewhere I’m inverting the cols and rows … but I can’t figure out where as small tweaks and suddenly everything breaks down

Setting dimensions to rectangle gave hint where the problem is. You had mixed rows and columns (x goes along horizontal axis so it matches columns). Once I replaced columns and rows things seem to work fine.

int spacer = 5;
int cols;
int rows;
int covid;
int initialCarrier = 100;
int chanceCured = 8;
int chanceSick = 4;
color fillCol = color(255, 255, 255);
boolean[][][] grid;

void setup() {
  size(1200, 800);
  rows = height/spacer;
  cols = width/spacer;
  covid = 2;
  grid = new boolean[cols][rows][covid];
  for (int y = 0; y < rows; y++) {
    for (int x = 0; x < cols; x++) {
      grid[x][y][1] = false;

      int rnd = floor(random(2));
      if (rnd == 1) {
        grid[x][y][0] = true;
      }

      if (grid[x][y][0] ) {
        int infect = floor(random(initialCarrier));
        if (infect == 1) {
          grid[x][y][1] = true;
        };
      }
    }
  }
}

void draw() {
  background(0);
  frameRate(5);
  noStroke();
  for (int y = 0; y < rows; y ++) {
    for (int x = 0; x < cols; x ++) {

      // cured
      if (grid[x][y][1]) {
        int cured = floor(random(chanceCured));
        if (cured == 1) {
          grid[x][y][1] = false;
        };
        if (cured == 2) {
          grid[x][y][1] = false;
          grid[x][y][0] = false;
        };
      }

      /// if contaminated
      if (grid[x][y][1] && grid[x][y][0]) {
        fillCol = color(0, 255, 0);
        fill(fillCol);
      }


      // not contaminated
      else if (grid[x][y][0] && !grid[x][y][1]) {

        int species = 0;
        // previous
        //for ( int i= -1; i<2; i++) {
        //  for ( int j= -1; j<2; j++) {
        //    int mody = (y + j + cols) % cols;
        //    int modx = (x + i + rows) % rows;
        //    if (grid[modx][mody][0]) {
        //      species +=1;
        //    }
        //  }
        //}


        // new basic calc
        for (int i = -1; i < 2; i++) {
          for (int j = -1; j < 2; j++) {
            int nX = x+j;
            int nY = y+i;

            if (nX <= 0) {
              nX = 0;
            }
            if (nY <= 0) {
              nY = 0;
            }
            if (nX >= cols-1) {
              nX = cols-1;
            }
            if (nY >= rows-1) {
              nY = rows-1;
            }
            if (grid[nX][nY][0]) {
              species += 1;
            }
          }
        }
        species -= 1;
        if (species > 6) {
          fillCol = color(255, 255, 255);
        } else if (species > 4) {
          fillCol = color(255, 0, 0);
        } else if (species > 2) {
          fillCol = color(0, 0, 255);
        } else {
          fillCol = color(0, 0, 100);
        }

        fill(fillCol);
      } else {
        fill(0, 0, 0);
      };

      rect(x*spacer, y*spacer, spacer, spacer);
    }
  }
  updateGrid();
}

void updateGrid() {
  boolean[][][] child = grid;

  for (int y = 0; y < rows; y++) {
    for (int x = 0; x < cols; x++) {
      if (!grid[x][y][1]) {
        boolean alive = grid[x][y][0];

        int species = 0;

        // check neighbour v2
        for (int i = -1; i < 2; i++) {
          for (int j = -1; j < 2; j++) {
            int nX = x+i;
            int nY = y+j;

            if (nX <= 0) {
              nX = 0;
            }
            if (nY <= 0) {
              nY = 0;
            }
            if (nX >= cols-1) {
              nX = cols-1;
            }
            if (nY >= rows-1) {
              nY = rows-1;
            }
            if (grid[nX][nY][0]) {
              species += 1;
            }

            //if neighbour contagious
            if (grid[nX][nY][1]) {
              int contagious = floor(random(chanceSick));
              if (contagious < 2 && alive) {
                child[x][y][1] = true;
              }
            }
          }
        }

        // check neighbour
        //for ( int i= -1; i<2; i++) {
        //  for ( int j= -1; j<2; j++) {
        //    int modi = (x + i + cols) % cols;
        //    int modj = (y + j + rows) % rows;

        //    // if neighbour contagious
        //    if (grid[modj][modi][1]) {
        //        int contagious = floor(random(chanceSick));
        //        if (contagious < 2 && alive) {
        //          child[x][y][1] = true;
        //        }
        //      }

        //      // add neighbour
        //      if (grid[modj][modi][0]) {
        //        species +=1;
        //      }
        //    }
        //  }

        if (alive) {
          species -= 1;
        }

        if (!alive && species == 3) {
          child[x][y][0] = true;
        } else if (alive && (species < 2 || species > 3)) {
          child[x][y][0] = false;
          child[x][y][1] = false;
        }
      }
    }
  }
  grid = child;
}