kale

Finished
88 * @returns {string}
99 */
1010export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
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 X's and O's
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') {
29 xCount++;
30 } else if (cell === 'O') {
31 oCount++;
32 } else if (cell !== ' ') {
33 throw new Error('Invalid board: contains invalid characters');
34 }
35 }
36 }
37
38 // Edge Case: Wrong turn order - O started
39 if (oCount > xCount) {
40 throw new Error('Wrong turn order: O started');
41 }
42
43 // Edge Case: Wrong turn order - X went twice in a row
44 if (xCount > oCount + 1) {
45 throw new Error('Wrong turn order: X went twice');
46 }
47
48 // Check for wins
49 const winPatterns = [
50 // Rows
51 [[0,0], [0,1], [0,2]],
52 [[1,0], [1,1], [1,2]],
53 [[2,0], [2,1], [2,2]],
54 // Columns
55 [[0,0], [1,0], [2,0]],
56 [[0,1], [1,1], [2,1]],
57 [[0,2], [1,2], [2,2]],
58 // Diagonals
59 [[0,0], [1,1], [2,2]],
60 [[0,2], [1,1], [2,0]]
61 ];
62
63 let xWins = false;
64 let oWins = false;
65
66 for (const pattern of winPatterns) {
67 const [a, b, c] = pattern;
68 const valA = board[a[0]][a[1]];
69 const valB = board[b[0]][b[1]];
70 const valC = board[c[0]][c[1]];
71
72 if (valA !== ' ' && valA === valB && valB === valC) {
73 if (valA === 'X') {
74 xWins = true;
75 } else if (valA === 'O') {
76 oWins = true;
77 }
78 }
79 }
80
81 // Edge Case: Both players have winning lines
82 if (xWins && oWins) {
83 throw new Error('Impossible board: game should have ended after the game was won');
84 }
85
86 // Edge Case: Game continued after a win was achieved - X won but O kept playing
87 if (xWins && oCount >= xCount) {
88 throw new Error('Impossible board: game should have ended after the game was won');
89 }
90
91 // Edge Case: Game continued after a win was achieved - O won but X kept playing
92 if (oWins && xCount > oCount) {
93 throw new Error('Impossible board: game should have ended after the game was won');
94 }
95
96 // Determine game state
97 if (xWins || oWins) {
98 return 'win';
99 }
100
101 // Edge Case: Full board with no winner (draw)
102 if (xCount + oCount === 9) {
103 return 'draw';
104 }
105
106 // Edge Case: Empty board or incomplete board with no winner (ongoing)
107 return 'ongoing';
108
109 // Handled Edge Cases:
110 // - Wrong turn order (O started)
111 // - Wrong turn order (X went twice)
112 // - Both players have winning lines
113 // - Game continued after X win
114 // - Game continued after O win
115 // - Full board with no winner (draw)
116 // - Empty or incomplete board with no winner (ongoing)
117 // - Invalid board dimensions
118 // - Invalid characters on board
12119};
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.