armor

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 3x3 board.
10 * Each character is either 'X', 'O', or ' ' (empty space).
11 * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state.
12 * @throws {Error} - Throws an error for invalid board states with specific messages.
913 */
1014export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
15 // Validate board structure
16 if (!Array.isArray(board) || board.length !== 3) {
17 throw new Error('Invalid board: must have 3 rows');
18 }
19
20 for (let i = 0; i < 3; i++) {
21 if (typeof board[i] !== 'string' || board[i].length !== 3) {
22 throw new Error('Invalid board: each row must be a string of length 3');
23 }
24 for (let j = 0; j < 3; j++) {
25 const char = board[i][j];
26 if (char !== 'X' && char !== 'O' && char !== ' ') {
27 throw new Error('Invalid board: contains invalid characters');
28 }
29 }
30 }
31
32 // Count X's and O's
33 let xCount = 0;
34 let oCount = 0;
35
36 for (let i = 0; i < 3; i++) {
37 for (let j = 0; j < 3; j++) {
38 if (board[i][j] === 'X') xCount++;
39 if (board[i][j] === 'O') oCount++;
40 }
41 }
42
43 // Edge Case: Invalid turn order - O started the game
44 if (oCount > xCount) {
45 throw new Error('Wrong turn order: O started');
46 }
47
48 // Edge Case: Invalid turn order - X went twice in a row (more than one move difference)
49 if (xCount > oCount + 1) {
50 throw new Error('Wrong turn order: X went twice');
51 }
52
53 // Check for wins
54 const winPatterns = [
55 // Rows
56 [[0,0], [0,1], [0,2]],
57 [[1,0], [1,1], [1,2]],
58 [[2,0], [2,1], [2,2]],
59 // Columns
60 [[0,0], [1,0], [2,0]],
61 [[0,1], [1,1], [2,1]],
62 [[0,2], [1,2], [2,2]],
63 // Diagonals
64 [[0,0], [1,1], [2,2]],
65 [[0,2], [1,1], [2,0]]
66 ];
67
68 let xWins = false;
69 let oWins = false;
70
71 for (const pattern of winPatterns) {
72 const [a, b, c] = pattern;
73 const valA = board[a[0]][a[1]];
74 const valB = board[b[0]][b[1]];
75 const valC = board[c[0]][c[1]];
76
77 if (valA !== ' ' && valA === valB && valB === valC) {
78 if (valA === 'X') {
79 xWins = true;
80 } else if (valA === 'O') {
81 oWins = true;
82 }
83 }
84 }
85
86 // Edge Case: Game continued after win
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 win (X won but O made another move)
92 if (xWins && oCount === xCount) {
93 throw new Error('Impossible board: game should have ended after the game was won');
94 }
95
96 // Edge Case: Game continued after win (O won but X made another move)
97 if (oWins && xCount > oCount) {
98 throw new Error('Impossible board: game should have ended after the game was won');
99 }
100
101 // Determine game state
102 if (xWins || oWins) {
103 return 'win';
104 }
105
106 // If board is full, it's a draw
107 if (xCount + oCount === 9) {
108 return 'draw';
109 }
110
111 // Otherwise, game is ongoing
112 return 'ongoing';
113
114 // Handled Edge Cases:
115 // 1. Invalid turn order - O started the game
116 // 2. Invalid turn order - X went twice in a row
117 // 3. Game continued after win (both players won)
118 // 4. Game continued after win (X won but O made another move)
119 // 5. Game continued after win (O won but X made another move)
120 // 6. Invalid board structure (not 3x3, invalid characters)
121};
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.