rockstar

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', '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');
12};
15 // Validate board structure
16 if (!Array.isArray(board) || board.length !== 3) {
17 throw new Error('Invalid board: must have 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 length 3');
23 }
24 }
25
26 // Count Xs and Os
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') {
34 countX++;
35 } else if (cell === 'O') {
36 countO++;
37 } else if (cell !== ' ') {
38 throw new Error('Invalid board: contains invalid characters');
39 }
40 }
41 }
42
43 // Check for wins first, as some winning boards might have unusual piece counts
44 const wins = (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 = wins('X');
72 const oWins = wins('O');
73
74 // Check for game continuation after win
75 if (xWins && oWins) {
76 // Both players win (impossible in a valid game)
77 throw new Error('Impossible board: game should have ended after the game was won');
78 } else if (xWins) {
79 // If X wins, O should have made exactly one fewer move (X started first)
80 // This checks if the game continued after X won
81 if (countO > countX - 1) {
82 throw new Error('Impossible board: game should have ended after the game was won');
83 }
84 } else if (oWins) {
85 // If O wins, both players should have made the same number of moves
86 // This checks if the game continued after O won
87 if (countO > countX) {
88 throw new Error('Impossible board: game should have ended after the game was won');
89 }
90 } else {
91 // No one has won yet, validate normal turn order
92 // Edge Case: Invalid turn order - O started (first move is O)
93 if (countO > 0 && countX === 0) {
94 throw new Error('Wrong turn order: O started');
95 }
96
97 // Edge Case: Invalid turn order - O went twice in a row
98 if (countO > countX) {
99 throw new Error('Wrong turn order: O went twice');
100 }
101
102 // Edge Case: Invalid turn order - X went twice in a row
103 if (countX > countO + 1) {
104 throw new Error('Wrong turn order: X went twice');
105 }
106 }
107
108
109
110 // Determine game state
111 if (xWins || oWins) {
112 return 'win';
113 }
114
115 // Edge Case: Draw - board is full with no winner
116 if (countX + countO === 9) {
117 return 'draw';
118 }
119
120 // Edge Case: Ongoing game - board is not full and no winner
121 return 'ongoing';
122
123 // Handled Edge Cases:
124 // 1. Invalid turn order - O started
125 // 2. Invalid turn order - X went twice in a row
126 // 3. Both players win (impossible in a valid game)
127 // 4. Game continued after X win
128 // 5. Game continued after O win
129 // 6. Draw - board is full with no winner
130 // 7. Ongoing game - board is not full and no winner
131 // 8. Invalid board structure
132 // 9. Invalid characters on board
133};
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.