Game of Life BUG

I dont know what is wrong but, my game of life looks very akward.

(My Code is shit but it works)

THE CODE IS BUGGY ON THE PROCESSINg WEBSITE SO I POSTED IT ON PASTEBIN http://pastebin.com/zh5uupi3 http://pastebin.com/zh5uupi3 http://pastebin.com/zh5uupi3

// GameSize = 10x10
  int gameSize = 100;
  Cell[][] c = new Cell[gameSize][gameSize];
  int fr = 10;
  int frames = 0;

  boolean isDraving = true;

  void setup() {
    size(400, 400);
    frameRate(30);
    noStroke();

    // random cells are alive
    for (int x = 0; x < gameSize; x++) {
      for (int y = 0; y < gameSize; y++) {
        c[x][y] = new Cell();
        if (random(1)< 0.05) {
          c[x][y].alive = true;
        }
      }
    }
  }

  void draw() {
    frames++;
    if (!isDraving) {
      if (frames % fr == 0) {
        for (int x = 0; x < gameSize; x++) {
          for (int y = 0; y < gameSize; y++) {

            // CALCULATE LIVING CELLES
            int lc = 0;
            try { // left upper
              if (c[x-1][y-1].alive == true) lc++;
            } 
            catch(Exception e) {
            }

            try { //  upper
              if (c[x][y-1].alive == true) lc++;
            } 
            catch(Exception e) {
            }

            try { // right upper
              if (c[x+1][y-1].alive == true) lc++;
            } 
            catch(Exception e) {
            }

            try { // left
              if (c[x-1][y].alive == true) lc++;
            } 
            catch(Exception e) {
            }

            try { // right
              if (c[x+1][y].alive == true) lc++;
            } 
            catch(Exception e) {
            }

            try { // left down
              if (c[x-1][y+1].alive == true) lc++;
            } 
            catch(Exception e) {
            }

            try { // down
              if (c[x][y-1].alive == true) lc++;
            } 
            catch(Exception e) {
            }

            try { // right down
              if (c[x+1][y+1].alive == true) lc++;
            } 
            catch(Exception e) {
            }

            if (lc > 0) println(lc);
            c[x][y].calculate(lc);

            // DRAW CELLES
            if (c[x][y].alive) {
              fill(255);
            } else {
              fill(0);
            }
            rect(width/gameSize*x, height/gameSize*y, width/gameSize, height/gameSize);
          }
          if (keyPressed) isDraving = true;
        }
      }
    } else {

      if (mousePressed) {
        int mx = floor( mouseX /(width/gameSize));
        int my = floor( mouseY /(width/gameSize));

        c[mx][my].alive = true;
      }

      if (keyPressed) isDraving = false;

      for (int x = 0; x < gameSize; x++) {
        for (int y = 0; y < gameSize; y++) {
          // DRAW CELLES
          if (c[x][y].alive) {
            fill(255);
          } else {
            fill(0);
          }
          rect(width/gameSize*x, height/gameSize*y, width/gameSize, height/gameSize);
        }
      }
    }
  }


  class Cell {
    boolean alive;

    void calculate(int lN) {
      if (lN == 3 || lN == 2) {
        // NOTHING ;D
        alive = true;
        //println("ALIVE");
      }
      if (lN < 2) {
        alive = false;
      }
      if (lN > 3) {
        alive = false;
      }
    }
  }
Tagged:

Answers

  • Answer ✓

    you are modifying the same grid you are using to calculate.

    usually you have two grids and modify the one using values from the other. then swap them.

    the exceptions are ugly and lazy.

    and to format the code for the forum highlight it and press ctrl-o. (i've done it this time)

Sign In or Register to comment.