Finished
44//
55
66export 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 }
931 }
1032
1133 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;
1388 }
1489
1590 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]);
1797 }
1898}
99
100// Handled Edge Cases: null/undefined grid, empty grid, inconsistent row lengths, boundary cells, grid modification protection
Test NameStatus
empty matrix
Pass
live cells with zero live neighbors die
Pass
live cells with only one live neighbor die
Pass
live cells with two live neighbors stay alive
Pass
live cells with three live neighbors stay alive
Pass
dead cells with three live neighbors become alive
Pass
live cells with four or more neighbors die
Pass
bigger matrix
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.