let it go

Finished
1//
2// This is only a SKELETON file for the 'State of Tic Tac Toe' exercise. It's been provided as a
3// convenience to get you started writing code faster.
4//
5
6/**
7 * @param {string[]} board
8 * @returns {string}
1/*
2 * Determines the state of a Tic-Tac-Toe game based on the given board.
3 *
4 * @param {string[]} board - An array of strings representing the game board.
5 * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state.
6 * @throws {Error} - Throws an error for invalid board states with specific messages.
97 */
108export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
9 // Validate board structure
10 if (!Array.isArray(board) || board.length !== 3) {
11 throw new Error('Invalid board: must have exactly 3 rows');
12 }
13
14 for (let i = 0; i < 3; i++) {
15 if (typeof board[i] !== 'string' || board[i].length !== 3) {
16 throw new Error('Invalid board: each row must be a string of exactly 3 characters');
17 }
18 }
19
20 // Count X's and O's
21 let xCount = 0;
22 let oCount = 0;
23
24 for (let i = 0; i < 3; i++) {
25 for (let j = 0; j < 3; j++) {
26 const cell = board[i][j];
27 if (cell === 'X') {
28 xCount++;
29 } else if (cell === 'O') {
30 oCount++;
31 } else if (cell !== ' ') {
32 throw new Error('Invalid board: contains invalid characters');
33 }
34 }
35 }
36
37 // Edge Case: Invalid turn order - X went twice in a row
38 if (xCount > oCount + 1) {
39 throw new Error('Wrong turn order: X went twice');
40 }
41
42 // Edge Case: Invalid turn order - O went twice in a row or O started
43 if (oCount > xCount) {
44 if (xCount === 0) {
45 throw new Error('Wrong turn order: O started');
46 } else {
47 throw new Error('Wrong turn order: O went twice');
48 }
49 }
50
51 // Check for wins
52 const checkWin = (player) => {
53 // Check rows
54 for (let i = 0; i < 3; i++) {
55 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
56 return true;
57 }
58 }
59
60 // Check columns
61 for (let j = 0; j < 3; j++) {
62 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
63 return true;
64 }
65 }
66
67 // Check diagonals
68 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
69 return true;
70 }
71
72 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
73 return true;
74 }
75
76 return false;
77 };
78
79 const xWins = checkWin('X');
80 const oWins = checkWin('O');
81
82 // Edge Case: Both players win (impossible in a valid game)
83 if (xWins && oWins) {
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 X wins, O should have played either the same number of times (X won on their turn)
89 // or one less time (X won and board isn't full)
90 if (xWins && oCount > xCount) {
91 throw new Error('Impossible board: game should have ended after the game was won');
92 }
93
94 // Edge Case: Game continued after win
95 // If O wins, X should have played either the same number of times (O won on their turn)
96 // or one more time (X started and O won)
97 if (oWins && xCount > oCount + 1) {
98 throw new Error('Impossible board: game should have ended after the game was won');
99 }
100
101 // Determine game state
102 if (xWins || oWins) {
103 return 'win';
104 }
105
106 // Edge Case: Draw - board is full with no winner
107 if (xCount + oCount === 9) {
108 return 'draw';
109 }
110
111 // Edge Case: Ongoing game - board is not full and no winner
112 return 'ongoing';
113
114 // Handled Edge Cases:
115 // - Invalid board structure
116 // - Invalid characters on board
117 // - O started the game
118 // - X went twice in a row
119 // - Both players win (impossible)
120 // - Game continued after X win
121 // - Game continued after O win
122 // - Draw (full board with no winner)
123 // - Ongoing game (incomplete board with no winner)
124};
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.