| 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, each representing a row of the board |
| 10 | * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state |
| 11 | * @throws {Error} - Throws an error for invalid board states with specific messages |
| 9 | 12 | */ |
| 10 | 13 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | | }; |
| 14 | // Validate board structure |
| 15 | if (!Array.isArray(board) || board.length !== 3) { |
| 16 | throw new Error('Invalid board: must have exactly 3 rows'); |
| 17 | } |
| 18 | |
| 19 | for (let i = 0; i < 3; i++) { |
| 20 | if (typeof board[i] !== 'string' || board[i].length !== 3) { |
| 21 | throw new Error('Invalid board: each row must be a string of exactly 3 characters'); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Count Xs and Os |
| 26 | let xCount = 0; |
| 27 | let oCount = 0; |
| 28 | |
| 29 | for (let i = 0; i < 3; i++) { |
| 30 | for (let j = 0; j < 3; j++) { |
| 31 | const cell = board[i][j]; |
| 32 | if (cell === 'X') { |
| 33 | xCount++; |
| 34 | } else if (cell === 'O') { |
| 35 | oCount++; |
| 36 | } else if (cell !== ' ') { |
| 37 | throw new Error('Invalid board: only X, O, or space allowed'); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Check for wins |
| 43 | const checkWin = (player) => { |
| 44 | // Check rows |
| 45 | for (let i = 0; i < 3; i++) { |
| 46 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Check columns |
| 52 | for (let j = 0; j < 3; j++) { |
| 53 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 54 | return true; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Check diagonals |
| 59 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | return false; |
| 68 | }; |
| 69 | |
| 70 | const xWins = checkWin('X'); |
| 71 | const oWins = checkWin('O'); |
| 72 | |
| 73 | // Edge Case: Invalid turn order - O started (when O has more moves than X and X has 0 moves) |
| 74 | if (oCount > xCount && xCount === 0) { |
| 75 | throw new Error('Wrong turn order: O started'); |
| 76 | } |
| 77 | |
| 78 | // Edge Case: Invalid turn order - O went twice in a row (when O has more moves than X and X has at least 1 move) |
| 79 | if (oCount > xCount && xCount > 0) { |
| 80 | throw new Error('Wrong turn order: O went twice'); |
| 81 | } |
| 82 | |
| 83 | // Edge Case: Invalid turn order - X went twice in a row |
| 84 | if (xCount > oCount + 1) { |
| 85 | throw new Error('Wrong turn order: X went twice'); |
| 86 | } |
| 87 | |
| 88 | // Edge Case: Both players win (impossible in a valid game) |
| 89 | if (xWins && oWins) { |
| 90 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 91 | } |
| 92 | |
| 93 | // Edge Case: X wins but O made another move |
| 94 | if (xWins && oCount >= xCount) { |
| 95 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 96 | } |
| 97 | |
| 98 | // Edge Case: O wins but X made another move |
| 99 | if (oWins && xCount > oCount) { |
| 100 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 101 | } |
| 102 | |
| 103 | // Determine game state |
| 104 | if (xWins || oWins) { |
| 105 | return 'win'; |
| 106 | } |
| 107 | |
| 108 | // Edge Case: Board is full (draw) |
| 109 | if (xCount + oCount === 9) { |
| 110 | return 'draw'; |
| 111 | } |
| 112 | |
| 113 | // Game is ongoing |
| 114 | return 'ongoing'; |
| 115 | |
| 116 | // Handled Edge Cases: |
| 117 | // 1. Invalid turn order - O started |
| 118 | // 2. Invalid turn order - X went twice in a row |
| 119 | // 3. Both players win (impossible in a valid game) |
| 120 | // 4. X wins but O made another move |
| 121 | // 5. O wins but X made another move |
| 122 | // 6. Board is full (draw) |
| 123 | // 7. Invalid board structure |
| 124 | // 8. Invalid characters on board |
| 125 | }; |