| 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 rows 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'); |
| 12 | | }; |
| 14 | // Validate board structure |
| 15 | if (!Array.isArray(board) || board.length !== 3) { |
| 16 | throw new Error('Invalid board: must have 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 length 3'); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Check that board only contains valid characters |
| 26 | const validChars = new Set(['X', 'O', ' ']); |
| 27 | for (let i = 0; i < 3; i++) { |
| 28 | for (let j = 0; j < 3; j++) { |
| 29 | if (!validChars.has(board[i][j])) { |
| 30 | throw new Error('Invalid board: contains invalid characters'); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Count Xs and Os |
| 36 | let xCount = 0; |
| 37 | let oCount = 0; |
| 38 | |
| 39 | for (let i = 0; i < 3; i++) { |
| 40 | for (let j = 0; j < 3; j++) { |
| 41 | if (board[i][j] === 'X') xCount++; |
| 42 | if (board[i][j] === 'O') oCount++; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Edge Case: Invalid turn order - O started the game |
| 47 | if (oCount > xCount) { |
| 48 | throw new Error('Wrong turn order: O started'); |
| 49 | } |
| 50 | |
| 51 | // Edge Case: Invalid turn order - X went twice in a row (more than one turn difference) |
| 52 | if (xCount > oCount + 1) { |
| 53 | throw new Error('Wrong turn order: X went twice'); |
| 54 | } |
| 55 | |
| 56 | // Check for wins |
| 57 | const winPatterns = [ |
| 58 | // Rows |
| 59 | [[0,0], [0,1], [0,2]], |
| 60 | [[1,0], [1,1], [1,2]], |
| 61 | [[2,0], [2,1], [2,2]], |
| 62 | // Columns |
| 63 | [[0,0], [1,0], [2,0]], |
| 64 | [[0,1], [1,1], [2,1]], |
| 65 | [[0,2], [1,2], [2,2]], |
| 66 | // Diagonals |
| 67 | [[0,0], [1,1], [2,2]], |
| 68 | [[0,2], [1,1], [2,0]] |
| 69 | ]; |
| 70 | |
| 71 | let xWins = false; |
| 72 | let oWins = false; |
| 73 | |
| 74 | for (const pattern of winPatterns) { |
| 75 | const [a, b, c] = pattern; |
| 76 | const [rowA, colA] = a; |
| 77 | const [rowB, colB] = b; |
| 78 | const [rowC, colC] = c; |
| 79 | |
| 80 | const cellA = board[rowA][colA]; |
| 81 | const cellB = board[rowB][colB]; |
| 82 | const cellC = board[rowC][colC]; |
| 83 | |
| 84 | if (cellA === 'X' && cellB === 'X' && cellC === 'X') { |
| 85 | xWins = true; |
| 86 | } |
| 87 | |
| 88 | if (cellA === 'O' && cellB === 'O' && cellC === 'O') { |
| 89 | oWins = true; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Edge Case: Game continued after it was won (both players can't win, and if one wins, the game should end) |
| 94 | if (xWins && oWins) { |
| 95 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 96 | } |
| 97 | |
| 98 | // Edge Case: Game continued after it was won (X wins but O continued) |
| 99 | if (xWins && oCount >= xCount) { |
| 100 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 101 | } |
| 102 | |
| 103 | // Edge Case: Game continued after it was won (O wins but X continued) |
| 104 | if (oWins && xCount > oCount) { |
| 105 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 106 | } |
| 107 | |
| 108 | // Determine game state |
| 109 | if (xWins || oWins) { |
| 110 | return 'win'; |
| 111 | } |
| 112 | |
| 113 | // If board is full, it's a draw |
| 114 | if (xCount + oCount === 9) { |
| 115 | return 'draw'; |
| 116 | } |
| 117 | |
| 118 | // Otherwise, game is ongoing |
| 119 | return 'ongoing'; |
| 120 | |
| 121 | // Handled Edge Cases: |
| 122 | // - Invalid turn order: O started the game |
| 123 | // - Invalid turn order: X went twice in a row |
| 124 | // - Game continued after it was won (X wins but O continued) |
| 125 | // - Game continued after it was won (O wins but X continued) |
| 126 | // - Game continued after it was won (both players win - impossible scenario) |
| 127 | // - Invalid board structure (not 3 rows, rows not length 3, invalid characters) |
| 128 | }; |