let it go

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