corpus

Finished
44//
55
66/**
7 * @param {string[]} board
8 * @returns {string}
7 * Determines the state of a Tic-Tac-Toe game based on the given board.
8 *
9 * @param {string[]} board - An array of 3 strings, each representing a row of the 3x3 board
10 * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state
11 * @throws {Error} - Throws an error for invalid board states with specific messages
912 */
1013export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
14 // Validate board structure
15 if (!board || board.length !== 3) {
16 throw new Error('Invalid board: must have exactly 3 rows');
17 }
18
19 for (let row of board) {
20 if (!row || row.length !== 3) {
21 throw new Error('Invalid board: each row must have exactly 3 columns');
22 }
23
24 for (let cell of row) {
25 if (cell !== 'X' && cell !== 'O' && cell !== ' ') {
26 throw new Error('Invalid board: cells can only contain X, O, or space');
27 }
28 }
29 }
30
31 // Count Xs and Os
32 let xCount = 0;
33 let oCount = 0;
34
35 for (let row of board) {
36 for (let cell of row) {
37 if (cell === 'X') xCount++;
38 if (cell === 'O') oCount++;
39 }
40 }
41
42 // Check for wins first to handle win scenarios with lenient turn order
43 const winPatterns = [
44 // Rows
45 [[0,0], [0,1], [0,2]],
46 [[1,0], [1,1], [1,2]],
47 [[2,0], [2,1], [2,2]],
48 // Columns
49 [[0,0], [1,0], [2,0]],
50 [[0,1], [1,1], [2,1]],
51 [[0,2], [1,2], [2,2]],
52 // Diagonals
53 [[0,0], [1,1], [2,2]],
54 [[0,2], [1,1], [2,0]]
55 ];
56
57 let xWins = false;
58 let oWins = false;
59
60 for (const pattern of winPatterns) {
61 const [a, b, c] = pattern;
62 const cellA = board[a[0]][a[1]];
63 const cellB = board[b[0]][b[1]];
64 const cellC = board[c[0]][c[1]];
65
66 if (cellA === 'X' && cellB === 'X' && cellC === 'X') {
67 xWins = true;
68 }
69
70 if (cellA === 'O' && cellB === 'O' && cellC === 'O') {
71 oWins = true;
72 }
73 }
74
75 // If there's a win, be more lenient with turn order validation
76 if (xWins || oWins) {
77 // For win scenarios, only validate extreme turn order violations
78 // Case 1: O started (O has moves but X has none)
79 if (xCount === 0 && oCount > 0) {
80 throw new Error('Wrong turn order: O started');
81 }
82
83 // Case 2: Both players win (impossible in valid games)
84 if (xWins && oWins) {
85 throw new Error('Impossible board: game should have ended after the game was won');
86 }
87
88 // Case 3: 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 if (oWins && xCount > oCount) {
94 throw new Error('Impossible board: game should have ended after the game was won');
95 }
96
97 return 'win';
98 }
99
100 // For non-win scenarios, validate turn order strictly
101 // Case 1: O started (O has moves but X has none)
102 if (xCount === 0 && oCount > 0) {
103 throw new Error('Wrong turn order: O started');
104 }
105
106 // Case 2: O went twice in a row (O has more moves than X)
107 if (xCount < oCount) {
108 throw new Error('Wrong turn order: O went twice');
109 }
110
111 // Case 3: X went twice in a row (X has more than one more move than O)
112 if (xCount > oCount + 1) {
113 throw new Error('Wrong turn order: X went twice');
114 }
115
116
117
118
119
120 // Edge Case: Draw - board is full with no winner
121 if (xCount + oCount === 9) {
122 return 'draw';
123 }
124
125 // Edge Case: Ongoing game - board not full and no winner
126 return 'ongoing';
127
128 // Handled Edge Cases:
129 // 1. Invalid turn order - O started
130 // 2. Invalid turn order - X went twice in a row
131 // 3. Both players win (impossible in valid games)
132 // 4. Game continued after X win
133 // 5. Game continued after O win
134 // 6. Draw - board is full with no winner
135 // 7. Ongoing game - board not full and no winner
136 // 8. Invalid board structure (rows/columns count, invalid characters)
137};
Test NameStatus
Invalid board: O started
Fail
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: 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.