xas

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 dimensions
10 if (board.length !== 3) {
11 throw new Error('Invalid board: must have 3 rows');
12 }
13
14 for (let row of board) {
15 if (row.length !== 3) {
16 throw new Error('Invalid board: each row must have 3 columns');
17 }
18 }
19
20 // Count X's and O's
21 let xCount = 0;
22 let oCount = 0;
23
24 for (let row of board) {
25 for (let cell of row) {
26 if (cell === 'X') xCount++;
27 else if (cell === 'O') oCount++;
28 else if (cell !== ' ') {
29 // Edge Case: Invalid characters on the board
30 throw new Error('Invalid board: contains invalid characters');
31 }
32 }
33 }
34
35 // Edge Case: Wrong turn order - O started
36 if (oCount > xCount && xCount === 0) {
37 throw new Error('Wrong turn order: O started');
38 }
39
40 // Edge Case: Wrong turn order - O went twice in a row
41 if (oCount > xCount && xCount > 0) {
42 throw new Error('Wrong turn order: O went twice');
43 }
44
45 // Edge Case: Wrong turn order - X went twice in a row
46 if (xCount > oCount + 1) {
47 throw new Error('Wrong turn order: X went twice');
48 }
49
50 // Edge Case: Wrong turn order - O went twice in a row
51 if (xCount > oCount && oCount > 0 && xCount === oCount) {
52 // This case shouldn't happen with the above logic, but keeping for clarity
53 }
54
55 // Check for wins
56 const winPatterns = [
57 // Rows
58 [0, 1, 2],
59 [3, 4, 5],
60 [6, 7, 8],
61 // Columns
62 [0, 3, 6],
63 [1, 4, 7],
64 [2, 5, 8],
65 // Diagonals
66 [0, 4, 8],
67 [2, 4, 6]
68 ];
69
70 // Convert board to a flat array for easier checking
71 const flatBoard = board.join('').split('');
72
73 let xWins = false;
74 let oWins = false;
75
76 for (const pattern of winPatterns) {
77 const [a, b, c] = pattern;
78 if (flatBoard[a] === 'X' && flatBoard[b] === 'X' && flatBoard[c] === 'X') {
79 xWins = true;
80 }
81 if (flatBoard[a] === 'O' && flatBoard[b] === 'O' && flatBoard[c] === 'O') {
82 oWins = true;
83 }
84 }
85
86 // Edge Case: Both players win (impossible in a real game)
87 if (xWins && oWins) {
88 throw new Error('Impossible board: game should have ended after the game was won');
89 }
90
91 // Edge Case: Game continued after a win
92 if (xWins && oCount >= xCount) {
93 throw new Error('Impossible board: game should have ended after the game was won');
94 }
95
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: Board is full with no winner (draw)
106 if (xCount + oCount === 9) {
107 return 'draw';
108 }
109
110 // Edge Case: Game is not finished yet (ongoing)
111 return 'ongoing';
112
113 // Handled Edge Cases:
114 // 1. Invalid board dimensions
115 // 2. Invalid characters on the board
116 // 3. Wrong turn order - O started
117 // 4. Wrong turn order - X went twice in a row
118 // 5. Both players win (impossible in a real game)
119 // 6. Game continued after a win
120 // 7. Board is full with no winner (draw)
121 // 8. Game is not finished yet (ongoing)
122};
Test NameStatus
Invalid board: O started
Fail
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: 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.