| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | /** |
| 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 representing the rows of the board. |
| 10 | * Each string contains 3 characters: 'X', 'O', or ' '. |
| 11 | * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state. |
| 12 | * @throws {Error} - Throws an error for invalid board states with specific messages. |
| 9 | 13 | */ |
| 10 | 14 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | | }; |
| 15 | // Validate board structure |
| 16 | if (!Array.isArray(board) || board.length !== 3) { |
| 17 | throw new Error('Invalid board: must have 3 rows'); |
| 18 | } |
| 19 | |
| 20 | for (let i = 0; i < 3; i++) { |
| 21 | if (typeof board[i] !== 'string' || board[i].length !== 3) { |
| 22 | throw new Error('Invalid board: each row must be a string of 3 characters'); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // Count Xs and Os |
| 27 | let countX = 0; |
| 28 | let countO = 0; |
| 29 | |
| 30 | for (let i = 0; i < 3; i++) { |
| 31 | for (let j = 0; j < 3; j++) { |
| 32 | const char = board[i][j]; |
| 33 | if (char === 'X') { |
| 34 | countX++; |
| 35 | } else if (char === 'O') { |
| 36 | countO++; |
| 37 | } else if (char !== ' ') { |
| 38 | throw new Error('Invalid board: contains invalid characters'); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Edge Case: Invalid turn order - O started or went twice |
| 44 | if (countO > countX) { |
| 45 | // If X has no moves, O clearly started |
| 46 | if (countX === 0) { |
| 47 | throw new Error('Wrong turn order: O started'); |
| 48 | } |
| 49 | |
| 50 | // For cases where both X and O have moves, we need to determine |
| 51 | // if O started or went twice. We'll use a heuristic based on |
| 52 | // the positions of the moves. |
| 53 | |
| 54 | // Collect positions of all X's and O's |
| 55 | const xPositions = []; |
| 56 | const oPositions = []; |
| 57 | |
| 58 | for (let i = 0; i < 3; i++) { |
| 59 | for (let j = 0; j < 3; j++) { |
| 60 | const char = board[i][j]; |
| 61 | if (char === 'X') { |
| 62 | xPositions.push(i * 3 + j); // Convert 2D position to 1D index |
| 63 | } else if (char === 'O') { |
| 64 | oPositions.push(i * 3 + j); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Sort positions |
| 70 | xPositions.sort((a, b) => a - b); |
| 71 | oPositions.sort((a, b) => a - b); |
| 72 | |
| 73 | // If all O's come before the first X in reading order, O started |
| 74 | if (oPositions.length > 0 && xPositions.length > 0 && |
| 75 | Math.max(...oPositions) < Math.min(...xPositions)) { |
| 76 | throw new Error('Wrong turn order: O started'); |
| 77 | } else { |
| 78 | throw new Error('Wrong turn order: O went twice'); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Edge Case: Invalid turn order - X went twice in a row |
| 83 | if (countX > countO + 1) { |
| 84 | throw new Error('Wrong turn order: X went twice'); |
| 85 | } |
| 86 | |
| 87 | // Check for wins |
| 88 | const wins = (player) => { |
| 89 | // Check rows |
| 90 | for (let i = 0; i < 3; i++) { |
| 91 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 92 | return true; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Check columns |
| 97 | for (let j = 0; j < 3; j++) { |
| 98 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 99 | return true; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Check diagonals |
| 104 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | return false; |
| 113 | }; |
| 114 | |
| 115 | const xWins = wins('X'); |
| 116 | const oWins = wins('O'); |
| 117 | |
| 118 | // Edge Case: Both players win (impossible in a valid game) |
| 119 | if (xWins && oWins) { |
| 120 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 121 | } |
| 122 | |
| 123 | // Edge Case: X wins but O continued playing |
| 124 | if (xWins && countO >= countX) { |
| 125 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 126 | } |
| 127 | |
| 128 | // Edge Case: O wins but X continued playing |
| 129 | if (oWins && countX > countO + 1) { |
| 130 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 131 | } |
| 132 | |
| 133 | // Determine game state |
| 134 | if (xWins || oWins) { |
| 135 | return 'win'; |
| 136 | } |
| 137 | |
| 138 | // Edge Case: Board is full (draw) |
| 139 | if (countX + countO === 9) { |
| 140 | return 'draw'; |
| 141 | } |
| 142 | |
| 143 | // Edge Case: Game is still ongoing |
| 144 | return 'ongoing'; |
| 145 | |
| 146 | // Handled Edge Cases: |
| 147 | // - Invalid board structure |
| 148 | // - Invalid characters on board |
| 149 | // - O started the game |
| 150 | // - X went twice in a row |
| 151 | // - Both players win (impossible) |
| 152 | // - X wins but O continued playing |
| 153 | // - O wins but X continued playing |
| 154 | // - Board is full (draw) |
| 155 | // - Game is still ongoing |
| 156 | }; |