perfect

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 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 3 rows');
17 }
18
19 for (let i = 0; i < 3; i++) {
20 if (!board[i] || board[i].length !== 3) {
21 throw new Error('Invalid board: each row must have 3 columns');
22 }
23 }
24
25 // Count X's and O's
26 let xCount = 0;
27 let oCount = 0;
28
29 for (let i = 0; i < 3; i++) {
30 for (let j = 0; j < 3; j++) {
31 const cell = board[i][j];
32 if (cell === 'X') xCount++;
33 else if (cell === 'O') oCount++;
34 else if (cell !== ' ') {
35 throw new Error('Invalid board: contains invalid characters');
36 }
37 }
38 }
39
40 // Edge Case: Invalid turn order - O started
41 if (oCount > xCount) {
42 throw new Error('Wrong turn order: O started');
43 }
44
45 // Edge Case: Invalid 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 checkWin = (player) => {
52 // Check rows
53 for (let i = 0; i < 3; i++) {
54 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
55 return true;
56 }
57 }
58
59 // Check columns
60 for (let j = 0; j < 3; j++) {
61 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
62 return true;
63 }
64 }
65
66 // Check diagonals
67 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
68 return true;
69 }
70
71 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
72 return true;
73 }
74
75 return false;
76 };
77
78 const xWins = checkWin('X');
79 const oWins = checkWin('O');
80
81 // Edge Case: Both players win (impossible in a valid 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 win
87 // If X wins, X should have exactly one more move than O
88 if (xWins && xCount !== oCount + 1) {
89 throw new Error('Impossible board: game should have ended after the game was won');
90 }
91
92 // Edge Case: Game continued after win
93 // If O wins, X should have exactly the same number of moves as O
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: Draw - board is full with no winner
104 if (xCount + oCount === 9) {
105 return 'draw';
106 }
107
108 // Edge Case: Ongoing game - board is not full and no winner
109 return 'ongoing';
110
111 // Handled Edge Cases:
112 // - Invalid board dimensions
113 // - Invalid characters on board
114 // - Wrong turn order (O started)
115 // - Wrong turn order (X went twice)
116 // - Both players winning
117 // - Game continued after win (X won but O played)
118 // - Game continued after win (O won but X played)
119 // - Draw (full board with no winner)
120 // - Ongoing game (incomplete board with no winner)
121};
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.