great

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