You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Week4/prep-exercises/2-game-of-life/README.md
+36-16Lines changed: 36 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -18,39 +18,59 @@ In the exercise code a new generation of cells replaces the previous one every 2
18
18
19
19
## Code walk-through
20
20
21
+
The JavaScript code is made up of four files, three of which contain JavaScript classes (one class per file) and a file containing a `main()` function.
22
+
23
+
### `class Cell`
24
+
25
+
This class represents a single cell.
26
+
27
+
<!--prettier-ignore-->
28
+
| Methods | Description |
29
+
|----------|-------------|
30
+
| 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 it next `alive` state. |
31
+
| draw() | Draws the cell on the canvas. The visual representation depends on whether the cell is alive or dead. |
32
+
33
+
### `class Grid`
34
+
35
+
This class manages the game grid, made up of cells.
36
+
21
37
<!--prettier-ignore-->
22
-
| Methods | Description |
38
+
| Methods | Description |
23
39
|----------|-------------|
24
-
|`constructor()`| Creates a two-dimensional array (i.e., an array of arrays) that represents a grid of cells that evolve over time. |
25
-
|`createCell()`| (`static` method) Creates a JavaScript object representing a cell with `x` (column number) and `y` (row number) properties and a boolean `aLive` property that is randomly initialized to `true` or `false`. |
40
+
|`constructor()`| Creates a two-dimensional array (i.e., an array of arrays) that represents a grid of cells that evolve over time. It keeps a reference the to the canvas context unto which cells will be drawn. |
26
41
|`forEachCell()`| Executes a callback function for each cell in the two-dimensional grid array, passing the cell as parameter to the callback. |
27
-
|`drawCell()`| Takes a cell object as a parameter and draws the cell on the canvas. The visual representation depends on whether the cell is alive or dead. |
28
42
|`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 dead. The function returns one if the given cell is alive or zero if its dead. |
29
43
|`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. |
30
-
|`updateGrid()`| Iterates through all cells of the grid and computes the new state of each cell by applying the rules of the Game Of Life. |
31
-
|`renderGrid()`| Iterates through all cells of the grid and draws each cell onto the canvas. |
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. |
45
+
|`render()`| Iterates through all cells of the grid and draws each cell onto the canvas. |
46
+
47
+
### `class Game`
48
+
49
+
<!--prettier-ignore-->
50
+
| Methods | Description |
51
+
|----------|-------------|
32
52
|`gameLoop()`| Executes one life cycle of the game (i.e., `updateGrid()` followed by `renderGrid()`) and then reschedules itself to run again after a delay. |
33
53
|`start()`| The `start()` function creates the initial grid, renders it to the web page by calling `renderGrid()` and calls `gameLoop()` to kickstart the game. |
34
54
35
-
The `main()` function gets a reference to the `canvas` element hard-coded in the `index.html` file and resizes the canvas to the desired size. It then instantiates a GameOfLife object and starts the game engine. The function `main()` itself is executed when the browser has finished loading the page.
55
+
The `main()` function gets a reference to the `canvas` element hard-coded in the `index.html` file and instantiates a `Game` object.
36
56
37
-
The diagram below visualizes the overall call hierarchy of the various functions. The `main()` function calls `createGame()`, which in turn creates a closure enclosing the `grid` array and a couple of functions that operate on that `grid`. Then, `main()` calls the `start()` function to start the game.
57
+
The diagram below visualizes the overall call hierarchy of the various classes and methods.
38
58
39
-
The `start()` method creates the initial grid, renders it to the web page by calling `renderGrid()` and calls `gameLoop()` to kickstart the game.
59
+

40
60
41
-
The `gameLoop()` method calls `updateGrid()` to update (each cell of) the grid according to the game rules (see above) and the calls `renderGrid()` to render the updated grid to the web page. It then schedules a call to itself using `setTimeout()`. This causes the game to keep evolving the grid according to the game rules every 200ms until the page is closed.
61
+
The `start()` method creates the initial grid and calls `gameLoop()` to kickstart the game.
42
62
43
-
Note: The use of [`window.requestAnimationFrame()`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) is not essential for the functioning of the game but helps to avoid screen flicker.
63
+
The `gameLoop()` method calls `grid.update()` to update (each cell of) the grid according to the game rules (see above) and the calls `grid.render()` to render the updated grid to the web page. It then schedules a call to itself using `setTimeout()`. This causes the game to keep evolving the grid according to the game rules every 200ms until the page is closed.
44
64
45
-

65
+
Note: The use of [`window.requestAnimationFrame()`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) is not essential for the functioning of the game but helps to avoid screen flicker.
46
66
47
67
### Exercise
48
68
49
-
In the supplied JavaScript code (file: `index.js`) the color of all living cells is a single shade of blue. This is in contrast to the illustration above where living cells have different shades of blue, depending on their life time. Your job is as follows:
69
+
In the supplied JavaScript code the color of all living cells is a single shade of blue. This is in contrast to the illustration above where living cells have different shades of blue, depending on their life time. Your job is as follows:
50
70
51
-
1. In function `createCell()`, add a numeric `lifeTime` property to the object and assign it the value of one if the cell is initially alive or zero if it is initially dead.
71
+
1. In the constructor of the `Cell` class, add a numeric `lifeTime` property to the object and assign it the value of `1` if the cell is initially alive or `0` if it is initially dead.
52
72
53
-
2. In function `drawCell()`, replace [`rgb()`](<https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb()>) with [`rgba()`](<https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgba()>) that adds a fourth parameter indicating `opacity` to the `rgb` value like this:
73
+
2. In `draw` method of the `Cell` class, replace [`rgb()`](<https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgb()>) with [`rgba()`](<https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/rgba()>) that adds a fourth parameter indicating `opacity` to the `rgb` value like this:
0 commit comments