| 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'); |
| 15 | // Validate board structure |
| 16 | if (!Array.isArray(board) || board.length !== 3) { |
| 17 | throw new Error('Invalid board: must have exactly 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 exactly 3 characters'); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // Count Xs and Os |
| 27 | let countX = 0; |
| 28 | let countO = 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 | countX++; |
| 35 | } else if (char === 'O') { |
| 36 | countO++; |
| 37 | } else if (char !== ' ') { |
| 38 | throw new Error('Invalid board: contains invalid characters'); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Check for wins |
| 44 | const winForX = checkWin(board, 'X'); |
| 45 | const winForO = checkWin(board, 'O'); |
| 46 | |
| 47 | // Edge Case: Both players won (impossible in a valid game) |
| 48 | if (winForX && winForO) { |
| 49 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 50 | } |
| 51 | |
| 52 | // Edge Case: Game continued after a win |
| 53 | if (winForX && countO >= countX) { |
| 54 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 55 | } |
| 56 | |
| 57 | // Edge Case: Game continued after a win |
| 58 | if (winForO && countX > countO) { |
| 59 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 60 | } |
| 61 | |
| 62 | // If there's a win, return 'win' without checking turn order |
| 63 | if (winForX || winForO) { |
| 64 | return 'win'; |
| 65 | } |
| 66 | |
| 67 | // Edge Case: Invalid turn order - X went twice in a row |
| 68 | if (countX > countO + 1) { |
| 69 | throw new Error('Wrong turn order: X went twice'); |
| 70 | } |
| 71 | |
| 72 | // Edge Case: Invalid turn order - O went twice or O started |
| 73 | if (countO > countX) { |
| 74 | if (countX === 0 && countO === 1) { |
| 75 | throw new Error('Wrong turn order: O started'); |
| 76 | } |
| 77 | throw new Error('Wrong turn order: O went twice'); |
| 78 | } |
| 79 | |
| 80 | // Edge Case: Board is full (draw) |
| 81 | if (countX + countO === 9) { |
| 82 | return 'draw'; |
| 83 | } |
| 84 | |
| 85 | // Edge Case: Game is not finished yet |
| 86 | return 'ongoing'; |
| 87 | |
| 88 | // Handled Edge Cases: Invalid turn order (O started, X went twice), both players won, |
| 89 | // game continued after a win, board is full (draw), game not finished |
| 12 | 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * Checks if the given player has won on the board. |
| 94 | * |
| 95 | * @param {string[]} board - The game board. |
| 96 | * @param {string} player - The player to check for a win ('X' or 'O'). |
| 97 | * @returns {boolean} - True if the player has won, false otherwise. |
| 98 | */ |
| 99 | function checkWin(board, player) { |
| 100 | // Check rows |
| 101 | for (let i = 0; i < 3; i++) { |
| 102 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Check columns |
| 108 | for (let j = 0; j < 3; j++) { |
| 109 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Check diagonals |
| 115 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | return false; |
| 124 | // Handled Edge Cases: All possible winning combinations checked |
| 125 | } |