seahawk

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 ' ' (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 // 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 X won, there should be exactly one more X than O (X made the winning move)
64 if (winForX && countX !== countO + 1) {
65 throw new Error('Impossible board: game should have ended after the game was won');
66 }
67
68 // Edge Case: Game continued after a win
69 // If O won, there should be equal number of X and O (O made the winning move)
70 if (winForO && countX !== countO) {
71 throw new Error('Impossible board: game should have ended after the game was won');
72 }
73
74 // Determine game state
75 if (winForX || winForO) {
76 return 'win';
77 }
78
79 // Edge Case: Board is full (draw)
80 if (countX + countO === 9) {
81 return 'draw';
82 }
83
84 // Edge Case: Game is not finished yet
85 return 'ongoing';
1286};
87
88/**
89 * Checks if the given player has won on the board.
90 *
91 * @param {string[]} board - The game board.
92 * @param {string} player - The player to check for win ('X' or 'O').
93 * @returns {boolean} - True if the player has won, false otherwise.
94 */
95function checkWin(board, player) {
96 // Check rows
97 for (let i = 0; i < 3; i++) {
98 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
99 return true;
100 }
101 }
102
103 // Check columns
104 for (let j = 0; j < 3; j++) {
105 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
106 return true;
107 }
108 }
109
110 // Check diagonals
111 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
112 return true;
113 }
114
115 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
116 return true;
117 }
118
119 return false;
120}
121
122// Handled Edge Cases:
123// 1. Invalid board structure (not 3 rows or rows not 3 chars)
124// 2. Invalid characters on board
125// 3. Wrong turn order - O started
126// 4. Wrong turn order - X went twice
127// 5. Both players won (impossible)
128// 6. Game continued after X won
129// 7. Game continued after O won
130// 8. Board is full (draw)
131// 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.