| 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 with specific messages for invalid board states. |
| 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 | 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 | // Edge Case: Invalid turn order - O started the game |
| 44 | if (countO > countX) { |
| 45 | throw new Error('Wrong turn order: O started'); |
| 46 | } |
| 47 | |
| 48 | // Edge Case: Invalid turn order - X went twice in a row |
| 49 | if (countX > countO + 1) { |
| 50 | throw new Error('Wrong turn order: X went twice'); |
| 51 | } |
| 52 | |
| 53 | // Check for wins |
| 54 | const wins = (player) => { |
| 55 | // Check rows |
| 56 | for (let i = 0; i < 3; i++) { |
| 57 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Check columns |
| 63 | for (let j = 0; j < 3; j++) { |
| 64 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Check diagonals |
| 70 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | }; |
| 80 | |
| 81 | const xWins = wins('X'); |
| 82 | const oWins = wins('O'); |
| 83 | |
| 84 | // Edge Case: Impossible board - game continued after win |
| 85 | if (xWins && oWins) { |
| 86 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 87 | } |
| 88 | |
| 89 | // Edge Case: Impossible board - game continued after win (X won but O played next) |
| 90 | if (xWins && countO >= countX) { |
| 91 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 92 | } |
| 93 | |
| 94 | // Edge Case: Impossible board - game continued after win (O won but X played next) |
| 95 | if (oWins && countX > countO) { |
| 96 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 97 | } |
| 98 | |
| 99 | // Determine game state |
| 100 | if (xWins || oWins) { |
| 101 | return 'win'; |
| 102 | } |
| 103 | |
| 104 | // Edge Case: Game is a draw (board is full with no winner) |
| 105 | if (countX + countO === 9) { |
| 106 | return 'draw'; |
| 107 | } |
| 108 | |
| 109 | // Edge Case: Game is ongoing (not full and no winner) |
| 110 | return 'ongoing'; |
| 111 | |
| 112 | // Handled Edge Cases: |
| 113 | // - Invalid turn order (O started) |
| 114 | // - Invalid turn order (X went twice) |
| 115 | // - Impossible board (game continued after win - both players won) |
| 116 | // - Impossible board (game continued after win - X won but O played next) |
| 117 | // - Impossible board (game continued after win - O won but X played next) |
| 118 | // - Game is a draw |
| 119 | // - Game is ongoing |
| 12 | 120 | }; |