unicode

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 '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 have exactly 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 exactly 3 characters');
23 }
24 }
25
26 // Count X's and O's
27 let countX = 0;
28 let countO = 0;
29
30 for (let i = 0; i < 3; i++) {
31 for (let j = 0; j < 3; j++) {
32 const cell = board[i][j];
33 if (cell === 'X') countX++;
34 else if (cell === 'O') countO++;
35 else if (cell !== ' ') {
36 throw new Error('Invalid board: contains invalid characters');
37 }
38 }
39 }
40
41 // Edge Case: Invalid turn order - O started or played more than X
42 if (countO > countX) {
43 throw new Error('Wrong turn order: O started');
44 }
45
46 // Edge Case: Invalid turn order - X went twice in a row
47 if (countX > countO + 1) {
48 throw new Error('Wrong turn order: X went twice');
49 }
50
51 // Check for wins
52 const wins = (player) => {
53 // Check rows
54 for (let i = 0; i < 3; i++) {
55 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
56 return true;
57 }
58 }
59
60 // Check columns
61 for (let j = 0; j < 3; j++) {
62 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
63 return true;
64 }
65 }
66
67 // Check diagonals
68 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
69 return true;
70 }
71
72 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
73 return true;
74 }
75
76 return false;
77 };
78
79 const xWins = wins('X');
80 const oWins = wins('O');
81
82 // Edge Case: Both players win (impossible in a valid game)
83 if (xWins && oWins) {
84 throw new Error('Impossible board: game should have ended after the game was won');
85 }
86
87 // Edge Case: X wins but O made another move
88 if (xWins && countX === countO) {
89 throw new Error('Impossible board: game should have ended after the game was won');
90 }
91
92 // Edge Case: O wins but X made another move
93 if (oWins && countX > countO) {
94 throw new Error('Impossible board: game should have ended after the game was won');
95 }
96
97 // Determine game state
98 if (xWins || oWins) {
99 return 'win';
100 }
101
102 // Edge Case: Board is full (draw)
103 if (countX + countO === 9) {
104 return 'draw';
105 }
106
107 // Game is ongoing
108 return 'ongoing';
12109};
110
111// Handled Edge Cases:
112// 1. Invalid board structure (not 3x3)
113// 2. Invalid characters on board
114// 3. Wrong turn order - O started
115// 4. Wrong turn order - X went twice
116// 5. Both players win (impossible)
117// 6. Game continued after X won
118// 7. Game continued after O won
119// 8. Board is full (draw)
120// 9. Game is ongoing
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.