# Game of life code error

**URL:** <https://discourse.processing.org/t/game-of-life-code-error/20238>\
**Category:** Coding Questions\
**Created:** [April 28, 2020, 2:42pm UTC](https://discourse.processing.org/t/game-of-life-code-error/20238 "2020-04-28T14:42:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallavi24](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@pallavi24](https://discourse.processing.org/u/pallavi24)\
**Post date:** [April 28, 2020, 2:42pm UTC](https://discourse.processing.org/t/game-of-life-code-error/20238/1 "2020-04-28T14:42:32Z")

</div>

I m trying to make the game of life on p5.js. I have used a 2d array of CA class object called ‘cell’ and saving every iteration in ‘new\_cell’ and finally copying back the values from ‘new \_cell’ to ‘cell’. The code works fine when i create the object ‘new\_cell’ in the draw loop but gives a weird output when I create the new\_cell object with the cell object in the setup loop.(i have commented out the statements for new\_cell in setup). I am unable to understand why does it give wrong value when I initialize in setup versus in draw loop when i anyway reassign the values in every draw loop.

```auto
let row_tot, col_tot; //total row and col
let cell = [];

let spacer = 10; //cell size

function setup() {
	createCanvas(500, 500);
	frameRate(10);

	//row_tot and col_tot
	row_tot = height / spacer;
	col_tot = width / spacer;

	//initialize cell
	for (let i = 0; i < col_tot; i++) {
		cell[i] = [];
		//	next_cell[i] = [];

		for (let j = 0; j < row_tot; j++) {

			cell[i][j] = new CA();
			//	next_cell[i][j] = new CA();
		}
	}
}

function draw() {

	background(0); //black background

	//initialize cell
	let next_cell = [];
	for (let i = 0; i < col_tot; i++) {

		next_cell[i] = [];

		for (let j = 0; j < row_tot; j++) {
			next_cell[i][j] = new CA();
		}
	}

	//rule
	for (let i = 0; i < col_tot; i++) {
		for (let j = 0; j < row_tot; j++) {

			//edge case

			//count the neighbour
			let sum = 0;

			for (let p = -1; p <= 1; p++) {
				for (let q = -1; q <= 1; q++) {
					let col = (i + p + col_tot) % col_tot;
					let row = (j + q + row_tot) % row_tot;
					sum += cell[col][row].state;
				}
			}
			sum -= cell[i][j].state;
			
			//Game of life rule
			if (cell[i][j].state == 1 && (sum < 2 || sum > 3)) next_cell[i][j].death(); //death
			else if ((cell[i][j].state == 0) && (sum == 3)) next_cell[i][j].birth(); //birth
			else next_cell[i][j] = cell[i][j]; //stasis

		}
	}

	//update old
	cell = next_cell;

	//render
	for (let i = 0; i < col_tot; i++) {
		for (let j = 0; j < row_tot; j++) {
			cell[i][j].show(i, j);
		}
	}
}

class CA {
	constructor() {
		this.state = random(1) < .5 ? 0 : 1;
	}

	show(a, b) {
		let p = a * spacer;
		let q = b * spacer;

		//draw the rec
		if (this.state == 1) {
			noStroke();
			fill(255);
			rect(p, q, spacer, spacer);
		}
	}

	death() {
		this.state = 0;
	}

	birth() {
		this.state = 1;
	}

}

```

---

<div class="post-metadata">

**Author:** ![Yom](https://avatars.discourse-cdn.com/v4/letter/y/c4cdca/32.png) [@Yom](https://discourse.processing.org/u/Yom)\
**Post date:** [April 29, 2020, 12:06am UTC](https://discourse.processing.org/t/game-of-life-code-error/20238/2 "2020-04-29T00:06:13Z")

</div>

Hello,

Try move “let next\_cell = [];” at the top and use “next\_cell = [];” in draw()

But it is useless to initialize the next\_cells in setup() if you immediately re-initialize them at the start of draw()…

---

<div class="post-metadata">

**Author:** ![pallavi24](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@pallavi24](https://discourse.processing.org/u/pallavi24)\
**Post date:** [April 29, 2020, 5:18am UTC](https://discourse.processing.org/t/game-of-life-code-error/20238/3 "2020-04-29T05:18:51Z")

</div>

If i initialize ‘next\_cell’ in setup then i wont be re-initializing in draw. I would just reassign the values everytime. But that gives wrong out put. I cant understand why

---

<div class="post-metadata">

**Author:** ![Sven](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sven/32/8293_2.png) [@Sven](https://discourse.processing.org/u/Sven)\
**Post date:** [April 30, 2020, 12:53pm UTC](https://discourse.processing.org/t/game-of-life-code-error/20238/4 "2020-04-30T12:53:58Z")

</div>

The reason why you won’t get the result you expect when initializing `next_cell` in `setup` is this line:

```javascript
//update old
cell = next_cell;

```

But why? Well, that line does **not** assign a copy of the `next_cell` array to the `cell` variable (as one may expect). It makes the `cell` variable reference (point to) the same array as the `next_cell` variable. After that line executes, `cell` and `next_cell` points to the same array.

```javascript
next_cell[0][0] = 9;

// This prints 9 to the console.
console.log(cell[0][0]);

```

Your life program ends up with just one state. That’s probably not what you want. If the above behavior is new to you, read up on the difference between [primitive and reference types in JavasScript](https://www.oreilly.com/library/view/javascript-the-definitive/0596101996/ch04.html#jscript5-CHP-4-SECT-4).

---

<div class="post-metadata">

**Author:** ![pallavi24](https://avatars.discourse-cdn.com/v4/letter/p/91b2a8/32.png) [@pallavi24](https://discourse.processing.org/u/pallavi24)\
**Post date:** [May 2, 2020, 2:40pm UTC](https://discourse.processing.org/t/game-of-life-code-error/20238/5 "2020-05-02T14:40:29Z")

</div>

Thank you so much for making it clear
