chris

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 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 (!Array.isArray(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 (typeof board[i] !== 'string' || board[i].length !== 3) {
21 throw new Error('Invalid board: each row must be a string of exactly 3 characters');
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: Invalid turn order - O started the game
43 if (oCount > xCount) {
44 throw new Error('Wrong turn order: O started');
45 }
46
47 // Edge Case: Invalid turn order - X went twice in a row
48 if (xCount > oCount + 1) {
49 throw new Error('Wrong turn order: X went twice');
50 }
51
52 // Check for wins
53 const winPatterns = [
54 // Rows
55 [[0,0], [0,1], [0,2]],
56 [[1,0], [1,1], [1,2]],
57 [[2,0], [2,1], [2,2]],
58 // Columns
59 [[0,0], [1,0], [2,0]],
60 [[0,1], [1,1], [2,1]],
61 [[0,2], [1,2], [2,2]],
62 // Diagonals
63 [[0,0], [1,1], [2,2]],
64 [[0,2], [1,1], [2,0]]
65 ];
66
67 let xWins = false;
68 let oWins = false;
69
70 for (const pattern of winPatterns) {
71 const [a, b, c] = pattern;
72 const valA = board[a[0]][a[1]];
73 const valB = board[b[0]][b[1]];
74 const valC = board[c[0]][c[1]];
75
76 if (valA !== ' ' && valA === valB && valB === valC) {
77 if (valA === 'X') {
78 xWins = true;
79 } else if (valA === 'O') {
80 oWins = true;
81 }
82 }
83 }
84
85 // Edge Case: Game continued after win
86 if (xWins && oWins) {
87 throw new Error('Impossible board: game should have ended after the game was won');
88 }
89
90 // Edge Case: Game continued after win (X won but O kept playing)
91 if (xWins && oCount >= xCount) {
92 throw new Error('Impossible board: game should have ended after the game was won');
93 }
94
95 // Edge Case: Game continued after win (O won but X kept playing)
96 if (oWins && xCount > oCount) {
97 throw new Error('Impossible board: game should have ended after the game was won');
98 }
99
100 // Determine game state
101 if (xWins || oWins) {
102 return 'win';
103 }
104
105 // Edge Case: Game ended in a draw (board is full)
106 if (xCount + oCount === 9) {
107 return 'draw';
108 }
109
110 // Edge Case: Game is ongoing
111 return 'ongoing';
112
113 // Handled Edge Cases:
114 // - Invalid turn order (O started)
115 // - Invalid turn order (X went twice)
116 // - Game continued after win (both players won)
117 // - Game continued after win (X won but O kept playing)
118 // - Game continued after win (O won but X kept playing)
119 // - Game ended in a draw
120 // - Game is ongoing
121 // - Invalid board structure
122 // - Invalid characters on board
123};
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.