weekends

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 * Determine the state of a tic-tac-toe game given a 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.
97 */
108export const gamestate = (board) => {
11 throw new Error('Remove this line and implement the function');
12};
9 // Validate board dimensions
10 if (board.length !== 3 || board.some(row => row.length !== 3)) {
11 throw new Error('Invalid board: must be 3x3');
12 }
13
14 // Count X's and O's
15 let xCount = 0;
16 let oCount = 0;
17
18 for (let i = 0; i < 3; i++) {
19 for (let j = 0; j < 3; j++) {
20 const cell = board[i][j];
21 if (cell === 'X') xCount++;
22 else if (cell === 'O') oCount++;
23 else if (cell !== ' ') {
24 // Edge Case: Invalid characters on the board
25 throw new Error('Invalid board: contains invalid characters');
26 }
27 }
28 }
29
30 // Edge Case: Wrong turn order - O started the game
31 if (oCount > 0 && xCount === 0) {
32 throw new Error('Wrong turn order: O started');
33 }
34
35 // Edge Case: Wrong turn order - X went twice
36 if (xCount > oCount + 1) {
37 throw new Error('Wrong turn order: X went twice');
38 }
39
40 // Edge Case: Wrong turn order - O went twice
41 if (oCount > xCount) {
42 throw new Error('Wrong turn order: O went twice');
43 }
44
45 // Check for wins
46 const win = (player) => {
47 // Check rows
48 for (let i = 0; i < 3; i++) {
49 if (board[i][0] === player && board[i][1] === player && board[i][2] === player) {
50 return true;
51 }
52 }
53
54 // Check columns
55 for (let j = 0; j < 3; j++) {
56 if (board[0][j] === player && board[1][j] === player && board[2][j] === player) {
57 return true;
58 }
59 }
60
61 // Check diagonals
62 if (board[0][0] === player && board[1][1] === player && board[2][2] === player) {
63 return true;
64 }
65
66 if (board[0][2] === player && board[1][1] === player && board[2][0] === player) {
67 return true;
68 }
69
70 return false;
71 };
72
73 const xWins = win('X');
74 const oWins = win('O');
75
76 // Edge Case: Both players win (impossible in valid games)
77 if (xWins && oWins) {
78 throw new Error('Impossible board: game should have ended after the game was won');
79 }
80
81 // Edge Case: Game continued after win
82 if (xWins && xCount === oCount) {
83 throw new Error('Impossible board: game should have ended after the game was won');
84 }
85
86 // Edge Case: Game continued after win
87 if (oWins && xCount > oCount) {
88 throw new Error('Impossible board: game should have ended after the game was won');
89 }
90
91 // Determine game state
92 if (xWins || oWins) {
93 return 'win';
94 }
95
96 // Edge Case: Board is full (draw)
97 if (xCount + oCount === 9) {
98 return 'draw';
99 }
100
101 // Edge Case: Game is not finished yet (ongoing)
102 return 'ongoing';
103
104 // Handled Edge Cases:
105 // 1. Invalid characters on the board
106 // 2. Wrong turn order - O started
107 // 3. Wrong turn order - X went twice in a row
108 // 4. Both players win (impossible in valid games)
109 // 5. Game continued after X win
110 // 6. Game continued after O win
111 // 7. Board is full (draw)
112 // 8. Game is not finished yet (ongoing)
113};
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.