| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class GameOfLife { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor(grid) { |
| 8 | // Edge Case: null or undefined grid |
| 9 | if (grid === null || grid === undefined) { |
| 10 | this.grid = []; |
| 11 | return; |
| 12 | } |
| 13 | |
| 14 | // Edge Case: empty grid |
| 15 | if (grid.length === 0) { |
| 16 | this.grid = []; |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | // Edge Case: grid with inconsistent row lengths |
| 21 | const numRows = grid.length; |
| 22 | const numCols = grid[0].length; |
| 23 | this.grid = []; |
| 24 | for (let i = 0; i < numRows; i++) { |
| 25 | // Edge Case: row with incorrect length |
| 26 | if (grid[i].length !== numCols) { |
| 27 | throw new Error('All rows must have the same length'); |
| 28 | } |
| 29 | this.grid.push([...grid[i]]); |
| 30 | } |
| 9 | 31 | } |
| 10 | 32 | |
| 11 | 33 | tick() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 34 | // Edge Case: empty grid |
| 35 | if (this.grid.length === 0) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | const numRows = this.grid.length; |
| 40 | const numCols = this.grid[0].length; |
| 41 | const newGrid = Array(numRows).fill().map(() => Array(numCols).fill(0)); |
| 42 | |
| 43 | for (let i = 0; i < numRows; i++) { |
| 44 | for (let j = 0; j < numCols; j++) { |
| 45 | const liveNeighbors = this.countLiveNeighbors(i, j); |
| 46 | const isAlive = this.grid[i][j] === 1; |
| 47 | |
| 48 | // Any live cell with two or three live neighbors lives on |
| 49 | if (isAlive && (liveNeighbors === 2 || liveNeighbors === 3)) { |
| 50 | newGrid[i][j] = 1; |
| 51 | } |
| 52 | // Any dead cell with exactly three live neighbors becomes a live cell |
| 53 | else if (!isAlive && liveNeighbors === 3) { |
| 54 | newGrid[i][j] = 1; |
| 55 | } |
| 56 | // All other cells die or stay dead |
| 57 | else { |
| 58 | newGrid[i][j] = 0; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | this.grid = newGrid; |
| 64 | } |
| 65 | |
| 66 | countLiveNeighbors(row, col) { |
| 67 | let count = 0; |
| 68 | const numRows = this.grid.length; |
| 69 | const numCols = this.grid[0].length; |
| 70 | |
| 71 | // Check all 8 neighboring cells |
| 72 | for (let i = -1; i <= 1; i++) { |
| 73 | for (let j = -1; j <= 1; j++) { |
| 74 | // Skip the cell itself |
| 75 | if (i === 0 && j === 0) continue; |
| 76 | |
| 77 | const newRow = row + i; |
| 78 | const newCol = col + j; |
| 79 | |
| 80 | // Edge Case: boundary cells - check if neighbor is within grid bounds |
| 81 | if (newRow >= 0 && newRow < numRows && newCol >= 0 && newCol < numCols) { |
| 82 | count += this.grid[newRow][newCol]; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return count; |
| 13 | 88 | } |
| 14 | 89 | |
| 15 | 90 | state() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 91 | // Edge Case: return a deep copy to prevent external modification |
| 92 | if (this.grid.length === 0) { |
| 93 | return []; |
| 94 | } |
| 95 | |
| 96 | return this.grid.map(row => [...row]); |
| 17 | 97 | } |
| 18 | 98 | } |
| 99 | |
| 100 | // Handled Edge Cases: null/undefined grid, empty grid, inconsistent row lengths, boundary cells, grid modification protection |