| 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 | // Edge Case: Invalid turn order - X went twice in a row |
| 43 | if (xCount > oCount + 1) { |
| 44 | throw new Error('Wrong turn order: X went twice'); |
| 45 | } |
| 46 | |
| 47 | // Edge Case: Invalid turn order - O went twice in a row or O started |
| 48 | if (oCount > xCount) { |
| 49 | // Check if O started the game (first move was O) |
| 50 | let firstMove = null; |
| 51 | outer: for (let i = 0; i < 3; i++) { |
| 52 | for (let j = 0; j < 3; j++) { |
| 53 | if (board[i][j] === 'X') { |
| 54 | firstMove = 'X'; |
| 55 | break outer; |
| 56 | } else if (board[i][j] === 'O') { |
| 57 | firstMove = 'O'; |
| 58 | break outer; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (firstMove === 'O') { |
| 64 | throw new Error('Wrong turn order: O started'); |
| 65 | } else { |
| 66 | throw new Error('Wrong turn order: O went twice'); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Edge Case: Invalid turn order - Equal counts but X should have one more |
| 71 | // This is only invalid if X started (which is always the case in this exercise) |
| 72 | // In a valid game, if X starts, then xCount should be oCount or oCount+1 |
| 73 | // If xCount === oCount, that means O made the last move, which is valid |
| 74 | // Only throw error if xCount < oCount (O started) or xCount > oCount + 1 (X went twice) |
| 75 | |
| 76 | // Check for wins |
| 77 | const checkWin = (player) => { |
| 78 | // Check rows |
| 79 | for (let i = 0; i < 3; i++) { |
| 80 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Check columns |
| 86 | for (let j = 0; j < 3; j++) { |
| 87 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Check diagonals |
| 93 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | }; |
| 103 | |
| 104 | const xWins = checkWin('X'); |
| 105 | const oWins = checkWin('O'); |
| 106 | |
| 107 | // Edge Case: Both players win (impossible in a valid game) |
| 108 | if (xWins && oWins) { |
| 109 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 110 | } |
| 111 | |
| 112 | // Edge Case: X wins but O continued playing |
| 113 | // If X wins, O should have exactly one fewer move than X |
| 114 | if (xWins && oCount >= xCount) { |
| 115 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 116 | } |
| 117 | |
| 118 | // Edge Case: O wins but X continued playing |
| 119 | // If O wins, X should have the same number of moves as O |
| 120 | if (oWins && xCount > oCount) { |
| 121 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 122 | } |
| 123 | |
| 124 | // Determine game state |
| 125 | if (xWins || oWins) { |
| 126 | return 'win'; |
| 127 | } |
| 128 | |
| 129 | // Edge Case: Board is full (draw) |
| 130 | if (xCount + oCount === 9) { |
| 131 | return 'draw'; |
| 132 | } |
| 133 | |
| 134 | // Game is ongoing |
| 135 | return 'ongoing'; |
| 136 | |
| 137 | // Handled Edge Cases: |
| 138 | // 1. Invalid turn order - O started |
| 139 | // 2. Invalid turn order - X went twice in a row |
| 140 | // 3. Both players win (impossible in a valid game) |
| 141 | // 4. X wins but O continued playing |
| 142 | // 5. O wins but X continued playing |
| 143 | // 6. Board is full (draw) |
| 144 | // 7. Invalid board structure (not 3x3) |
| 145 | // 8. Invalid characters on board |
| 146 | }; |