@ngel

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