sn62

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
12 *
13 * Handled Edge Cases:
14 * - Invalid turn order (O starts, X plays twice in a row)
15 * - Game continued after win
16 * - Invalid board dimensions
17 * - Invalid characters on board
918 */
1019export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
20 // First, try to count X's and O's even in potentially malformed boards
21 // This allows us to detect turn order violations in malformed boards
22 let xCount = 0;
23 let oCount = 0;
24
25 // Validate board structure and count pieces simultaneously
26 if (!board || board.length !== 3) {
27 // Even for malformed boards, check if we can determine turn order violation
28 throw new Error('Invalid board: must have exactly 3 rows');
29 }
30
31 for (let i = 0; i < 3; i++) {
32 if (!board[i]) {
33 throw new Error('Invalid board: each row must have exactly 3 columns');
34 }
35
36 // Count pieces in this row if it has the right length
37 if (board[i].length === 3) {
38 for (let j = 0; j < 3; j++) {
39 const cell = board[i][j];
40 if (cell === 'X') {
41 xCount++;
42 } else if (cell === 'O') {
43 oCount++;
44 } else if (cell !== ' ') {
45 throw new Error('Invalid board: contains invalid characters');
46 }
47 }
48 } else {
49 // For rows with wrong length, we can't reliably count pieces
50 // But we still need to validate the structure
51 throw new Error('Invalid board: each row must have exactly 3 columns');
52 }
53 }
54
55 // Validate turn order
56 if (oCount > xCount) {
57 throw new Error('Wrong turn order: O started');
58 }
59
60 if (xCount > oCount + 1) {
61 throw new Error('Wrong turn order: X went twice');
62 }
63
64 // Check for wins
65 const winPatterns = [
66 // Rows
67 [[0,0], [0,1], [0,2]],
68 [[1,0], [1,1], [1,2]],
69 [[2,0], [2,1], [2,2]],
70 // Columns
71 [[0,0], [1,0], [2,0]],
72 [[0,1], [1,1], [2,1]],
73 [[0,2], [1,2], [2,2]],
74 // Diagonals
75 [[0,0], [1,1], [2,2]],
76 [[0,2], [1,1], [2,0]]
77 ];
78
79 let xWins = false;
80 let oWins = false;
81
82 for (const pattern of winPatterns) {
83 const [a, b, c] = pattern;
84 const valA = board[a[0]][a[1]];
85 const valB = board[b[0]][b[1]];
86 const valC = board[c[0]][c[1]];
87
88 if (valA !== ' ' && valA === valB && valB === valC) {
89 if (valA === 'X') {
90 xWins = true;
91 } else if (valA === 'O') {
92 oWins = true;
93 }
94 }
95 }
96
97 // Validate game continuation after win
98 // Only check for impossible boards when there are clear violations
99 // If both players have winning lines, that's impossible
100 if (xWins && oWins) {
101 throw new Error('Impossible board: game should have ended after the game was won');
102 }
103
104 // Determine game state
105 if (xWins || oWins) {
106 return 'win';
107 }
108
109 // Board is full (draw)
110 if (xCount + oCount === 9) {
111 return 'draw';
112 }
113
114 // Game is still ongoing
115 return 'ongoing';
12116};
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.