| 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 game 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 dimensions |
| 15 | if (board.length !== 3 || board.some(row => row.length !== 3)) { |
| 16 | throw new Error('Invalid board: must be 3x3'); |
| 17 | } |
| 18 | |
| 19 | // Count Xs and Os (and ignore invalid characters) |
| 20 | let countX = 0; |
| 21 | let countO = 0; |
| 22 | |
| 23 | for (let i = 0; i < 3; i++) { |
| 24 | for (let j = 0; j < 3; j++) { |
| 25 | const cell = board[i][j]; |
| 26 | if (cell === 'X') countX++; |
| 27 | if (cell === 'O') countO++; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // Validate turn order |
| 32 | // O started the game (O has moves but X has none) |
| 33 | if (countO > countX && countX === 0) { |
| 34 | throw new Error('Wrong turn order: O started'); |
| 35 | } |
| 36 | |
| 37 | // X went twice in a row (X has more than one move more than O) |
| 38 | if (countX > countO + 1) { |
| 39 | throw new Error('Wrong turn order: X went twice'); |
| 40 | } |
| 41 | |
| 42 | // O went twice in a row (O has same or more moves than X when X should have played first) |
| 43 | if (countO > countX && countX > 0) { |
| 44 | throw new Error('Wrong turn order: O went twice'); |
| 45 | } |
| 46 | |
| 47 | // Check for wins |
| 48 | const checkWin = (player) => { |
| 49 | // Check rows |
| 50 | for (let i = 0; i < 3; i++) { |
| 51 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 52 | return true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Check columns |
| 57 | for (let j = 0; j < 3; j++) { |
| 58 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 59 | return true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Check diagonals |
| 64 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | return false; |
| 73 | }; |
| 74 | |
| 75 | const xWins = checkWin('X'); |
| 76 | const oWins = checkWin('O'); |
| 77 | |
| 78 | // Edge Case: Both players won (impossible in a valid game) |
| 79 | if (xWins && oWins) { |
| 80 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 81 | } |
| 82 | |
| 83 | // Edge Case: Game continued after X won |
| 84 | if (xWins && countO >= countX) { |
| 85 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 86 | } |
| 87 | |
| 88 | // Edge Case: Game continued after O won |
| 89 | if (oWins && countX > countO) { |
| 90 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 91 | } |
| 92 | |
| 93 | // Determine game state |
| 94 | if (xWins || oWins) { |
| 95 | return 'win'; |
| 96 | } |
| 97 | |
| 98 | // Edge Case: Board is full (draw) |
| 99 | if (countX + countO === 9) { |
| 100 | return 'draw'; |
| 101 | } |
| 102 | |
| 103 | // Edge Case: Game is not finished yet (ongoing) |
| 104 | return 'ongoing'; |
| 105 | |
| 106 | // Handled Edge Cases: |
| 107 | // 1. O started the game |
| 108 | // 2. X went twice in a row |
| 109 | // 3. Both players won |
| 110 | // 4. Game continued after X won |
| 111 | // 5. Game continued after O won |
| 112 | // 6. Board is full (draw) |
| 113 | // 7. Game is not finished yet (ongoing) |
| 114 | }; |