viola

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 * Each string contains 3 characters: 'X', 'O', or ' ' (empty space).
11 * @returns {string} - Returns one of the following states:
12 * 'ongoing' - Game is still in progress.
13 * 'draw' - Game ended in a draw.
14 * 'win' - Game ended with a win.
15 * @throws {Error} - Throws an error with a specific message if the board is invalid.
16 *
17 * Handled Edge Cases:
18 * - Invalid turn order (O starts, X plays twice in a row)
19 * - Game continued after a win
20 * - Board with incorrect number of X's and O's
921 */
1022export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
23 // Validate board structure
24 if (!Array.isArray(board) || board.length !== 3) {
25 throw new Error('Invalid board: must be an array of 3 strings');
26 }
27
28 for (let i = 0; i < 3; i++) {
29 if (typeof board[i] !== 'string' || board[i].length !== 3) {
30 throw new Error('Invalid board: each row must be a string of 3 characters');
31 }
32 }
33
34 // Count X's and O's
35 let countX = 0;
36 let countO = 0;
37
38 for (let i = 0; i < 3; i++) {
39 for (let j = 0; j < 3; j++) {
40 const char = board[i][j];
41 if (char === 'X') countX++;
42 else if (char === 'O') countO++;
43 else if (char !== ' ' && char !== '.') {
44 throw new Error('Invalid board: contains invalid characters');
45 }
46 }
47 }
48
49 // Edge Case: Invalid turn order - O started
50 if (countO > countX) {
51 throw new Error('Wrong turn order: O started');
52 }
53
54 // Edge Case: Invalid turn order - X played twice in a row
55 if (countX > countO + 1) {
56 throw new Error('Wrong turn order: X went twice');
57 }
58
59 // Check for wins
60 const wins = (player) => {
61 // Check rows
62 for (let i = 0; i < 3; i++) {
63 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
64 return true;
65 }
66 }
67
68 // Check columns
69 for (let j = 0; j < 3; j++) {
70 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
71 return true;
72 }
73 }
74
75 // Check diagonals
76 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
77 return true;
78 }
79
80 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
81 return true;
82 }
83
84 return false;
85 };
86
87 const xWins = wins('X');
88 const oWins = wins('O');
89
90 // Edge Case: Both players win (impossible in a valid game)
91 if (xWins && oWins) {
92 throw new Error('Impossible board: game should have ended after the game was won');
93 }
94
95 // Edge Case: Game continued after a win
96 if (xWins && countO >= countX) {
97 throw new Error('Impossible board: game should have ended after the game was won');
98 }
99
100 // Edge Case: Game continued after a win
101 if (oWins && countX > countO) {
102 throw new Error('Impossible board: game should have ended after the game was won');
103 }
104
105 // Determine game state
106 if (xWins || oWins) {
107 return 'win';
108 }
109
110 // Edge Case: Board is full (draw)
111 if (countX + countO === 9) {
112 return 'draw';
113 }
114
115 // Game is ongoing
116 return 'ongoing';
12117};
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.