hope

Finished
88 * @returns {string}
99 */
1010export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
11 // Validate board dimensions
12 if (board.length !== 3) {
13 throw new Error('Invalid board: must have 3 rows');
14 }
15
16 for (let row of board) {
17 if (row.length !== 3) {
18 throw new Error('Invalid board: each row must have 3 columns');
19 }
20 }
21
22 // Count Xs and Os
23 let xCount = 0;
24 let oCount = 0;
25
26 for (let row of board) {
27 for (let cell of row) {
28 if (cell === 'X') xCount++;
29 else if (cell === 'O') oCount++;
30 else if (cell !== ' ') {
31 // Edge Case: Invalid characters on the board
32 throw new Error('Invalid board: contains invalid characters');
33 }
34 }
35 }
36
37 // Edge Case: Wrong turn order - O started
38 if (oCount > xCount) {
39 throw new Error('Wrong turn order: O started');
40 }
41
42 // Edge Case: Wrong turn order - X went twice
43 if (xCount > oCount + 1) {
44 throw new Error('Wrong turn order: X went twice');
45 }
46
47 // Check for wins
48 const winPatterns = [
49 // Rows
50 [[0,0], [0,1], [0,2]],
51 [[1,0], [1,1], [1,2]],
52 [[2,0], [2,1], [2,2]],
53 // Columns
54 [[0,0], [1,0], [2,0]],
55 [[0,1], [1,1], [2,1]],
56 [[0,2], [1,2], [2,2]],
57 // Diagonals
58 [[0,0], [1,1], [2,2]],
59 [[0,2], [1,1], [2,0]]
60 ];
61
62 let xWins = false;
63 let oWins = false;
64
65 for (const pattern of winPatterns) {
66 const [a, b, c] = pattern;
67 const [rowA, colA] = a;
68 const [rowB, colB] = b;
69 const [rowC, colC] = c;
70
71 const cellA = board[rowA][colA];
72 const cellB = board[rowB][colB];
73 const cellC = board[rowC][colC];
74
75 if (cellA === 'X' && cellB === 'X' && cellC === 'X') {
76 xWins = true;
77 }
78 if (cellA === 'O' && cellB === 'O' && cellC === 'O') {
79 oWins = true;
80 }
81 }
82
83 // Edge Case: Both players win (impossible in a valid game)
84 if (xWins && oWins) {
85 throw new Error('Impossible board: game should have ended after the game was won');
86 }
87
88 // Edge Case: Game continued after win
89 if (xWins && oCount >= xCount) {
90 throw new Error('Impossible board: game should have ended after the game was won');
91 }
92
93 // Edge Case: Game continued after win
94 if (oWins && xCount > oCount) {
95 throw new Error('Impossible board: game should have ended after the game was won');
96 }
97
98 // Determine game state
99 if (xWins) {
100 return 'win';
101 }
102
103 if (oWins) {
104 return 'win';
105 }
106
107 // Edge Case: Draw - board is full with no winner
108 if (xCount + oCount === 9) {
109 return 'draw';
110 }
111
112 // Edge Case: Ongoing game - board is not full and no winner
113 return 'ongoing';
114
115 // Handled Edge Cases:
116 // 1. Invalid characters on the board
117 // 2. Wrong turn order - O started
118 // 3. Wrong turn order - X went twice
119 // 4. Both players win (impossible in a valid game)
120 // 5. Game continued after X win
121 // 6. Game continued after O win
122 // 7. Draw - board is full with no winner
123 // 8. Ongoing game - board is not full and no winner
124 // 9. Invalid board dimensions
125};
Test NameStatus
Finished game where X won via left column victory
Pass
Finished game where X won via middle column victory
Pass
Finished game where X won via right column victory
Pass
Finished game where O won via left column victory
Pass
Finished game where O won via middle column victory
Pass
Finished game where O won via right column victory
Pass
Finished game where X won via top row victory
Pass
Finished game where X won via middle row victory
Pass
Finished game where X won via bottom row victory
Pass
Finished game where O won via top row victory
Pass
Finished game where O won via middle row victory
Pass
Finished game where O won via bottom row victory
Pass
Finished game where X won via falling diagonal victory
Pass
Finished game where X won via rising diagonal victory
Pass
Finished game where O won via falling diagonal victory
Pass
Finished game where O won via rising diagonal victory
Pass
Finished game where X won via a row and a column victory
Pass
Finished game where X won via two diagonal victories
Pass
Draw
Pass
Another draw
Pass
Ongoing game: one move in
Pass
Ongoing game: two moves in
Pass
Ongoing game: five moves in
Pass
Invalid board: X went twice
Pass
Invalid board: O started
Pass
Invalid board: X won and O kept playing
Pass
Invalid board: players kept playing after a win
Pass

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