| 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 3x3 board. |
| 10 | * Each character is either 'X', 'O', or ' ' (empty space). |
| 11 | * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state. |
| 12 | * @throws {Error} - Throws an error for invalid board states with specific messages. |
| 9 | 13 | */ |
| 10 | 14 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | | }; |
| 15 | // Validate board structure |
| 16 | if (!Array.isArray(board) || board.length !== 3) { |
| 17 | throw new Error('Invalid board: must have 3 rows'); |
| 18 | } |
| 19 | |
| 20 | for (let i = 0; i < 3; i++) { |
| 21 | if (typeof board[i] !== 'string' || board[i].length !== 3) { |
| 22 | throw new Error('Invalid board: each row must be a string of length 3'); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // Count X's and O's |
| 27 | let xCount = 0; |
| 28 | let oCount = 0; |
| 29 | |
| 30 | for (let i = 0; i < 3; i++) { |
| 31 | for (let j = 0; j < 3; j++) { |
| 32 | const char = board[i][j]; |
| 33 | if (char === 'X') { |
| 34 | xCount++; |
| 35 | } else if (char === 'O') { |
| 36 | oCount++; |
| 37 | } else if (char !== ' ') { |
| 38 | throw new Error('Invalid board: contains invalid characters'); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Check for wins |
| 44 | const checkWin = (player) => { |
| 45 | // Check rows |
| 46 | for (let i = 0; i < 3; i++) { |
| 47 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Check columns |
| 53 | for (let j = 0; j < 3; j++) { |
| 54 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Check diagonals |
| 60 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | }; |
| 70 | |
| 71 | const xWins = checkWin('X'); |
| 72 | const oWins = checkWin('O'); |
| 73 | |
| 74 | // Edge Case: Both players have winning positions (continued playing after win) |
| 75 | if (xWins && oWins) { |
| 76 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 77 | } |
| 78 | |
| 79 | // Validate move counts for winning boards |
| 80 | if (xWins) { |
| 81 | // For X win, X should have played last, so xCount = oCount + 1 |
| 82 | // Exception: X can win with minimum moves (3 X's, 0 O's) |
| 83 | if (xCount === oCount + 1 || (xCount === 3 && oCount === 0)) { |
| 84 | return 'win'; |
| 85 | } |
| 86 | // If X has too many moves |
| 87 | else if (xCount > oCount + 1) { |
| 88 | throw new Error('Wrong turn order: X went twice'); |
| 89 | } |
| 90 | // If O played last (should not happen in valid X win) |
| 91 | else { |
| 92 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (oWins) { |
| 97 | // For O win, O should have played last, so oCount = xCount |
| 98 | if (oCount === xCount) { |
| 99 | return 'win'; |
| 100 | } |
| 101 | // If O has too many moves |
| 102 | else if (oCount > xCount && xCount > 0) { |
| 103 | throw new Error('Wrong turn order: O went twice'); |
| 104 | } |
| 105 | // If X played last (should not happen in valid O win) |
| 106 | else { |
| 107 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Check if O started the game (O played first) |
| 112 | let firstMove = null; |
| 113 | for (let i = 0; i < 3 && firstMove === null; i++) { |
| 114 | for (let j = 0; j < 3 && firstMove === null; j++) { |
| 115 | const char = board[i][j]; |
| 116 | if (char === 'X' || char === 'O') { |
| 117 | firstMove = char; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Edge Case: Invalid turn order - O started the game (O played first) |
| 123 | if (firstMove === 'O') { |
| 124 | throw new Error('Wrong turn order: O started'); |
| 125 | } |
| 126 | |
| 127 | // Edge Case: Invalid turn order - X went twice in a row (more than one turn difference) |
| 128 | if (xCount > oCount + 1) { |
| 129 | throw new Error('Wrong turn order: X went twice'); |
| 130 | } |
| 131 | |
| 132 | // Edge Case: Invalid turn order - O went twice in a row |
| 133 | if (oCount > xCount && xCount > 0) { |
| 134 | throw new Error('Wrong turn order: O went twice'); |
| 135 | } |
| 136 | |
| 137 | // Edge Case: Board is full with no winner (draw) |
| 138 | if (xCount + oCount === 9) { |
| 139 | return 'draw'; |
| 140 | } |
| 141 | |
| 142 | // Edge Case: Game is not finished (ongoing) |
| 143 | return 'ongoing'; |
| 144 | |
| 145 | // Handled Edge Cases: |
| 146 | // 1. Invalid turn order - O started the game |
| 147 | // 2. Invalid turn order - X went twice in a row |
| 148 | // 3. Both players have winning positions |
| 149 | // 4. X won but O played last |
| 150 | // 5. O won but X played last |
| 151 | // 6. Board is full with no winner |
| 152 | // 7. Game is not finished |
| 153 | }; |