uv

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 character is either 'X', 'O', or ' ' (empty space).
11 * @returns {string} - Returns one of 'win', 'draw', or 'ongoing'.
12 * @throws {Error} - Throws an error for invalid board states with specific messages.
913 */
1014export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
15 // Validate board structure
16 if (!Array.isArray(board) || board.length !== 3) {
17 throw new Error('Invalid board: must have 3 rows');
18 }
19
20 for (let i = 0; i < 3; i++) {
21 if (typeof board[i] !== 'string' || board[i].length !== 3) {
22 throw new Error('Invalid board: each row must be a string of length 3');
23 }
24 for (let j = 0; j < 3; j++) {
25 const char = board[i][j];
26 if (char !== 'X' && char !== 'O' && char !== ' ') {
27 throw new Error('Invalid board: contains invalid characters');
28 }
29 }
30 }
31
32 // Count Xs and Os
33 let countX = 0;
34 let countO = 0;
35
36 for (let i = 0; i < 3; i++) {
37 for (let j = 0; j < 3; j++) {
38 if (board[i][j] === 'X') countX++;
39 if (board[i][j] === 'O') countO++;
40 }
41 }
42
43 // Check for wins
44 const wins = (player) => {
45 // Check rows
46 for (let i = 0; i < 3; i++) {
47 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
48 return true;
49 }
50 }
51
52 // Check columns
53 for (let j = 0; j < 3; j++) {
54 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
55 return true;
56 }
57 }
58
59 // Check diagonals
60 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
61 return true;
62 }
63
64 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
65 return true;
66 }
67
68 return false;
69 };
70
71 const xWins = wins('X');
72 const oWins = wins('O');
73
74 // Validate turn order and game state
75
76 // If someone wins, check if the game was valid
77 if (xWins || oWins) {
78 // If both players win, it's impossible
79 if (xWins && oWins) {
80 throw new Error('Impossible board: game should have ended after the game was won');
81 }
82
83 // If X wins
84 if (xWins) {
85 // Check if O continued playing after X won
86 // X wins on their nth move, so O should have at most (n-1) moves
87 // If O has made as many or more moves than X, the game continued after X won
88 if (countO >= countX) {
89 throw new Error('Impossible board: game should have ended after the game was won');
90 }
91 return 'win';
92 }
93
94 // If O wins
95 if (oWins) {
96 // Check if X continued playing after O won
97 // O wins on their nth move, so X should have at most n moves
98 // If X has made more moves than O, the game continued after O won
99 if (countX > countO) {
100 throw new Error('Impossible board: game should have ended after the game was won');
101 }
102 return 'win';
103 }
104 }
105
106 // Now validate turn order for non-winning games
107 // Edge Case: Invalid turn order - O started the game (O has moves but X has none)
108 if (countO > 0 && countX === 0) {
109 throw new Error('Wrong turn order: O started');
110 }
111
112 // Edge Case: Invalid turn order - O has too many moves (O went twice)
113 if (countO > countX) {
114 throw new Error('Wrong turn order: O went twice');
115 }
116
117 // Edge Case: Invalid turn order - X has more than one extra move
118 if (countX > countO + 1) {
119 throw new Error('Wrong turn order: X went twice');
120 }
121
122 // Determine game state for non-winning boards
123 // Edge Case: Board is full with no winner (draw)
124 if (countX + countO === 9) {
125 return 'draw';
126 }
127
128 // Edge Case: Game is not finished yet (ongoing)
129 return 'ongoing';
12130};
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.