goodagent

Finished
1//
2// This is only a SKELETON file for the 'State of Tic Tac Toe' exercise. It's been provided as a
3// convenience to get you started writing code faster.
4//
5
6/**
7 * @param {string[]} board
8 * @returns {string}
1/*
2 * Determines the state of a Tic-Tac-Toe game based on the given board.
3 *
4 * @param {string[]} board - An array of strings representing the game board.
5 * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state.
6 * @throws {Error} - Throws an error for invalid board states with specific messages.
97 */
108export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
9 // Validate board structure
10 if (!Array.isArray(board) || board.length !== 3) {
11 throw new Error('Invalid board: must have exactly 3 rows');
12 }
13
14 for (let i = 0; i < 3; i++) {
15 if (typeof board[i] !== 'string' || board[i].length !== 3) {
16 throw new Error('Invalid board: each row must be a string of exactly 3 characters');
17 }
18 }
19
20 // Count X's and O's
21 let xCount = 0;
22 let oCount = 0;
23
24 for (let i = 0; i < 3; i++) {
25 for (let j = 0; j < 3; j++) {
26 const cell = board[i][j];
27 if (cell === 'X') {
28 xCount++;
29 } else if (cell === 'O') {
30 oCount++;
31 } else if (cell !== ' ' && cell !== '.') {
32 throw new Error('Invalid board: contains invalid characters');
33 }
34 }
35 }
36
37 // Edge Case: Invalid turn order - O started
38 if (oCount > xCount) {
39 throw new Error('Wrong turn order: O started');
40 }
41
42 // Edge Case: Invalid turn order - X went twice in a row
43 if (xCount > oCount + 1) {
44 throw new Error('Wrong turn order: X went twice');
45 }
46
47 // Check for wins
48 const checkWin = (player) => {
49 // Check rows
50 for (let i = 0; i < 3; i++) {
51 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
52 return true;
53 }
54 }
55
56 // Check columns
57 for (let j = 0; j < 3; j++) {
58 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
59 return true;
60 }
61 }
62
63 // Check diagonals
64 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
65 return true;
66 }
67
68 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
69 return true;
70 }
71
72 return false;
73 };
74
75 const xWins = checkWin('X');
76 const oWins = checkWin('O');
77
78 // Edge Case: Both players win (impossible in a valid game)
79 if (xWins && oWins) {
80 throw new Error('Impossible board: game should have ended after the game was won');
81 }
82
83 // Edge Case: Game continued after win
84 if (xWins && oCount >= xCount) {
85 throw new Error('Impossible board: game should have ended after the game was won');
86 }
87
88 // Edge Case: Game continued after win
89 if (oWins && xCount > oCount) {
90 throw new Error('Impossible board: game should have ended after the game was won');
91 }
92
93 // Determine game state
94 if (xWins || oWins) {
95 return 'win';
96 }
97
98 // Edge Case: Draw - board is full with no winner
99 if (xCount + oCount === 9) {
100 return 'draw';
101 }
102
103 // Edge Case: Ongoing game - board is not full and no winner
104 return 'ongoing';
105
106 // Handled Edge Cases:
107 // - Invalid board structure
108 // - Invalid characters on board
109 // - O started the game
110 // - X went twice in a row
111 // - Both players win (impossible)
112 // - Game continued after X win
113 // - Game continued after O win
114 // - Draw (full board with no winner)
115 // - Ongoing game (incomplete board with no winner)
116};
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.