awesome

Finished
88 * @returns {string}
99 */
1010export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
11 // Validate board dimensions
12 if (board.length !== 3) {
13 throw new Error('Invalid board: must have 3 rows');
14 }
15
16 for (let i = 0; i < 3; i++) {
17 if (board[i].length !== 3) {
18 throw new Error('Invalid board: each row must have 3 columns');
19 }
20 }
21
22 // Count X's and O's
23 let xCount = 0;
24 let oCount = 0;
25
26 for (let i = 0; i < 3; i++) {
27 for (let j = 0; j < 3; j++) {
28 const cell = board[i][j];
29 if (cell === 'X') {
30 xCount++;
31 } else if (cell === 'O') {
32 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 the game
40 if (oCount > xCount) {
41 throw new Error('Wrong turn order: O started');
42 }
43
44 // Edge Case: Invalid turn order - X went twice in a row (more than one turn difference)
45 if (xCount > oCount + 1) {
46 throw new Error('Wrong turn order: X went twice');
47 }
48
49 // Check for wins
50 const checkWin = (player) => {
51 // Check rows
52 for (let i = 0; i < 3; i++) {
53 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
54 return true;
55 }
56 }
57
58 // Check columns
59 for (let j = 0; j < 3; j++) {
60 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
61 return true;
62 }
63 }
64
65 // Check diagonals
66 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
67 return true;
68 }
69 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
70 return true;
71 }
72
73 return false;
74 };
75
76 const xWins = checkWin('X');
77 const oWins = checkWin('O');
78
79 // Edge Case: Both players win (impossible in a valid game)
80 if (xWins && oWins) {
81 throw new Error('Impossible board: game should have ended after the game was won');
82 }
83
84 // Edge Case: Game continued after win
85 if (xWins && oCount >= xCount) {
86 throw new Error('Impossible board: game should have ended after the game was won');
87 }
88
89 // Edge Case: Game continued after win
90 if (oWins && xCount > oCount) {
91 throw new Error('Impossible board: game should have ended after the game was won');
92 }
93
94 // Determine game state
95 if (xWins) {
96 return 'win';
97 }
98
99 if (oWins) {
100 return 'win';
101 }
102
103 // Edge Case: Board is full (draw)
104 if (xCount + oCount === 9) {
105 return 'draw';
106 }
107
108 // Edge Case: Game is ongoing
109 return 'ongoing';
110
111 // Handled Edge Cases:
112 // 1. Invalid turn order - O started the game
113 // 2. Invalid turn order - X went twice in a row
114 // 3. Both players win (impossible in a valid game)
115 // 4. Game continued after X win
116 // 5. Game continued after O win
117 // 6. Board is full (draw)
118 // 7. Game is ongoing
119};
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.