| 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 3 rows'); |
| 17 | } |
| 18 | |
| 19 | for (const row of board) { |
| 20 | if (typeof row !== 'string' || row.length !== 3) { |
| 21 | throw new Error('Invalid board: each row must be a string of length 3'); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Count X's and O's |
| 26 | let xCount = 0; |
| 27 | let oCount = 0; |
| 28 | |
| 29 | for (const row of board) { |
| 30 | for (const cell of row) { |
| 31 | if (cell === 'X') xCount++; |
| 32 | else if (cell === 'O') oCount++; |
| 33 | else if (cell !== ' ') { |
| 34 | throw new Error('Invalid board: contains invalid characters'); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Check for wins first to handle game continuation scenarios |
| 40 | const winPatterns = [ |
| 41 | // Rows |
| 42 | [0, 1, 2], |
| 43 | [3, 4, 5], |
| 44 | [6, 7, 8], |
| 45 | // Columns |
| 46 | [0, 3, 6], |
| 47 | [1, 4, 7], |
| 48 | [2, 5, 8], |
| 49 | // Diagonals |
| 50 | [0, 4, 8], |
| 51 | [2, 4, 6] |
| 52 | ]; |
| 53 | |
| 54 | // Convert board to flat array for easier checking |
| 55 | const flatBoard = []; |
| 56 | for (const row of board) { |
| 57 | for (const cell of row) { |
| 58 | flatBoard.push(cell); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | let xWins = false; |
| 63 | let oWins = false; |
| 64 | |
| 65 | for (const pattern of winPatterns) { |
| 66 | const [a, b, c] = pattern; |
| 67 | if (flatBoard[a] === 'X' && flatBoard[b] === 'X' && flatBoard[c] === 'X') { |
| 68 | xWins = true; |
| 69 | } |
| 70 | if (flatBoard[a] === 'O' && flatBoard[b] === 'O' && flatBoard[c] === 'O') { |
| 71 | oWins = true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Edge Case: Both players win (impossible in a valid game) |
| 76 | if (xWins && oWins) { |
| 77 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 78 | } |
| 79 | |
| 80 | // Edge Case: Game continued after win |
| 81 | // If X wins, O should have exactly one less move than X (X just won) |
| 82 | // If O made as many or more moves than X, then O played after X won |
| 83 | if (xWins && oCount >= xCount) { |
| 84 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 85 | } |
| 86 | |
| 87 | // Edge Case: Game continued after win |
| 88 | // If O wins, X should have exactly the same number of moves as O (O just won) |
| 89 | // If X made more moves than O, then X played after O won |
| 90 | if (oWins && xCount > oCount) { |
| 91 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 92 | } |
| 93 | |
| 94 | // Validate turn order, but be careful about win continuation scenarios |
| 95 | // Edge Case: Invalid turn order - O started (unless O just won) |
| 96 | if (oCount > xCount && !oWins) { |
| 97 | throw new Error('Wrong turn order: O started'); |
| 98 | } |
| 99 | |
| 100 | // Edge Case: Invalid turn order - X went twice in a row (unless X just won) |
| 101 | if (xCount > oCount + 1 && !xWins) { |
| 102 | throw new Error('Wrong turn order: X went twice'); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | |
| 107 | // Determine game state |
| 108 | if (xWins || oWins) { |
| 109 | return 'win'; |
| 110 | } |
| 111 | |
| 112 | // Edge Case: Draw - board is full with no winner |
| 113 | if (xCount + oCount === 9) { |
| 114 | return 'draw'; |
| 115 | } |
| 116 | |
| 117 | // Edge Case: Ongoing game - board is not full and no winner |
| 118 | return 'ongoing'; |
| 119 | |
| 120 | // Handled Edge Cases: |
| 121 | // - Invalid turn order (O started) |
| 122 | // - Invalid turn order (X went twice) |
| 123 | // - Both players win |
| 124 | // - Game continued after X win |
| 125 | // - Game continued after O win |
| 126 | // - Draw (full board, no winner) |
| 127 | // - Ongoing game (incomplete board, no winner) |
| 128 | }; |