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