| 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 if the board is invalid. |
| 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 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 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 countX = 0; |
| 34 | let countO = 0; |
| 35 | |
| 36 | for (let i = 0; i < 3; i++) { |
| 37 | for (let j = 0; j < 3; j++) { |
| 38 | if (board[i][j] === 'X') countX++; |
| 39 | if (board[i][j] === 'O') countO++; |
| 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 | // Edge Case: Both players win (impossible in a valid game) |
| 75 | if (xWins && oWins) { |
| 76 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 77 | } |
| 78 | |
| 79 | // Check for wins first |
| 80 | if (xWins || oWins) { |
| 81 | // For winning boards, check if the turn counts are valid for the win |
| 82 | if (xWins) { |
| 83 | // X wins - X should have made the last move |
| 84 | if (countX === countO) { |
| 85 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 86 | } |
| 87 | // Check if there are too many X moves |
| 88 | if (countX > countO + 1) { |
| 89 | throw new Error('Wrong turn order: X went twice'); |
| 90 | } |
| 91 | } else if (oWins) { |
| 92 | // O wins - O should have made the last move |
| 93 | if (countX > countO) { |
| 94 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 95 | } |
| 96 | // Check if there are too many O moves |
| 97 | if (countO > countX) { |
| 98 | throw new Error('Wrong turn order: O went twice'); |
| 99 | } |
| 100 | } |
| 101 | return 'win'; |
| 102 | } |
| 103 | |
| 104 | // For non-winning boards, validate turn order |
| 105 | // Edge Case: Invalid turn order - O started |
| 106 | if (countO > 0 && countX === 0) { |
| 107 | throw new Error('Wrong turn order: O started'); |
| 108 | } |
| 109 | |
| 110 | // Edge Case: Invalid turn order - X went twice in a row |
| 111 | if (countX > countO + 1) { |
| 112 | throw new Error('Wrong turn order: X went twice'); |
| 113 | } |
| 114 | |
| 115 | // Edge Case: Invalid turn order - O went twice in a row |
| 116 | if (countO > countX) { |
| 117 | throw new Error('Wrong turn order: O went twice'); |
| 118 | } |
| 119 | |
| 120 | // Edge Case: Draw - board is full with no winner |
| 121 | if (countX + countO === 9) { |
| 122 | return 'draw'; |
| 123 | } |
| 124 | |
| 125 | // Edge Case: Ongoing game - board is not full and no winner |
| 126 | return 'ongoing'; |
| 127 | |
| 128 | // Determine game state |
| 129 | if (xWins || oWins) { |
| 130 | return 'win'; |
| 131 | } |
| 132 | |
| 133 | // Edge Case: Draw - board is full with no winner |
| 134 | if (countX + countO === 9) { |
| 135 | return 'draw'; |
| 136 | } |
| 137 | |
| 138 | // Edge Case: Ongoing game - board is not full and no winner |
| 139 | return 'ongoing'; |
| 140 | |
| 141 | // Handled Edge Cases: |
| 142 | // - Invalid board structure |
| 143 | // - Invalid characters on board |
| 144 | // - Wrong turn order (O started) |
| 145 | // - Wrong turn order (X went twice) |
| 146 | // - Both players winning |
| 147 | // - Game continued after X win |
| 148 | // - Game continued after O win |
| 149 | // - Draw state |
| 150 | // - Ongoing game state |
| 151 | }; |