JiaYou

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' if a player has won, 'draw' if the game is a draw,
12 * or 'ongoing' if the game is still in progress.
13 * @throws {Error} - Throws an error with a specific message if the board is invalid.
914 */
1015export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
16 // Validate board structure
17 if (!Array.isArray(board) || board.length !== 3) {
18 throw new Error('Invalid board: must be an array of 3 strings');
19 }
20
21 for (let i = 0; i < 3; i++) {
22 if (typeof board[i] !== 'string' || board[i].length !== 3) {
23 throw new Error('Invalid board: each row must be a string of length 3');
24 }
25 for (let j = 0; j < 3; j++) {
26 const char = board[i][j];
27 if (char !== 'X' && char !== 'O' && char !== ' ') {
28 throw new Error('Invalid board: contains invalid characters');
29 }
30 }
31 }
32
33 // Count Xs and Os
34 let countX = 0;
35 let countO = 0;
36
37 for (let i = 0; i < 3; i++) {
38 for (let j = 0; j < 3; j++) {
39 if (board[i][j] === 'X') countX++;
40 if (board[i][j] === 'O') countO++;
41 }
42 }
43
44 // Edge Case: Invalid turn order - O started the game
45 if (countO > countX) {
46 throw new Error('Wrong turn order: O started');
47 }
48
49 // Edge Case: Invalid turn order - X went twice in a row (more than one move difference)
50 if (countX > countO + 1) {
51 throw new Error('Wrong turn order: X went twice');
52 }
53
54 // Check for wins
55 const winPatterns = [
56 // Rows
57 [[0,0], [0,1], [0,2]],
58 [[1,0], [1,1], [1,2]],
59 [[2,0], [2,1], [2,2]],
60 // Columns
61 [[0,0], [1,0], [2,0]],
62 [[0,1], [1,1], [2,1]],
63 [[0,2], [1,2], [2,2]],
64 // Diagonals
65 [[0,0], [1,1], [2,2]],
66 [[0,2], [1,1], [2,0]]
67 ];
68
69 let xWins = false;
70 let oWins = false;
71
72 for (const pattern of winPatterns) {
73 const [a, b, c] = pattern;
74 const valA = board[a[0]][a[1]];
75 const valB = board[b[0]][b[1]];
76 const valC = board[c[0]][c[1]];
77
78 if (valA !== ' ' && valA === valB && valB === valC) {
79 if (valA === 'X') xWins = true;
80 if (valA === 'O') oWins = true;
81 }
82 }
83
84 // Edge Case: Impossible board - game continued after win
85 if (xWins && oWins) {
86 throw new Error('Impossible board: game should have ended after the game was won');
87 }
88
89 // Edge Case: Impossible board - game continued after win (X won but O made another move)
90 if (xWins && countO === countX) {
91 throw new Error('Impossible board: game should have ended after the game was won');
92 }
93
94 // Edge Case: Impossible board - game continued after win (O won but X made another move)
95 if (oWins && countX > countO) {
96 throw new Error('Impossible board: game should have ended after the game was won');
97 }
98
99 // Determine game state
100 if (xWins || oWins) {
101 return 'win';
102 }
103
104 // Edge Case: Game is a draw (board is full with no winner)
105 if (countX + countO === 9) {
106 return 'draw';
107 }
108
109 // Edge Case: Game is ongoing (not won and board is not full)
110 return 'ongoing';
111
112 // Handled Edge Cases:
113 // - Invalid turn order: O started the game
114 // - Invalid turn order: X went twice in a row
115 // - Impossible board: game continued after win (both players won)
116 // - Impossible board: game continued after win (X won but O made another move)
117 // - Impossible board: game continued after win (O won but X made another move)
118 // - Game is a draw (board is full with no winner)
119 // - Game is ongoing (not won and board is not full)
120};
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.