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 (!Array.isArray(board) || board.length !== 3) {
16 throw new Error('Invalid board: must have 3 rows');
17 }
18
19 for (const row of board) {
20 if (typeof row !== 'string' || row.length !== 3) {
21 throw new Error('Invalid board: each row must be a string of length 3');
22 }
23 }
24
25 // Count X's and O's
26 let xCount = 0;
27 let oCount = 0;
28
29 for (const row of board) {
30 for (const cell of row) {
31 if (cell === 'X') xCount++;
32 else if (cell === 'O') oCount++;
33 else if (cell !== ' ') {
34 throw new Error('Invalid board: contains invalid characters');
35 }
36 }
37 }
38
39 // Edge Case: Invalid turn order - O started (only when O has 1 and X has 0)
40 if (oCount === 1 && xCount === 0) {
41 throw new Error('Wrong turn order: O started');
42 }
43
44 // Edge Case: Invalid turn order - O went twice in a row
45 if (oCount > xCount && oCount > 1) {
46 throw new Error('Wrong turn order: O went twice');
47 }
48
49 // Edge Case: Invalid turn order - X went twice in a row
50 if (xCount > oCount + 1) {
51 throw new Error('Wrong turn order: X went twice');
52 }
53
54 // Check for wins
55 const winPatterns = [
56 // Rows
57 [0, 1, 2],
58 [3, 4, 5],
59 [6, 7, 8],
60 // Columns
61 [0, 3, 6],
62 [1, 4, 7],
63 [2, 5, 8],
64 // Diagonals
65 [0, 4, 8],
66 [2, 4, 6]
67 ];
68
69 // Convert board to flat array for easier checking
70 const flatBoard = board.join('').split('');
71
72 let xWins = false;
73 let oWins = false;
74
75 for (const pattern of winPatterns) {
76 const [a, b, c] = pattern;
77 if (flatBoard[a] === 'X' && flatBoard[b] === 'X' && flatBoard[c] === 'X') {
78 xWins = true;
79 }
80 if (flatBoard[a] === 'O' && flatBoard[b] === 'O' && flatBoard[c] === 'O') {
81 oWins = true;
82 }
83 }
84
85 // Edge Case: Both players win (impossible in a valid game)
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
91 // X wins but O made the same or more moves (X should have exactly one more)
92 if (xWins && xCount !== oCount + 1) {
93 throw new Error('Impossible board: game should have ended after the game was won');
94 }
95
96 // Edge Case: Game continued after win
97 // O wins but X made more moves (both should have the same number)
98 if (oWins && xCount !== oCount) {
99 throw new Error('Impossible board: game should have ended after the game was won');
100 }
101
102 // Determine game state
103 if (xWins || oWins) {
104 return 'win';
105 }
106
107 // Edge Case: Board is full (draw)
108 if (xCount + oCount === 9) {
109 return 'draw';
110 }
111
112 // Edge Case: Game is still ongoing
113 return 'ongoing';
114
115 // Handled Edge Cases:
116 // - Invalid turn order (O started)
117 // - Invalid turn order (X went twice)
118 // - Both players winning
119 // - Game continued after win (X won but O played)
120 // - Game continued after win (O won but X played)
121 // - Board is full (draw)
122 // - Game is still ongoing
123};
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.