tobe

Finished
88 * @returns {string}
99 */
1010export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
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
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: Impossible board - game continued after win
80 if (xWins && oWins) {
81 throw new Error('Impossible board: game should have ended after the game was won');
82 }
83
84 // Determine game state
85 if (xWins) {
86 return 'win';
87 }
88
89 if (oWins) {
90 return 'win';
91 }
92
93 // Edge Case: Game ended in a draw - board is full
94 if (xCount + oCount === 9) {
95 return 'draw';
96 }
97
98 // Edge Case: Game is ongoing - board is not full and no one has won
99 return 'ongoing';
12100};
101
102// Handled Edge Cases: Invalid board dimensions, Invalid characters on board, O started the game, X went twice in a row, Game continued after win, Game ended in a draw, Game is ongoing
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.