lexicon

Finished
44//
55
66/**
7 * @param {string[]} board
8 * @returns {string}
7 * Determines the state of a Tic-Tac-Toe game based on the given board.
8 *
9 * @param {string[]} board - An array of 3 strings, each representing a row of the board
10 * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state
11 * @throws {Error} - Throws an error for invalid board states with specific messages
912 */
1013export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
14 // Validate board structure
15 if (!Array.isArray(board) || board.length !== 3) {
16 throw new Error('Invalid board: must have exactly 3 rows');
17 }
18
19 for (let i = 0; i < 3; i++) {
20 if (typeof board[i] !== 'string' || board[i].length !== 3) {
21 throw new Error('Invalid board: each row must be a string of exactly 3 characters');
22 }
23 }
24
25 // Count X's and O's
26 let xCount = 0;
27 let oCount = 0;
28
29 for (let i = 0; i < 3; i++) {
30 for (let j = 0; j < 3; j++) {
31 const cell = board[i][j];
32 if (cell === 'X') {
33 xCount++;
34 } else if (cell === 'O') {
35 oCount++;
36 } else if (cell !== ' ') {
37 // Edge Case: Invalid characters on the board
38 throw new Error('Invalid board: contains invalid characters');
39 }
40 }
41 }
42
43 // Check for wins
44 const checkWin = (player) => {
45 // Check rows
46 for (let i = 0; i < 3; i++) {
47 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
48 return true;
49 }
50 }
51
52 // Check columns
53 for (let j = 0; j < 3; j++) {
54 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
55 return true;
56 }
57 }
58
59 // Check diagonals
60 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
61 return true;
62 }
63
64 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
65 return true;
66 }
67
68 return false;
69 };
70
71 const xWins = checkWin('X');
72 const oWins = checkWin('O');
73
74 // Edge Case: Both players win (impossible in a valid game)
75 if (xWins && oWins) {
76 throw new Error('Impossible board: game should have ended after the game was won');
77 }
78
79 // Validate turn order for all boards
80 // O cannot have more moves than X
81 if (oCount > xCount) {
82 if (xCount === 0) {
83 // O started the game
84 throw new Error('Wrong turn order: O started');
85 } else {
86 // O went twice in a row
87 throw new Error('Wrong turn order: O went twice');
88 }
89 }
90
91 // X can have at most one more move than O
92 if (xCount > oCount + 1) {
93 throw new Error('Wrong turn order: X went twice');
94 }
95
96 // Check for wins
97 if (xWins || oWins) {
98 // Edge Case: Game continued after a win
99 // If X wins, O should not have made moves after X's winning move
100 // X wins with at least 3 X's, so O should have at most 2 moves
101 if (xWins && oCount >= 3) {
102 throw new Error('Impossible board: game should have ended after the game was won');
103 }
104
105 // If O wins, X should not have made moves after O's winning move
106 // O wins with at least 3 O's, so X should have at most 3 moves
107 if (oWins && xCount > 3) {
108 throw new Error('Impossible board: game should have ended after the game was won');
109 }
110
111 // Valid win
112 return 'win';
113 }
114
115 // Edge Case: Board is full (draw)
116 if (xCount + oCount === 9) {
117 return 'draw';
118 }
119
120 // Game is ongoing
121 return 'ongoing';
122
123 // Determine game state
124 if (xWins || oWins) {
125 return 'win';
126 }
127
128 // Edge Case: Board is full (draw)
129 if (xCount + oCount === 9) {
130 return 'draw';
131 }
132
133 // Game is ongoing
134 return 'ongoing';
12135};
136
137// Handled Edge Cases:
138// 1. Invalid board structure (not 3 rows or rows not 3 characters)
139// 2. Invalid characters on the board
140// 3. Wrong turn order - O started
141// 4. Wrong turn order - X went twice in a row
142// 5. Both players win (impossible)
143// 6. Game continued after X win
144// 7. Game continued after O win
145// 8. Board is full (draw)
Test NameStatus
Finished game where X won via top row victory
Fail
Finished game where X won via bottom row victory
Fail
Finished game where O won via top row victory
Fail
Finished game where O won via bottom row victory
Fail
Finished game where O won via falling diagonal victory
Fail
Finished game where X won via a row and a column victory
Fail
Finished game where X won via two diagonal victories
Fail
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 middle row victory
Pass
Finished game where O won via middle 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 rising diagonal victory
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.