| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'State of Tic Tac Toe' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 5 | | |
| 6 | | /** |
| 7 | | * @param {string[]} board |
| 8 | | * @returns {string} |
| 1 | /* |
| 2 | * Determines the state of a Tic-Tac-Toe game based on the given board. |
| 3 | * |
| 4 | * @param {string[]} board - An array of 3 strings, each representing a row of the board. |
| 5 | * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state. |
| 6 | * @throws {Error} - Throws an error for invalid board states with specific messages. |
| 9 | 7 | */ |
| 10 | 8 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | | }; |
| 9 | // Validate board dimensions |
| 10 | if (board.length !== 3) { |
| 11 | throw new Error('Invalid board: must have 3 rows'); |
| 12 | } |
| 13 | |
| 14 | for (let row of board) { |
| 15 | if (row.length !== 3) { |
| 16 | throw new Error('Invalid board: each row must have 3 columns'); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // Count Xs and Os |
| 21 | let xCount = 0; |
| 22 | let oCount = 0; |
| 23 | |
| 24 | for (let row of board) { |
| 25 | for (let cell of row) { |
| 26 | if (cell === 'X') xCount++; |
| 27 | else if (cell === 'O') oCount++; |
| 28 | else if (cell !== ' ') { |
| 29 | throw new Error('Invalid board: contains invalid characters'); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Check for wins first to determine if turn order validation should apply |
| 35 | const win = (player) => { |
| 36 | // Check rows |
| 37 | for (let i = 0; i < 3; i++) { |
| 38 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 39 | return true; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Check columns |
| 44 | for (let i = 0; i < 3; i++) { |
| 45 | if (board[0][i] === player && board[1][i] === player && board[2][i] === player) { |
| 46 | return true; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Check diagonals |
| 51 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | return false; |
| 60 | }; |
| 61 | |
| 62 | const xWins = win('X'); |
| 63 | const oWins = win('O'); |
| 64 | |
| 65 | // Only validate turn order for non-winning, non-full boards |
| 66 | const isFullBoard = xCount + oCount === 9; |
| 67 | |
| 68 | if (!xWins && !oWins && !isFullBoard) { |
| 69 | // Edge Case: Invalid turn order - X went twice in a row (more than one extra move) |
| 70 | if (xCount > oCount + 1) { |
| 71 | throw new Error('Wrong turn order: X went twice'); |
| 72 | } |
| 73 | |
| 74 | // Edge Case: Invalid turn order - O started (no X's but O's exist) |
| 75 | if (oCount > 0 && xCount === 0) { |
| 76 | throw new Error('Wrong turn order: O started'); |
| 77 | } |
| 78 | |
| 79 | // Edge Case: Invalid turn order - O went twice in a row |
| 80 | if (oCount > xCount) { |
| 81 | throw new Error('Wrong turn order: O went twice'); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // xWins and oWins are already defined above |
| 86 | |
| 87 | // Edge Case: Both players win (impossible in a real game) |
| 88 | if (xWins && oWins) { |
| 89 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 90 | } |
| 91 | |
| 92 | // Edge Case: Game continued after win |
| 93 | if (xWins && xCount <= oCount) { |
| 94 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 95 | } |
| 96 | |
| 97 | // Edge Case: Game continued after win |
| 98 | if (oWins && xCount > oCount) { |
| 99 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 100 | } |
| 101 | |
| 102 | // Determine game state |
| 103 | if (xWins || oWins) { |
| 104 | return 'win'; |
| 105 | } |
| 106 | |
| 107 | // Edge Case: Board is full (draw) |
| 108 | if (xCount + oCount === 9) { |
| 109 | return 'draw'; |
| 110 | } |
| 111 | |
| 112 | // Edge Case: Game is not finished yet |
| 113 | return 'ongoing'; |
| 114 | |
| 115 | // Handled Edge Cases: |
| 116 | // 1. Invalid turn order - O started |
| 117 | // 2. Invalid turn order - X went twice in a row |
| 118 | // 3. Both players win (impossible in a real game) |
| 119 | // 4. Game continued after X win |
| 120 | // 5. Game continued after O win |
| 121 | // 6. Board is full (draw) |
| 122 | // 7. Game is not finished yet |
| 123 | // 8. Invalid board dimensions |
| 124 | // 9. Invalid characters on board |
| 125 | }; |