game of life
in
Programming Questions
•
11 months ago
Hello,
I'm trying to make the Game of Life. It gives me no errors but it doesn't do anything!
Here is my code..
- int sz = 100;
- int recSize = 5;
- int seeds = 30;
- int field[][] = new int[sz][sz];
- void setup() {
- frameRate(5);
- size(recSize*sz, recSize*sz);
- for ( int i = 0; i < seeds; i++) {
- field[int(random(0, sz))][int(random(0, sz))] = 1;
- }
- }
- void draw() {
- //noStroke();
- // background(0);
- drawField();
- replaceCells();
- }
- void replaceCells() {
- for ( int x = 1; x < sz-1; x++) {
- for ( int y = 1; y < sz-1; y++) {
- int AboveLeft = field[x-1][y-1];
- int AboveCenter = field[x][y-1];
- int AboveRight = field[x+1][y-1];
- int Left = field[x-1][y];
- int Right = field[x+1][y];
- int BottomLeft = field[x-1][y+1];
- int BottomCenter = field[x][y+1];
- int BottomRight = field[x+1][y+1];
- int sum = AboveLeft+AboveCenter+AboveRight+Left+Right+BottomLeft+BottomCenter+BottomRight;
- println(sum);
- if (field[x][y] == 1 && sum < 2) {
- field[x][y] = 0;
- }
- if (field[x][y] == 1 && sum == 2) {
- field[x][y] = 1;
- }
- if (field[x][y] == 1 && sum == 3) {
- field[x][y] = 1;
- }
- if (field[x][y] == 1 && sum > 3) {
- field[x][y] = 0;
- }
- if (field[x][y] == 0 && sum == 3) {
- field[x][y] = 1;
- }
- }
- }
- }
- void drawField() {
- for ( int c = 0; c < sz; c++) {
- for ( int d = 0; d < sz; d++) {
- stroke(100);
- if (field[c][d] == 1) {
- fill(255);
- }
- else {
- fill(0);
- }
- rect(c*recSize, d*recSize, recSize, recSize);
- }
- }
- }
1