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 // Check for wins
40 const checkWin = (player) => {
41 // Check rows
42 for (let i = 0; i < 3; i++) {
43 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
44 return true;
45 }
46 }
47
48 // Check columns
49 for (let j = 0; j < 3; j++) {
50 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
51 return true;
52 }
53 }
54
55 // Check diagonals
56 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
57 return true;
58 }
59 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
60 return true;
61 }
62
63 return false;
64 };
65
66 const xWins = checkWin('X');
67 const oWins = checkWin('O');
68
69 // Edge Case: Both players win (impossible board)
70 if (xWins && oWins) {
71 throw new Error('Impossible board: game should have ended after the game was won');
72 }
73
74 // Edge Case: Game continued after win
75 if (xWins && oCount >= xCount) {
76 throw new Error('Impossible board: game should have ended after the game was won');
77 }
78
79 // Edge Case: Game continued after win
80 if (oWins && xCount > oCount) {
81 throw new Error('Impossible board: game should have ended after the game was won');
82 }
83
84 // Edge Case: Invalid turn order - O started the game
85 if (oCount > xCount && xCount === 0) {
86 throw new Error('Wrong turn order: O started');
87 }
88
89 // Edge Case: Invalid turn order - O went twice in a row
90 if (oCount > xCount && xCount >= 1) {
91 throw new Error('Wrong turn order: O went twice');
92 }
93
94 // Edge Case: Invalid turn order - X went twice in a row (more than one turn difference)
95 if (xCount > oCount + 1) {
96 throw new Error('Wrong turn order: X went twice');
97 }
98
99 // Determine game state
100 if (xWins) {
101 return 'win';
102 }
103
104 if (oWins) {
105 return 'win';
106 }
107
108 // Edge Case: Board is full (draw)
109 if (xCount + oCount === 9) {
110 return 'draw';
111 }
112
113 // Edge Case: Game is not finished yet (ongoing)
114 return 'ongoing';
115
116 // Handled Edge Cases:
117 // - Invalid board dimensions
118 // - Invalid characters on board
119 // - O started the game
120 // - X went twice in a row
121 // - Both players win (impossible)
122 // - Game continued after X win
123 // - Game continued after O win
124 // - Board is full (draw)
125 // - Game is not finished yet (ongoing)
126};
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.