sn62

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
12 *
13 * Handled Edge Cases:
14 * - Invalid turn order (O starts, X plays twice)
15 * - Game continued after win
16 * - Invalid board dimensions
17 * - Invalid characters on board
918 */
1019export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
20 // Validate board structure
21 if (!board || board.length !== 3) {
22 throw new Error('Invalid board: must have 3 rows');
23 }
24
25 for (let i = 0; i < 3; i++) {
26 if (!board[i] || board[i].length !== 3) {
27 throw new Error('Invalid board: each row must have 3 columns');
28 }
29 }
30
31 // Count X's and O's
32 let countX = 0;
33 let countO = 0;
34
35 for (let i = 0; i < 3; i++) {
36 for (let j = 0; j < 3; j++) {
37 const cell = board[i][j];
38 if (cell === 'X') {
39 countX++;
40 } else if (cell === 'O') {
41 countO++;
42 } else if (cell !== ' ') {
43 throw new Error('Invalid board: contains invalid characters');
44 }
45 }
46 }
47
48 // Edge Case: Invalid turn order - O started
49 if (countO > countX) {
50 throw new Error('Wrong turn order: O started');
51 }
52
53 // Edge Case: Invalid turn order - X went twice
54 if (countX > countO + 1) {
55 throw new Error('Wrong turn order: X went twice');
56 }
57
58 // Check for wins
59 const wins = (player) => {
60 // Check rows
61 for (let i = 0; i < 3; i++) {
62 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
63 return true;
64 }
65 }
66
67 // Check columns
68 for (let j = 0; j < 3; j++) {
69 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
70 return true;
71 }
72 }
73
74 // Check diagonals
75 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
76 return true;
77 }
78
79 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
80 return true;
81 }
82
83 return false;
84 };
85
86 const xWins = wins('X');
87 const oWins = wins('O');
88
89 // Edge Case: Both players win (impossible in a valid game)
90 if (xWins && oWins) {
91 throw new Error('Impossible board: game should have ended after the game was won');
92 }
93
94 // Edge Case: Game continued after win
95 if (xWins && countO >= countX) {
96 throw new Error('Impossible board: game should have ended after the game was won');
97 }
98
99 // Edge Case: Game continued after win
100 if (oWins && countX > countO) {
101 throw new Error('Impossible board: game should have ended after the game was won');
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 // Game is ongoing
115 return 'ongoing';
12116};
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.