Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32ee8fd

Browse files
remarcmijstasel
authored andcommitted
Move live or die logic to Cell class
1 parent c365297 commit 32ee8fd

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

Week4/prep-exercises/2-game-of-life/Cell.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,21 @@ export default class Cell {
3737
);
3838
}
3939
}
40+
41+
liveAndLetDie(aliveNeighbors) {
42+
if (aliveNeighbors === 2) {
43+
// Living cell remains living, dead cell remains dead
44+
this.nextAlive = this.alive;
45+
} else if (aliveNeighbors === 3) {
46+
// Dead cell becomes living, living cell remains living
47+
this.nextAlive = true;
48+
} else {
49+
// Living cell dies, dead cell remains dead
50+
this.nextAlive = false;
51+
}
52+
}
53+
54+
update() {
55+
this.alive = this.nextAlive;
56+
}
4057
}

Week4/prep-exercises/2-game-of-life/Grid.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,11 @@ export default class Grid {
5252

5353
update() {
5454
this.forEachCell((cell) => {
55-
const numAlive = this.countLivingNeighbors(cell);
56-
57-
if (numAlive === 2) {
58-
// Living cell remains living, dead cell remains dead
59-
cell.nextAlive = cell.alive;
60-
} else if (numAlive === 3) {
61-
// Dead cell becomes living, living cell remains living
62-
cell.nextAlive = true;
63-
} else {
64-
// Living cell dies, dead cell remains dead
65-
cell.nextAlive = false;
66-
}
55+
const aliveNeighbors = this.countLivingNeighbors(cell);
56+
cell.liveAndLetDie(aliveNeighbors);
6757
});
6858

69-
this.forEachCell((cell) => {
70-
cell.alive = cell.nextAlive;
71-
});
59+
this.forEachCell((cell) => cell.update());
7260
}
7361

7462
render(context) {

Week4/prep-exercises/2-game-of-life/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ This class represents a single cell.
2929
|----------|-------------|
3030
| constructor() | Initializes the cell's `x` and `y` coordinates from arguments passed to the constructor. It randomly sets the initial `alive` boolean state of the cell and initializes its next `alive` state. |
3131
| draw() | Draws the cell on the canvas. The visual representation depends on whether the cell is alive or dead. |
32+
| liveAndLetDie() | Determines the next state (alive or dead) depending on the number of living neighbors of the cell, by applying the rules of the Game Of Life. |
33+
| update() | Updates the state of the cell (alive or dead) as set previously by `liveAndLetDie()`.
3234

3335
### `class Grid`
3436

@@ -41,7 +43,7 @@ This class manages the game grid, made up of cells.
4143
| `forEachCell()` | Executes a callback function for each cell in the two-dimensional grid array, passing the cell as a parameter to the callback. |
4244
| `isAlive()` | Determines whether a cell at the given coordinates is alive or dead. The coordinates could potentially be off-grid. Off-grid cells are presumed to be dead. The function returns one if the given cell is alive or zero if its dead. |
4345
| `countLivingNeighbors()` | Counts the number of living neighbors for a given cell. Each cell has eight neighbors, some of which may be off-grid if the cell is located at an edge or a corner of the grid. |
44-
| `update()` | Iterates through all cells of the grid and computes the new state of each cell by applying the rules of the Game Of Life. |
46+
| `update()` | Iterates through all cells of the grid and computes the new state of each cell. |
4547
| `render()` | Iterates through all cells of the grid and draws each cell onto the canvas. |
4648

4749
### `class Game`
@@ -83,7 +85,7 @@ In the supplied JavaScript code the color of all living cells is a single shade
8385
| 3 | 0.75 |
8486
| 4+ | 1 |
8587

86-
3. In the `update` method of the `Grid` class add code to update the `lifeTime` value of each cell:
88+
3. In the `liveAndLetDie()` method of the `Cell` class add code to update the `lifeTime` value of each cell:
8789

8890
- A living cell that remains living should have its `lifeTime` incremented by one.
8991
- A living cell that dies should have its `lifeTime` reset to zero.

0 commit comments

Comments
 (0)