conway's game of life fails
in
Programming Questions
•
1 year ago
My conway's game of life looks cool... but for some reason it doesn't act right.
As far as I know, these are the correct rules...
So why isn't it acting correctly?
(it does look cool though)
My code:
As far as I know, these are the correct rules...
So why isn't it acting correctly?
(it does look cool though)
My code:
- boolean cells[][];
- boolean newCells[][];
- int rows, cols;
- void setup() {
- size(400, 400);
- rows = height / 10;
- cols = width / 10;
- cells = new boolean[cols][rows];
- newCells = new boolean[cols][rows];
- for (int i = 0; i < cols; i++) {
- for (int j = 0; j < rows; j++) {
- if (int(random(0, 3)) == 1)
- cells[i][j] = true;
- else
- cells[i][j] = false;
- }
- }
- frameRate(30);
- }
- void draw() {
- background(0);
- for (int i = 0; i < cols; i++) { //loop through x of array
- for (int j = 0; j < rows; j++) { //loop through y of array
- if (cells[i][j])
- rect(i * 10, j * 10, 10, 10); //draw a cell if it's active
- int adjacent = 0; //the number of adjacent cells
- for (int k = i - 1; k <= i + 1; k++) { //Loop through adjacent cells on the x axis
- if (k < cols && k >= 0) { //no arrayIndexOutOfBoundsExceptions
- for (int l = j - 1; l <= j + 1; l++) { //Loop through adjacent cells on the y axis
- if (l < rows && l >= 0) {
- if (cells[k][l])
- adjacent++; //that's one more adjacent cell! Yay!
- }
- }
- }
- }
- //now for rule calculation...
- if (cells[i][j]) {
- if (adjacent < 2 || adjacent > 3) //lonely or overcrowded?
- newCells[i][j] = false;
- else
- newCells[i][j] = true; //two or three friends is good :D
- } else { //dead :(
- if (adjacent == 3)
- newCells[i][j] = true; //reincarnation! :D
- else
- newCells[i][j] = false; //still dead. :(
- }
- }
- }
- arrayCopy(newCells, cells);
- }
Thanks in advance.
1