| 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 one of: 'ongoing', 'draw', 'win' |
| 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 | for (let j = 0; j < 3; j++) { |
| 25 | const char = board[i][j]; |
| 26 | if (char !== 'X' && char !== 'O' && char !== ' ') { |
| 27 | throw new Error('Invalid board: contains invalid characters'); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Count Xs and Os |
| 33 | let countX = 0; |
| 34 | let countO = 0; |
| 35 | |
| 36 | for (let i = 0; i < 3; i++) { |
| 37 | for (let j = 0; j < 3; j++) { |
| 38 | if (board[i][j] === 'X') countX++; |
| 39 | if (board[i][j] === 'O') countO++; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Edge Case: Invalid turn order - O started the game |
| 44 | if (countO > countX) { |
| 45 | if (countX === 0) { |
| 46 | throw new Error('Wrong turn order: O started'); |
| 47 | } else { |
| 48 | throw new Error('Wrong turn order: O went twice'); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Edge Case: Invalid turn order - X went twice in a row (more than one move difference) |
| 53 | if (countX > countO + 1) { |
| 54 | throw new Error('Wrong turn order: X went twice'); |
| 55 | } |
| 56 | |
| 57 | // Check for wins |
| 58 | const wins = (player) => { |
| 59 | // Check rows |
| 60 | for (let i = 0; i < 3; i++) { |
| 61 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Check columns |
| 67 | for (let j = 0; j < 3; j++) { |
| 68 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Check diagonals |
| 74 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | }; |
| 84 | |
| 85 | const xWins = wins('X'); |
| 86 | const oWins = wins('O'); |
| 87 | |
| 88 | // Edge Case: Both players win (impossible in a valid game) |
| 89 | if (xWins && oWins) { |
| 90 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 91 | } |
| 92 | |
| 93 | // Edge Case: Game continued after a win |
| 94 | if (xWins && countX === countO) { |
| 95 | // X won but O made another move |
| 96 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 97 | } |
| 98 | |
| 99 | // Edge Case: Game continued after a win |
| 100 | if (oWins && countX > countO) { |
| 101 | // O won but X made another move |
| 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: Game is a draw (board is full with no winner) |
| 111 | if (countX + countO === 9) { |
| 112 | return 'draw'; |
| 113 | } |
| 114 | |
| 115 | // Edge Case: Game is ongoing (not won and not drawn) |
| 116 | return 'ongoing'; |
| 117 | |
| 118 | // Handled Edge Cases: |
| 119 | // - Invalid turn order: O started |
| 120 | // - Invalid turn order: X went twice |
| 121 | // - Both players win |
| 122 | // - Game continued after X win |
| 123 | // - Game continued after O win |
| 124 | // - Game is a draw |
| 125 | // - Game is ongoing |
| 126 | }; |