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