bohdan

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');
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 // Edge Case: Invalid turn order - O started
44 if (countO > countX) {
45 throw new Error('Wrong turn order: O started');
46 }
47
48 // Edge Case: Invalid turn order - X went twice in a row
49 if (countX > countO + 1) {
50 throw new Error('Wrong turn order: X went twice');
51 }
52
53 // Check for wins
54 const winForX = checkWin(board, 'X');
55 const winForO = checkWin(board, 'O');
56
57 // Edge Case: Both players won (impossible in a valid game)
58 if (winForX && winForO) {
59 throw new Error('Impossible board: game should have ended after the game was won');
60 }
61
62 // Edge Case: Game continued after a win
63 if (winForX && countO >= countX) {
64 throw new Error('Impossible board: game should have ended after the game was won');
65 }
66
67 // Edge Case: Game continued after a win
68 if (winForO && countX > countO) {
69 throw new Error('Impossible board: game should have ended after the game was won');
70 }
71
72 // Determine game state
73 if (winForX || winForO) {
74 return 'win';
75 }
76
77 // Edge Case: Board is full (draw)
78 if (countX + countO === 9) {
79 return 'draw';
80 }
81
82 // Edge Case: Game is not finished yet
83 return 'ongoing';
1284};
85
86/**
87 * Checks if the given player has won on the board.
88 *
89 * @param {string[]} board - The game board.
90 * @param {string} player - The player to check for win ('X' or 'O').
91 * @returns {boolean} - True if the player has won, false otherwise.
92 */
93const checkWin = (board, player) => {
94 // Check rows
95 for (let i = 0; i < 3; i++) {
96 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
97 return true;
98 }
99 }
100
101 // Check columns
102 for (let j = 0; j < 3; j++) {
103 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
104 return true;
105 }
106 }
107
108 // Check diagonals
109 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
110 return true;
111 }
112
113 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
114 return true;
115 }
116
117 return false;
118};
119
120// Handled Edge Cases:
121// 1. Invalid board structure (not 3 rows or rows not 3 chars)
122// 2. Invalid characters on board
123// 3. Wrong turn order - O started
124// 4. Wrong turn order - X went twice
125// 5. Both players won (impossible)
126// 6. Game continued after X won
127// 7. Game continued after O won
128// 8. Board is full (draw)
129// 9. Game is not finished yet (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.