| 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 3x3 game board. |
| 10 | * Each string has 3 characters: 'X', 'O', or ' '. |
| 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 be an array of 3 strings'); |
| 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 xCount = 0; |
| 34 | let oCount = 0; |
| 35 | |
| 36 | for (let i = 0; i < 3; i++) { |
| 37 | for (let j = 0; j < 3; j++) { |
| 38 | if (board[i][j] === 'X') xCount++; |
| 39 | if (board[i][j] === 'O') oCount++; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Edge Case: Invalid turn order - O started the game |
| 44 | if (oCount > xCount && xCount === 0) { |
| 45 | throw new Error('Wrong turn order: O started'); |
| 46 | } |
| 47 | |
| 48 | // Edge Case: Invalid turn order - Player went twice consecutively |
| 49 | if (xCount > oCount + 1) { |
| 50 | throw new Error('Wrong turn order: X went twice'); |
| 51 | } |
| 52 | |
| 53 | // Edge Case: Invalid turn order - O went twice consecutively |
| 54 | if (oCount > xCount + 1) { |
| 55 | throw new Error('Wrong turn order: O went twice'); |
| 56 | } |
| 57 | |
| 58 | // Check for wins |
| 59 | const wins = (player) => { |
| 60 | // Check rows |
| 61 | for (let i = 0; i < 3; i++) { |
| 62 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 63 | return true; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Check columns |
| 68 | for (let j = 0; j < 3; j++) { |
| 69 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Check diagonals |
| 75 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | return false; |
| 84 | }; |
| 85 | |
| 86 | const xWins = wins('X'); |
| 87 | const oWins = wins('O'); |
| 88 | |
| 89 | // Edge Case: Game continued after a win |
| 90 | if (xWins && oCount >= xCount) { |
| 91 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 92 | } |
| 93 | |
| 94 | // Edge Case: Game continued after a win |
| 95 | if (oWins && xCount > oCount + 1) { |
| 96 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 97 | } |
| 98 | |
| 99 | // Edge Case: Both players win (impossible in a valid game) |
| 100 | // This should only happen in invalid boards, but we've already checked for continued play |
| 101 | // So if both win, it's still an impossible board |
| 102 | if (xWins && oWins) { |
| 103 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 104 | } |
| 105 | |
| 106 | // Determine game state |
| 107 | if (xWins || oWins) { |
| 108 | return 'win'; |
| 109 | } |
| 110 | |
| 111 | // Edge Case: Board is full with no winner (draw) |
| 112 | if (xCount + oCount === 9) { |
| 113 | return 'draw'; |
| 114 | } |
| 115 | |
| 116 | // Edge Case: Game is not finished yet (ongoing) |
| 117 | return 'ongoing'; |
| 118 | |
| 119 | // Handled Edge Cases: |
| 120 | // - Invalid board structure |
| 121 | // - Invalid characters on board |
| 122 | // - O starting the game |
| 123 | // - Player going twice consecutively |
| 124 | // - Both players winning |
| 125 | // - Game continuing after a win |
| 126 | // - Board full with no winner (draw) |
| 127 | // - Game not finished yet (ongoing) |
| 12 | 128 | }; |