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 wins = (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 = wins('X');
77 const oWins = wins('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 a 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 a 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 || oWins) {
96 return 'win';
97 }
98
99 // Edge Case: Board is full with no winner (draw)
100 if (xCount + oCount === 9) {
101 return 'draw';
102 }
103
104 // Edge Case: Game is not finished yet (ongoing)
105 return 'ongoing';
12106};
107
108// Handled Edge Cases: Invalid board dimensions, Invalid characters on board, O started the game, X went twice in a row, Both players win, Game continued after X win, Game continued after O win, Board is full with no winner, Game is not finished yet
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.