| 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 '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'); |
| 12 | | }; |
| 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 cell = board[i][j]; |
| 33 | if (cell === 'X') { |
| 34 | countX++; |
| 35 | } else if (cell === 'O') { |
| 36 | countO++; |
| 37 | } else if (cell !== ' ') { |
| 38 | throw new Error('Invalid board: contains invalid characters'); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Check for wins |
| 44 | const wins = (player) => { |
| 45 | // Check rows |
| 46 | for (let i = 0; i < 3; i++) { |
| 47 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Check columns |
| 53 | for (let j = 0; j < 3; j++) { |
| 54 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Check diagonals |
| 60 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | }; |
| 70 | |
| 71 | const xWins = wins('X'); |
| 72 | const oWins = wins('O'); |
| 73 | |
| 74 | // If there are wins, check win validity first |
| 75 | if (xWins || oWins) { |
| 76 | // Edge Case: Both players win (impossible in a valid game) |
| 77 | if (xWins && oWins) { |
| 78 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 79 | } |
| 80 | |
| 81 | // Edge Case: Game continued after win |
| 82 | if (xWins && countX === countO) { |
| 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 | if (oWins && countX > countO) { |
| 88 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 89 | } |
| 90 | |
| 91 | // If win is valid, return win |
| 92 | return 'win'; |
| 93 | } |
| 94 | |
| 95 | // If no wins, validate turn order for ongoing games |
| 96 | // Edge Case: Invalid turn order - X went twice in a row |
| 97 | if (countX > countO + 1) { |
| 98 | throw new Error('Wrong turn order: X went twice'); |
| 99 | } |
| 100 | |
| 101 | // Edge Case: Invalid turn order - O started or O went twice |
| 102 | if (countO > countX) { |
| 103 | if (countX === 0) { |
| 104 | throw new Error('Wrong turn order: O started'); |
| 105 | } else { |
| 106 | throw new Error('Wrong turn order: O went twice'); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Edge Case: Board is full (draw) |
| 111 | if (countX + countO === 9) { |
| 112 | return 'draw'; |
| 113 | } |
| 114 | |
| 115 | // Edge Case: Game is not finished yet |
| 116 | return 'ongoing'; |
| 117 | |
| 118 | // Handled Edge Cases: |
| 119 | // - Invalid board structure |
| 120 | // - Invalid characters on board |
| 121 | // - O started the game |
| 122 | // - X went twice in a row |
| 123 | // - Both players win (impossible) |
| 124 | // - Game continued after X win |
| 125 | // - Game continued after O win |
| 126 | // - Board is full (draw) |
| 127 | // - Game is not finished yet |
| 128 | }; |