| 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'); |
| 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 X's and O's |
| 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 | // Edge Case: Invalid characters on the board |
| 38 | throw new Error('Invalid board: contains invalid characters'); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Validate turn order first |
| 44 | // X starts first, so X count must be equal to or one more than O count |
| 45 | if (oCount > xCount) { |
| 46 | throw new Error('Wrong turn order: O started'); |
| 47 | } |
| 48 | |
| 49 | if (xCount > oCount + 1) { |
| 50 | throw new Error('Wrong turn order: X went twice'); |
| 51 | } |
| 52 | |
| 53 | // Check for wins |
| 54 | const checkWin = (player) => { |
| 55 | // Check rows |
| 56 | for (let i = 0; i < 3; i++) { |
| 57 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Check columns |
| 63 | for (let j = 0; j < 3; j++) { |
| 64 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Check diagonals |
| 70 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | }; |
| 80 | |
| 81 | const xWins = checkWin('X'); |
| 82 | const oWins = checkWin('O'); |
| 83 | |
| 84 | // Edge Case: Both players win (impossible in a valid game) |
| 85 | if (xWins && oWins) { |
| 86 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 87 | } |
| 88 | |
| 89 | // Edge Case: Game continued after a win |
| 90 | // If X wins, O should not have made the same number or more moves |
| 91 | if (xWins && oCount >= xCount) { |
| 92 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 93 | } |
| 94 | |
| 95 | // Edge Case: Game continued after a win |
| 96 | // If O wins, X should not have made more moves |
| 97 | if (oWins && xCount > oCount) { |
| 98 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 99 | } |
| 100 | |
| 101 | // Determine game state |
| 102 | if (xWins || oWins) { |
| 103 | return 'win'; |
| 104 | } |
| 105 | |
| 106 | // Edge Case: Board is full (draw) |
| 107 | if (xCount + oCount === 9) { |
| 108 | return 'draw'; |
| 109 | } |
| 110 | |
| 111 | // Game is ongoing |
| 112 | return 'ongoing'; |
| 12 | 113 | }; |
| 114 | |
| 115 | // Handled Edge Cases: |
| 116 | // 1. Invalid board structure (not 3 rows or rows not 3 characters) |
| 117 | // 2. Invalid characters on the board |
| 118 | // 3. Wrong turn order - O started |
| 119 | // 4. Wrong turn order - X went twice in a row |
| 120 | // 5. Both players win (impossible) |
| 121 | // 6. Game continued after X win |
| 122 | // 7. Game continued after O win |
| 123 | // 8. Board is full (draw) |