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