| 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 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'); |
| 14 | // Validate board structure |
| 15 | if (!board || board.length !== 3) { |
| 16 | throw new Error('Invalid board: must have exactly 3 rows'); |
| 17 | } |
| 18 | |
| 19 | for (let i = 0; i < 3; i++) { |
| 20 | if (!board[i] || board[i].length !== 3) { |
| 21 | throw new Error('Invalid board: each row must have exactly 3 columns'); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Count Xs and Os |
| 26 | let countX = 0; |
| 27 | let countO = 0; |
| 28 | |
| 29 | for (let i = 0; i < 3; i++) { |
| 30 | for (let j = 0; j < 3; j++) { |
| 31 | const cell = board[i][j]; |
| 32 | if (cell === 'X') { |
| 33 | countX++; |
| 34 | } else if (cell === 'O') { |
| 35 | countO++; |
| 36 | } else if (cell !== ' ') { |
| 37 | throw new Error('Invalid board: contains invalid characters'); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Edge Case: Invalid turn order - X went twice in a row |
| 43 | // This happens when X has more than one move advantage over O |
| 44 | if (countX > countO + 1) { |
| 45 | throw new Error('Wrong turn order: X went twice'); |
| 46 | } |
| 47 | |
| 48 | // Edge Case: Too many X moves in total |
| 49 | // X can have at most 5 moves in a valid game |
| 50 | if (countX > 5) { |
| 51 | throw new Error('Wrong turn order: X went twice'); |
| 52 | } |
| 53 | |
| 54 | // Edge Case: Too many O moves in total |
| 55 | // O can have at most 4 moves in a valid game |
| 56 | if (countO > 4) { |
| 57 | throw new Error('Wrong turn order: O started'); |
| 58 | } |
| 59 | |
| 60 | // Edge Case: Invalid turn order - O went twice in a row or O started |
| 61 | if (countO > countX) { |
| 62 | // Need to distinguish between "O started" and "O went twice" |
| 63 | // If total moves = 1 and O = 1, then O started |
| 64 | // If total moves = 3 and X = 1, O = 2, then O went twice |
| 65 | const totalMoves = countX + countO; |
| 66 | |
| 67 | if (totalMoves === 1 && countO === 1) { |
| 68 | throw new Error('Wrong turn order: O started'); |
| 69 | } else if (totalMoves === 3 && countX === 1 && countO === 2) { |
| 70 | throw new Error('Wrong turn order: O went twice'); |
| 71 | } else if (countO > countX) { |
| 72 | // General case - O has more moves than allowed |
| 73 | throw new Error('Wrong turn order: O started'); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Check for wins |
| 78 | const winForX = checkWin(board, 'X'); |
| 79 | const winForO = checkWin(board, 'O'); |
| 80 | |
| 81 | // Edge Case: Both players won (impossible) |
| 82 | if (winForX && winForO) { |
| 83 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 84 | } |
| 85 | |
| 86 | // Edge Case: Game continued after win |
| 87 | // When X wins, O should not have played after X won (countO < countX) |
| 88 | // Exception: If the board is full and X just won on the last move, countO could equal countX |
| 89 | if (winForX && countO > countX) { |
| 90 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 91 | } |
| 92 | |
| 93 | // Edge Case: Game continued after win |
| 94 | // When O wins, X should not have played after O won (countX <= countO) |
| 95 | if (winForO && countX > countO) { |
| 96 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 97 | } |
| 98 | |
| 99 | // Determine game state |
| 100 | if (winForX || winForO) { |
| 101 | return 'win'; |
| 102 | } |
| 103 | |
| 104 | // Edge Case: Board is full (draw) |
| 105 | if (countX + countO === 9) { |
| 106 | return 'draw'; |
| 107 | } |
| 108 | |
| 109 | // Edge Case: Game is not finished yet (ongoing) |
| 110 | return 'ongoing'; |
| 111 | |
| 112 | // Handled Edge Cases: Invalid turn order (O started), Invalid turn order (X went twice), |
| 113 | // Both players won, Game continued after win, Board is full, Game is not finished yet |
| 12 | 114 | }; |
| 115 | |
| 116 | /** |
| 117 | * Checks if the specified player has won on the board. |
| 118 | * |
| 119 | * @param {string[]} board - The game board |
| 120 | * @param {string} player - The player to check for win ('X' or 'O') |
| 121 | * @returns {boolean} - True if the player has won, false otherwise |
| 122 | */ |
| 123 | function checkWin(board, player) { |
| 124 | // Check rows |
| 125 | for (let i = 0; i < 3; i++) { |
| 126 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Check columns |
| 132 | for (let j = 0; j < 3; j++) { |
| 133 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Check diagonals |
| 139 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | return false; |
| 148 | |
| 149 | // Handled Edge Cases: All possible win conditions (rows, columns, diagonals) |
| 150 | } |