yahoo

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 board
10 * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state
11 * @throws {Error} - Throws an error for invalid board states
912 */
1013export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
14 // Validate board structure
15 if (!Array.isArray(board) || board.length !== 3) {
16 throw new Error('Invalid board: must have 3 rows');
17 }
18
19 for (const row of board) {
20 if (typeof row !== 'string' || row.length !== 3) {
21 throw new Error('Invalid board: each row must be a string of length 3');
22 }
23 }
24
25 // Count Xs and Os
26 let xCount = 0;
27 let oCount = 0;
28
29 for (const row of board) {
30 for (const cell of row) {
31 if (cell === 'X') xCount++;
32 else if (cell === 'O') oCount++;
33 else if (cell !== ' ') {
34 // Edge Case: Invalid characters on the board
35 throw new Error('Invalid board: contains invalid characters');
36 }
37 }
38 }
39
40 // Edge Case: Wrong turn order - O started
41 if (oCount > xCount) {
42 throw new Error('Wrong turn order: O started');
43 }
44
45 // Edge Case: Wrong turn order - X went twice in a row
46 if (xCount > oCount + 1) {
47 throw new Error('Wrong turn order: X went twice');
48 }
49
50 // Check for wins
51 const winPatterns = [
52 // Rows
53 [0, 1, 2],
54 [3, 4, 5],
55 [6, 7, 8],
56 // Columns
57 [0, 3, 6],
58 [1, 4, 7],
59 [2, 5, 8],
60 // Diagonals
61 [0, 4, 8],
62 [2, 4, 6]
63 ];
64
65 // Convert board to a flat array for easier checking
66 const flatBoard = board.join('');
67
68 let xWins = false;
69 let oWins = false;
70
71 for (const pattern of winPatterns) {
72 const [a, b, c] = pattern;
73 if (flatBoard[a] === 'X' && flatBoard[b] === 'X' && flatBoard[c] === 'X') {
74 xWins = true;
75 }
76 if (flatBoard[a] === 'O' && flatBoard[b] === 'O' && flatBoard[c] === 'O') {
77 oWins = true;
78 }
79 }
80
81 // Edge Case: Both players win (impossible in a real game)
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
87 // If X wins, O should not have played after that
88 if (xWins && oCount >= xCount) {
89 throw new Error('Impossible board: game should have ended after the game was won');
90 }
91
92 // Edge Case: Game continued after a win
93 // If O wins, X should not have played after that
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 || oWins) {
100 return 'win';
101 }
102
103 // Edge Case: Board is full (draw)
104 if (xCount + oCount === 9) {
105 return 'draw';
106 }
107
108 // Game is ongoing
109 return 'ongoing';
110
111 // Handled Edge Cases:
112 // - Invalid board structure (not 3x3)
113 // - Invalid characters on the board
114 // - Wrong turn order (O started)
115 // - Wrong turn order (X went twice)
116 // - Both players win (impossible)
117 // - Game continued after a win (X won but O played)
118 // - Game continued after a win (O won but X played)
119 // - Board is full (draw)
120};
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.