| 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'); |
| 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 | } |
| 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 | // Validate turn order |
| 44 | if (countO > countX) { |
| 45 | if (countX === 0) { |
| 46 | throw new Error('Wrong turn order: O started'); |
| 47 | } else { |
| 48 | throw new Error('Wrong turn order: O went twice'); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (countX > countO + 1) { |
| 53 | throw new Error('Wrong turn order: X went twice'); |
| 54 | } |
| 55 | |
| 56 | // Check for wins |
| 57 | const winForX = checkWin(board, 'X'); |
| 58 | const winForO = checkWin(board, 'O'); |
| 59 | |
| 60 | // Edge Case: Both players won (impossible) |
| 61 | if (winForX && winForO) { |
| 62 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 63 | } |
| 64 | |
| 65 | // Edge Case: Game continued after win |
| 66 | if (winForX && countO >= countX) { |
| 67 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 68 | } |
| 69 | |
| 70 | // Edge Case: Game continued after win |
| 71 | if (winForO && countX > countO) { |
| 72 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 73 | } |
| 74 | |
| 75 | // Determine game state |
| 76 | if (winForX || winForO) { |
| 77 | return 'win'; |
| 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 board structure, invalid characters, O started, X went twice, |
| 89 | // both players won, game continued after win, board is full, 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 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 | |
| 125 | // Handled Edge Cases: All possible win conditions checked |
| 126 | } |