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 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
70 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
71 return true;
72 }
73
74 return false;
75 };
76
77 const xWins = wins('X');
78 const oWins = wins('O');
79
80 // Edge Case: Both players have winning positions (continued playing after win)
81 if (xWins && oWins) {
82 throw new Error('Impossible board: game should have ended after the game was won');
83 }
84
85 // Edge Case: X won but O played next (continued playing after win)
86 if (xWins && xCount === oCount) {
87 throw new Error('Impossible board: game should have ended after the game was won');
88 }
89
90 // Edge Case: O won but X played next (continued playing after win)
91 if (oWins && xCount > oCount) {
92 throw new Error('Impossible board: game should have ended after the game was won');
93 }
94
95 // Determine game state
96 if (xWins) {
97 return 'win';
98 }
99
100 if (oWins) {
101 return 'win';
102 }
103
104 // Edge Case: Board is full with no winner (draw)
105 if (xCount + oCount === 9) {
106 return 'draw';
107 }
108
109 // Edge Case: Game is still ongoing
110 return 'ongoing';
111
112 // Handled Edge Cases:
113 // 1. Invalid turn order - O started the game
114 // 2. Invalid turn order - X went twice in a row
115 // 3. Both players have winning positions
116 // 4. X won but O played next
117 // 5. O won but X played next
118 // 6. Board is full with no winner
119 // 7. Game is still ongoing
120 // 8. Invalid board dimensions
121 // 9. Invalid characters on board
122};
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.