joseph

Finished
88 * @returns {string}
99 */
1010export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
11 // Edge Case: Board is not 3x3
12 if (board.length !== 3 || board.some(row => row.length !== 3)) {
13 throw new Error('Invalid board: must be 3x3');
14 }
15
16 // Count X's and O's and check for invalid characters
17 let xCount = 0;
18 let oCount = 0;
19 let hasInvalidChar = false;
20
21 for (let i = 0; i < 3; i++) {
22 for (let j = 0; j < 3; j++) {
23 const cell = board[i][j];
24 if (cell !== 'X' && cell !== 'O' && cell !== ' ') {
25 hasInvalidChar = true;
26 }
27 if (cell === 'X') xCount++;
28 if (cell === 'O') oCount++;
29 }
30 }
31
32 // Check for wins
33 const checkWin = (player) => {
34 // Check rows
35 for (let i = 0; i < 3; i++) {
36 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
37 return true;
38 }
39 }
40
41 // Check columns
42 for (let j = 0; j < 3; j++) {
43 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
44 return true;
45 }
46 }
47
48 // Check diagonals
49 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
50 return true;
51 }
52 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
53 return true;
54 }
55
56 return false;
57 };
58
59 // Debug win detection for specific case
60 // For board ['XOX', ' O ', ' XO']:
61 // board[0] = 'XOX', board[1] = ' O ', board[2] = ' XO'
62 // Column 1: board[0][1] = 'O', board[1][1] = 'O', board[2][1] = 'O' - should be win!
63
64 // Debug: Check win detection for 'O' with board ['XOX', ' O ', ' XO']
65 // Row 0: XOX - no
66 // Row 1: ' O ' - no
67 // Row 2: ' XO' - no
68 // Col 0: X X - no
69 // Col 1: O O - yes! This should return true
70 // Col 2: X O - no
71 // Diag 1: X X - no
72 // Diag 2: X X - no
73
74 const xWins = checkWin('X');
75 const oWins = checkWin('O');
76
77 // Edge Case: Both players win (impossible)
78 if (xWins && oWins) {
79 throw new Error('Impossible board: game should have ended after the game was won');
80 }
81
82 // Edge Case: Game continued after win
83 if (xWins && xCount === oCount) {
84 throw new Error('Impossible board: game should have ended after the game was won');
85 }
86
87 // Edge Case: Game continued after win
88 if (oWins && xCount > oCount) {
89 throw new Error('Impossible board: game should have ended after the game was won');
90 }
91
92 // Edge Case: Wrong turn order - validate based on win state
93 if (xWins) {
94 // X won, so X should have exactly one more piece than O
95 if (xCount < oCount) {
96 throw new Error('Wrong turn order: O started');
97 }
98 if (xCount > oCount + 1) {
99 throw new Error('Wrong turn order: X went twice');
100 }
101 } else if (oWins) {
102 // O won, so counts should be equal
103 if (oCount < xCount) {
104 throw new Error('Wrong turn order: X went twice');
105 }
106 if (oCount > xCount) {
107 throw new Error('Wrong turn order: O went twice');
108 }
109 } else {
110 // No one has won yet, check normal turn order
111 // O started
112 if (oCount > 0 && xCount === 0) {
113 throw new Error('Wrong turn order: O started');
114 }
115
116 // X went twice
117 if (xCount > oCount + 1) {
118 throw new Error('Wrong turn order: X went twice');
119 }
120
121 // O went twice
122 if (oCount > xCount + 1) {
123 throw new Error('Wrong turn order: O went twice');
124 }
125 }
126
127 // Edge Case: Invalid characters on board (check after turn order as per test comment)
128 if (hasInvalidChar) {
129 throw new Error('Invalid board: contains invalid characters');
130 }
131
132 // Determine game state
133 if (xWins) {
134 return 'win';
135 }
136
137 if (oWins) {
138 return 'win';
139 }
140
141 // If board is full, it's a draw
142 if (xCount + oCount === 9) {
143 return 'draw';
144 }
145
146 // Otherwise, game is ongoing
147 return 'ongoing';
12148};
149
150// Handled Edge Cases: Board is not 3x3, Invalid characters on board, Wrong turn order - O started, Wrong turn order - X went twice, Both players win (impossible), Game continued after win
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.