couple

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 representing the 3x3 game board.
10 * Each string has 3 characters which can be 'X', 'O', or ' '.
11 * @returns {string} - Returns 'win' if a player has won, 'draw' if the game is a draw,
12 * or 'ongoing' if the game is still in progress.
13 * @throws {Error} - Throws an error with a specific message if the board is invalid.
14 *
15 * Handled Edge Cases:
16 * - Board with wrong turn order (O starts or players take turns incorrectly)
17 * - Board where game continued after a win
18 * - Board with invalid characters
19 * - Board with incorrect dimensions
920 */
1021export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
22 // Validate board structure
23 if (!Array.isArray(board) || board.length !== 3) {
24 throw new Error('Invalid board: must be an array of 3 strings');
25 }
26
27 // Edge Case: Board with incorrect string lengths
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 length 3');
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') {
42 countX++;
43 } else if (char === 'O') {
44 countO++;
45 } else if (char !== ' ') {
46 // Edge Case: Board with invalid characters
47 throw new Error('Invalid board: contains invalid characters');
48 }
49 }
50 }
51
52 // Validate turn order
53 // Edge Case: Board with wrong turn order (O starts)
54 if (countO > countX) {
55 throw new Error('Wrong turn order: O started');
56 }
57
58 // Edge Case: Board with wrong turn order (player went twice)
59 if (countX > countO + 1) {
60 throw new Error('Wrong turn order: X went twice');
61 }
62
63 // Check for wins
64 const wins = (player) => {
65 // Check rows
66 for (let i = 0; i < 3; i++) {
67 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
68 return true;
69 }
70 }
71
72 // Check columns
73 for (let j = 0; j < 3; j++) {
74 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
75 return true;
76 }
77 }
78
79 // Check diagonals
80 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
81 return true;
82 }
83
84 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
85 return true;
86 }
87
88 return false;
89 };
90
91 const xWins = wins('X');
92 const oWins = wins('O');
93
94 // Edge Case: Board where both players win (impossible in a valid game)
95 if (xWins && oWins) {
96 throw new Error('Impossible board: game should have ended after the game was won');
97 }
98
99 // Edge Case: Board where game continued after a win
100 if (xWins && countO >= countX) {
101 throw new Error('Impossible board: game should have ended after the game was won');
102 }
103
104 // Edge Case: Board where game continued after a win
105 if (oWins && countX > countO) {
106 throw new Error('Impossible board: game should have ended after the game was won');
107 }
108
109 // Determine game state
110 if (xWins || oWins) {
111 return 'win';
112 }
113
114 // If board is full, it's a draw
115 if (countX + countO === 9) {
116 return 'draw';
117 }
118
119 // Otherwise, game is ongoing
120 return 'ongoing';
12121};
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.