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 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 length 3');
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 // Check for wins
38 const checkWin = (player) => {
39 // Check rows
40 for (let i = 0; i < 3; i++) {
41 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
42 return true;
43 }
44 }
45
46 // Check columns
47 for (let j = 0; j < 3; j++) {
48 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
49 return true;
50 }
51 }
52
53 // Check diagonals
54 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
55 return true;
56 }
57
58 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
59 return true;
60 }
61
62 return false;
63 };
64
65 const xWins = checkWin('X');
66 const oWins = checkWin('O');
67
68 // Edge Case: Both players win (impossible in valid game)
69 if (xWins && oWins) {
70 throw new Error('Impossible board: game should have ended after the game was won');
71 }
72
73 // Edge Case: Game continued after win
74 if (xWins && oCount >= xCount) {
75 throw new Error('Impossible board: game should have ended after the game was won');
76 }
77
78 // Edge Case: Game continued after win
79 if (oWins && xCount > oCount) {
80 throw new Error('Impossible board: game should have ended after the game was won');
81 }
82
83 // Edge Case: Invalid turn order validation
84 // Apply basic turn order rules first
85 // X starts first, so O count can't exceed X count
86 // Players alternate, so X count can't exceed O count by more than 1
87 if (oCount > xCount) {
88 throw new Error('Wrong turn order: O started');
89 }
90 if (xCount > oCount + 1) {
91 throw new Error('Wrong turn order: X went twice');
92 }
93
94 // Additional validation for win scenarios
95 // If X wins, X should have played last (xCount > oCount)
96 // If O wins, both players should have played the same number of times (xCount == oCount)
97 if (xWins && xCount <= oCount) {
98 throw new Error('Wrong turn order: X went twice');
99 }
100 if (oWins && xCount > oCount) {
101 throw new Error('Wrong turn order: O started');
102 }
103
104
105 // Determine game state
106 if (xWins || oWins) {
107 return 'win';
108 }
109
110 // Edge Case: Draw - board is full with no winner
111 if (xCount + oCount === 9) {
112 return 'draw';
113 }
114
115 // Edge Case: Ongoing game - board is not full and no winner
116 return 'ongoing';
117
118 // Handled Edge Cases:
119 // 1. Invalid turn order - O started
120 // 2. Invalid turn order - X went twice in a row
121 // 3. Both players win (impossible in valid game)
122 // 4. Game continued after X win
123 // 5. Game continued after O win
124 // 6. Draw - board is full with no winner
125 // 7. Ongoing game - board is not full and no winner
126 // 8. Invalid board structure
127 // 9. Invalid characters on board
128};
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.