|
| 1 | +// @ts-check |
| 2 | +/*------------------------------------------------------------------------------ |
| 3 | +Full description at: https://github.com/HackYourFuture/Assignments/tree/main/2-Browsers/Week1#exercise-6-conways-game-of-life |
| 4 | +
|
| 5 | +THIS IS A PREP EXERCISE FOR THE Q&A SESSION, IT SHOULD NOT BE PART OF THE ASSIGNMENT |
| 6 | +
|
| 7 | +Adapted from: https://spicyyoghurt.com/tutorials/javascript/conways-game-of-life-canvas |
| 8 | +Refactored from ES6 Class syntax to regular functions |
| 9 | +------------------------------------------------------------------------------*/ |
| 10 | +const CELL_SIZE = 10; |
| 11 | +const NUM_COLUMNS = 75; |
| 12 | +const NUM_ROWS = 40; |
| 13 | + |
| 14 | +/** |
| 15 | + * @typedef {Object} GridCell |
| 16 | + * @property {number} x |
| 17 | + * @property {number} y |
| 18 | + * @property {boolean} alive |
| 19 | + * @property {boolean} [nextAlive] |
| 20 | + */ |
| 21 | + |
| 22 | +/** @typedef {GridCell[]} GridRow */ |
| 23 | + |
| 24 | +/** |
| 25 | + * Create a cell with the given coordinates and randomly assign its begin state: |
| 26 | + * life or death |
| 27 | + * @param {number} x |
| 28 | + * @param {number} y |
| 29 | + * @returns {GridCell} |
| 30 | + */ |
| 31 | +function createCell(x, y) { |
| 32 | + const alive = Math.random() > 0.5; |
| 33 | + return { |
| 34 | + x, |
| 35 | + y, |
| 36 | + alive, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Create the game "engine" with a closure |
| 42 | + * @param {CanvasRenderingContext2D} context |
| 43 | + * @param {number} numRows |
| 44 | + * @param {number} numColumns |
| 45 | + * @returns |
| 46 | + */ |
| 47 | +export function createGame(context, numRows, numColumns) { |
| 48 | + /** @type {GridRow[]} */ |
| 49 | + const grid = []; |
| 50 | + |
| 51 | + // Create the grid as a two-dimensional array (i.e. an array of arrays) |
| 52 | + function createGrid() { |
| 53 | + for (let y = 0; y < numRows; y++) { |
| 54 | + /** @type {GridRow} */ |
| 55 | + const row = []; |
| 56 | + for (let x = 0; x < numColumns; x++) { |
| 57 | + const cell = createCell(x, y); |
| 58 | + row.push(cell); |
| 59 | + } |
| 60 | + grid.push(row); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Execute a callback for each cell in the grid |
| 66 | + * @param {(cell: GridCell) => void} callback |
| 67 | + */ |
| 68 | + function forEachCell(callback) { |
| 69 | + grid.forEach((row) => { |
| 70 | + row.forEach((cell) => callback(cell)); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Draw a cell onto the canvas |
| 76 | + * @param {GridCell} cell |
| 77 | + */ |
| 78 | + function drawCell(cell) { |
| 79 | + // Draw cell background |
| 80 | + context.fillStyle = '#303030'; |
| 81 | + context.fillRect( |
| 82 | + cell.x * CELL_SIZE, |
| 83 | + cell.y * CELL_SIZE, |
| 84 | + CELL_SIZE, |
| 85 | + CELL_SIZE |
| 86 | + ); |
| 87 | + |
| 88 | + if (cell.alive) { |
| 89 | + // Draw living cell inside background |
| 90 | + context.fillStyle = `rgb(24, 215, 236)`; |
| 91 | + context.fillRect( |
| 92 | + cell.x * CELL_SIZE + 1, |
| 93 | + cell.y * CELL_SIZE + 1, |
| 94 | + CELL_SIZE - 2, |
| 95 | + CELL_SIZE - 2 |
| 96 | + ); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Check the state of the cell at the given coordinates |
| 102 | + * @param {number} x |
| 103 | + * @param {number} y |
| 104 | + * @returns {0 | 1} |
| 105 | + */ |
| 106 | + function isAlive(x, y) { |
| 107 | + // Out-of-border cells are presumed dead |
| 108 | + if (x < 0 || x >= numColumns || y < 0 || y >= numRows) { |
| 109 | + return 0; |
| 110 | + } |
| 111 | + |
| 112 | + return grid[y][x].alive ? 1 : 0; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Count the number of living neighboring cells for a given cell |
| 117 | + * @param {GridCell} cell |
| 118 | + * @returns {number} |
| 119 | + */ |
| 120 | + function countLivingNeighbors(cell) { |
| 121 | + const { x, y } = cell; |
| 122 | + return ( |
| 123 | + isAlive(x - 1, y - 1) + |
| 124 | + isAlive(x, y - 1) + |
| 125 | + isAlive(x + 1, y - 1) + |
| 126 | + isAlive(x - 1, y) + |
| 127 | + isAlive(x + 1, y) + |
| 128 | + isAlive(x - 1, y + 1) + |
| 129 | + isAlive(x, y + 1) + |
| 130 | + isAlive(x + 1, y + 1) |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Update the state of the cells in the grid by applying the Game Of Life |
| 136 | + * rules on each cell. |
| 137 | + */ |
| 138 | + function updateGrid() { |
| 139 | + // Loop over all cells to determine their next state. |
| 140 | + forEachCell((cell) => { |
| 141 | + // Count number of living neighboring cells |
| 142 | + const numAlive = countLivingNeighbors(cell); |
| 143 | + |
| 144 | + if (numAlive === 2) { |
| 145 | + // Living cell remains living, dead cell remains dead |
| 146 | + cell.nextAlive = cell.alive; |
| 147 | + } else if (numAlive === 3) { |
| 148 | + // Dead cell becomes living, living cell remains living |
| 149 | + cell.nextAlive = true; |
| 150 | + } else { |
| 151 | + // Living cell dies, dead cell remains dead |
| 152 | + cell.nextAlive = false; |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + // Apply the newly computed state to the cells |
| 157 | + forEachCell((cell) => { |
| 158 | + cell.alive = cell.nextAlive ?? false; |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + // |
| 163 | + |
| 164 | + /** |
| 165 | + * Render a visual representation of the grid |
| 166 | + */ |
| 167 | + function renderGrid() { |
| 168 | + // Draw all cells in the grid |
| 169 | + forEachCell(drawCell); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Execute one game cycle |
| 174 | + */ |
| 175 | + function gameLoop() { |
| 176 | + // Update the state of cells in the grid |
| 177 | + updateGrid(); |
| 178 | + |
| 179 | + // Render the updated grid |
| 180 | + renderGrid(); |
| 181 | + |
| 182 | + // Schedule the next generation |
| 183 | + setTimeout(() => { |
| 184 | + window.requestAnimationFrame(gameLoop); |
| 185 | + }, 200); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Start the game |
| 190 | + */ |
| 191 | + function start() { |
| 192 | + // Create initial grid |
| 193 | + createGrid(); |
| 194 | + |
| 195 | + // Render the initial generation |
| 196 | + renderGrid(); |
| 197 | + |
| 198 | + // Kick-start the gameLoop |
| 199 | + window.requestAnimationFrame(gameLoop); |
| 200 | + } |
| 201 | + |
| 202 | + return { grid, updateGrid, start }; |
| 203 | +} |
| 204 | + |
| 205 | +function main() { |
| 206 | + // Resize the canvas to accommodate the desired number of cell rows and |
| 207 | + // columns |
| 208 | + const canvas = document.getElementById('canvas'); |
| 209 | + if (!(canvas instanceof HTMLCanvasElement)) { |
| 210 | + throw new Error('Canvas element not found'); |
| 211 | + } |
| 212 | + |
| 213 | + canvas.height = NUM_ROWS * CELL_SIZE; |
| 214 | + canvas.width = NUM_COLUMNS * CELL_SIZE; |
| 215 | + |
| 216 | + // Obtain a context that is needed to draw on the canvas |
| 217 | + const context = canvas.getContext('2d'); |
| 218 | + if (!(context instanceof CanvasRenderingContext2D)) { |
| 219 | + throw new Error('Context not found'); |
| 220 | + } |
| 221 | + |
| 222 | + // Create the game "engine" |
| 223 | + const { start } = createGame(context, NUM_ROWS, NUM_COLUMNS); |
| 224 | + |
| 225 | + // Start the game |
| 226 | + start(); |
| 227 | +} |
| 228 | + |
| 229 | +// ! Do not change or remove any code below |
| 230 | +try { |
| 231 | + window.addEventListener('load', main); |
| 232 | +} catch { |
| 233 | + // ignore if running in node with jest |
| 234 | +} |
0 commit comments