fishlium

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