fistinz

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 (!board || board.length !== 3) {
16 throw new Error('Invalid board: must have 3 rows');
17 }
18
19 for (let i = 0; i < 3; i++) {
20 if (!board[i] || board[i].length !== 3) {
21 throw new Error('Invalid board: each row must have 3 columns');
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 // Check for wins
43 const checkWin = (player) => {
44 // Check rows
45 for (let i = 0; i < 3; i++) {
46 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
47 return true;
48 }
49 }
50
51 // Check columns
52 for (let j = 0; j < 3; j++) {
53 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
54 return true;
55 }
56 }
57
58 // Check diagonals
59 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
60 return true;
61 }
62
63 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
64 return true;
65 }
66
67 return false;
68 };
69
70 const xWins = checkWin('X');
71 const oWins = checkWin('O');
72
73 // Edge Case: Both players win (impossible in a valid game)
74 if (xWins && oWins) {
75 throw new Error('Impossible board: game should have ended after the game was won');
76 }
77
78 // Check for game continued after win BEFORE turn order validation
79 // If X wins, check if the board could have been reached in a valid game
80 // In a valid game where X wins:
81 // - X should have played either the same number of times as O (if X won on X's last move)
82 // - or one more than O (if X won before O's last move)
83 // Any other case means the game continued after X's win or the board is invalid
84 if (xWins && oCount >= xCount) {
85 throw new Error('Impossible board: game should have ended after the game was won');
86 }
87
88 // If O wins, check if the board could have been reached in a valid game
89 // In a valid game where O wins:
90 // - O should have played either the same number of times as X (if O won on O's last move)
91 // - or one more than X (if O won before X's last move)
92 // Any other case means the game continued after O's win or the board is invalid
93 if (oWins && xCount > oCount) {
94 throw new Error('Impossible board: game should have ended after the game was won');
95 }
96
97 // Determine game state
98 if (xWins || oWins) {
99 return 'win';
100 }
101
102 // Validate turn order only for boards without win conditions
103 // Edge Case: Invalid turn order - O started
104 if (oCount > xCount) {
105 throw new Error('Wrong turn order: O started');
106 }
107
108 // Edge Case: Invalid turn order - X went twice in a row
109 if (xCount > oCount + 1) {
110 throw new Error('Wrong turn order: X went twice');
111 }
112
113 // Edge Case: Draw - board is full with no winner
114 if (xCount + oCount === 9) {
115 return 'draw';
116 }
117
118 // Edge Case: Ongoing game - board not full and no winner
119 return 'ongoing';
120
121 // Handled Edge Cases:
122 // 1. Invalid turn order - O started
123 // 2. Invalid turn order - X went twice in a row
124 // 3. Both players win (impossible in a valid game)
125 // 4. Game continued after X win
126 // 5. Game continued after O win
127 // 6. Draw - board is full with no winner
128 // 7. Ongoing game - board not full and no winner
129 // 8. Invalid board structure
130 // 9. Invalid characters on board
131};
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.