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 row of board) {
17 if (row.length !== 3) {
18 throw new Error('Invalid board: each row must have 3 columns');
19 }
20 }
21
22 // Count Xs and Os
23 let countX = 0;
24 let countO = 0;
25
26 for (let row of board) {
27 for (let cell of row) {
28 if (cell === 'X') {
29 countX++;
30 } else if (cell === 'O') {
31 countO++;
32 } else if (cell !== ' ') {
33 throw new Error('Invalid board: contains invalid characters');
34 }
35 }
36 }
37
38 // Check for wins first
39 const winX = checkWin(board, 'X');
40 const winO = checkWin(board, 'O');
41
42 // Edge Case: Both players won (impossible)
43 if (winX && winO) {
44 throw new Error('Impossible board: game should have ended after the game was won');
45 }
46
47 // If there's a winner, check for extreme turn order violations
48 if (winX || winO) {
49 // X went twice in a row (X has 2+ more than O)
50 if (countX > countO + 1) {
51 throw new Error('Wrong turn order: X went twice');
52 }
53
54 // O went twice in a row (O has more than X)
55 if (countO > countX) {
56 throw new Error('Wrong turn order: O went twice');
57 }
58
59 // If we have a valid win, return 'win'
60 return 'win';
61 }
62
63 // For non-winning boards, validate turn order strictly
64 // O started the game (O has moves but X has none)
65 if (countO > 0 && countX === 0) {
66 throw new Error('Wrong turn order: O started');
67 }
68
69 // X went twice in a row (X has 2+ more than O)
70 if (countX > countO + 1) {
71 throw new Error('Wrong turn order: X went twice');
72 }
73
74 // O went twice in a row (O has more than X)
75 if (countO > countX) {
76 throw new Error('Wrong turn order: O went twice');
77 }
78
79 // Determine game state
80 if (winX || winO) {
81 return 'win';
82 }
83
84 // Edge Case: Draw - board is full with no winner
85 if (countX + countO === 9) {
86 return 'draw';
87 }
88
89 // Edge Case: Game is ongoing
90 return 'ongoing';
1291};
92
93/**
94 * Helper function to check if a player has won
95 * @param {string[]} board
96 * @param {string} player
97 * @returns {boolean}
98 */
99const checkWin = (board, player) => {
100 // Check rows
101 for (let i = 0; i < 3; i++) {
102 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
103 return true;
104 }
105 }
106
107 // Check columns
108 for (let i = 0; i < 3; i++) {
109 if (board[0][i] === player && board[1][i] === player && board[2][i] === player) {
110 return true;
111 }
112 }
113
114 // Check diagonals
115 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
116 return true;
117 }
118
119 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
120 return true;
121 }
122
123 return false;
124};
125
126// Handled Edge Cases:
127// 1. Invalid board dimensions
128// 2. Invalid characters on board
129// 3. Wrong turn order - O started
130// 4. Wrong turn order - X went twice in a row
131// 5. Both players won (impossible)
132// 6. Game continued after X win
133// 7. Game continued after O win
134// 8. Draw - board is full with no winner
135// 9. Game is ongoing
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.