Finished
44//
55
66export const gamestate = (board) => {
7 throw new Error('Remove this line and implement the function');
7 // Edge Case: Board with incorrect dimensions
8 if (board.length !== 3 || board.some(row => row.length !== 3)) {
9 throw new Error('Invalid board dimensions');
10 }
11
12 // Edge Case: Board with invalid characters
13 const validChars = ['X', 'O', ' '];
14 for (let i = 0; i < 3; i++) {
15 for (let j = 0; j < 3; j++) {
16 if (!validChars.includes(board[i][j])) {
17 throw new Error('Invalid character on board');
18 }
19 }
20 }
21
22 // Count Xs and Os
23 let xCount = 0;
24 let oCount = 0;
25 for (let i = 0; i < 3; i++) {
26 for (let j = 0; j < 3; j++) {
27 if (board[i][j] === 'X') xCount++;
28 if (board[i][j] === 'O') oCount++;
29 }
30 }
31
32 // Edge Case: Wrong turn order (O starts or count mismatch)
33 if (oCount > xCount) {
34 throw new Error('Wrong turn order: O started');
35 }
36 if (xCount > oCount + 1) {
37 throw new Error('Wrong turn order: X went twice');
38 }
39
40 // Check for wins
41 const checkWin = (player) => {
42 // Check rows
43 for (let i = 0; i < 3; i++) {
44 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
45 return true;
46 }
47 }
48
49 // Check columns
50 for (let j = 0; j < 3; j++) {
51 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
52 return true;
53 }
54 }
55
56 // Check diagonals
57 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
58 return true;
59 }
60 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
61 return true;
62 }
63
64 return false;
65 };
66
67 const xWins = checkWin('X');
68 const oWins = checkWin('O');
69
70 // Edge Case: Game was played after it already ended
71 if (xWins && oWins) {
72 throw new Error('Impossible board: game should have ended after the game was won');
73 }
74
75 // Edge Case: Game was played after it already ended
76 if (xWins && oCount >= xCount) {
77 throw new Error('Impossible board: game should have ended after the game was won');
78 }
79
80 // Edge Case: Game was played after it already ended
81 if (oWins && xCount > oCount) {
82 throw new Error('Impossible board: game should have ended after the game was won');
83 }
84
85 // Determine game state
86 if (xWins || oWins) {
87 return 'win';
88 }
89
90 if (xCount + oCount === 9) {
91 return 'draw';
92 }
93
94 return 'ongoing';
895};
96
97// Handled Edge Cases: Board with incorrect dimensions, Board with invalid characters, Wrong turn order (O starts or count mismatch), Game was played after it already ended
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.