weekends

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 * @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 (!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 X's and O's
26 let xCount = 0;
27 let oCount = 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 xCount++;
34 } else if (cell === 'O') {
35 oCount++;
36 } else if (cell !== ' ') {
37 throw new Error('Invalid board: contains invalid characters');
38 }
39 }
40 }
41
42 // Edge Case: Wrong turn order validation
43 // X always starts, so X count should be equal to O count or exactly one more
44 if (oCount > xCount) {
45 // This could be either O started or O went twice
46 // If X hasn't made any moves (xCount === 0) and O has made moves, then O started
47 // If X has made moves (xCount > 0) and O has more moves, then O went twice
48 if (xCount === 0) {
49 throw new Error('Wrong turn order: O started');
50 } else {
51 throw new Error('Wrong turn order: O went twice');
52 }
53 }
54
55 if (xCount > oCount + 1) {
56 throw new Error('Wrong turn order: X went twice');
57 }
58
59 // Check for wins
60 const checkWin = (player) => {
61 // Check rows
62 for (let i = 0; i < 3; i++) {
63 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
64 return true;
65 }
66 }
67
68 // Check columns
69 for (let j = 0; j < 3; j++) {
70 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
71 return true;
72 }
73 }
74
75 // Check diagonals
76 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
77 return true;
78 }
79
80 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
81 return true;
82 }
83
84 return false;
85 };
86
87 const xWins = checkWin('X');
88 const oWins = checkWin('O');
89
90 // Edge Case: Both players win (impossible in a valid game)
91 if (xWins && oWins) {
92 throw new Error('Impossible board: game should have ended after the game was won');
93 }
94
95 // Edge Case: Game continued after win
96 // If X wins, O should have exactly one less move than X
97 // If O has played after X won, it's invalid
98 if (xWins && oCount !== xCount - 1) {
99 throw new Error('Impossible board: game should have ended after the game was won');
100 }
101
102 // Edge Case: Game continued after win
103 // If O wins, X should have exactly the same number of moves as O
104 // If X has played after O won, it's invalid
105 if (oWins && xCount !== oCount) {
106 throw new Error('Impossible board: game should have ended after the game was won');
107 }
108
109 // Determine game state
110 if (xWins || oWins) {
111 return 'win';
112 }
113
114 // Edge Case: Draw - board is full with no winner
115 if (xCount + oCount === 9) {
116 return 'draw';
117 }
118
119 // Edge Case: Ongoing game - board not full and no winner
120 return 'ongoing';
121
122 // Handled Edge Cases:
123 // 1. Wrong turn order - O started
124 // 2. Wrong turn order - X went twice in a row
125 // 3. Both players win (impossible in a valid game)
126 // 4. Game continued after X win
127 // 5. Game continued after O win
128 // 6. Draw - board is full with no winner
129 // 7. Ongoing game - board not full and no winner
130 // 8. Invalid board structure
131 // 9. Invalid characters on board
132};
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.