Alone

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: 'X', 'O', or ' ' (empty).
11 * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state.
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 be an array of 3 strings');
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 X's and O's
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 checkWin = (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 = checkWin('X');
72 const oWins = checkWin('O');
73
74 // Edge Case: Both players win (impossible in a valid game)
75 if (xWins && oWins) {
76 throw new Error('Impossible board: game should have ended after the game was won');
77 }
78
79 // Edge Case: Game continued after a win
80 if (xWins && countO >= countX) {
81 throw new Error('Impossible board: game should have ended after the game was won');
82 }
83
84 if (oWins && countX > countO) {
85 throw new Error('Impossible board: game should have ended after the game was won');
86 }
87
88 // Check turn order violations
89 // Edge Case: Invalid turn order - O started the game or O went twice
90 if (countO > countX) {
91 // If O has more than X, either O started (X=0, O>=1) or O went twice (X>=1, O>X)
92 if (countX === 0) {
93 throw new Error('Wrong turn order: O started');
94 } else {
95 throw new Error('Wrong turn order: O went twice');
96 }
97 }
98
99 // Edge Case: Invalid turn order - X went twice in a row
100 if (countX > countO + 1) {
101 throw new Error('Wrong turn order: X went twice');
102 }
103
104 // Determine game state
105 if (xWins || oWins) {
106 return 'win';
107 }
108
109 // Edge Case: Board is full (draw)
110 if (countX + countO === 9) {
111 return 'draw';
112 }
113
114 // Edge Case: Game is not finished yet (ongoing)
115 return 'ongoing';
116
117 // Handled Edge Cases:
118 // - Invalid board structure
119 // - Invalid characters on board
120 // - O started the game
121 // - Player went twice in a row
122 // - Both players win
123 // - Game continued after a win
124 // - Board is full (draw)
125 // - Game is not finished yet (ongoing)
12126};
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.