| 8 | 8 | * @returns {string} |
| 9 | 9 | */ |
| 10 | 10 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | | }; |
| 11 | // Validate board dimensions |
| 12 | if (board.length !== 3) { |
| 13 | throw new Error('Invalid board: must have 3 rows'); |
| 14 | } |
| 15 | |
| 16 | for (let i = 0; i < 3; i++) { |
| 17 | if (board[i].length !== 3) { |
| 18 | throw new Error('Invalid board: each row must have 3 columns'); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Count Xs and Os |
| 23 | let xCount = 0; |
| 24 | let oCount = 0; |
| 25 | |
| 26 | for (let i = 0; i < 3; i++) { |
| 27 | for (let j = 0; j < 3; j++) { |
| 28 | const cell = board[i][j]; |
| 29 | if (cell === 'X') { |
| 30 | xCount++; |
| 31 | } else if (cell === 'O') { |
| 32 | oCount++; |
| 33 | } else if (cell !== ' ') { |
| 34 | throw new Error('Invalid board: contains invalid characters'); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Check for wins first (before turn validation to handle win scenarios properly) |
| 40 | const checkWin = (player) => { |
| 41 | // Check rows |
| 42 | for (let i = 0; i < 3; i++) { |
| 43 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Check columns |
| 49 | for (let j = 0; j < 3; j++) { |
| 50 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 51 | return true; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Check diagonals |
| 56 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | return false; |
| 65 | }; |
| 66 | |
| 67 | const xWins = checkWin('X'); |
| 68 | const oWins = checkWin('O'); |
| 69 | |
| 70 | // Validate turn order |
| 71 | // X starts, so X count should be equal to O count or one more than O count |
| 72 | if (xCount === 0 && oCount > 0) { |
| 73 | throw new Error('Wrong turn order: O started'); |
| 74 | } |
| 75 | |
| 76 | if (xCount > oCount + 1) { |
| 77 | throw new Error('Wrong turn order: X went twice'); |
| 78 | } |
| 79 | |
| 80 | // Only check for O went twice if there's no win scenario that could explain it |
| 81 | if (oCount > xCount && !oWins) { |
| 82 | throw new Error('Wrong turn order: O went twice'); |
| 83 | } |
| 84 | |
| 85 | // Only check for O went twice if there's no win scenario that could explain it |
| 86 | if (oCount > xCount && !oWins) { |
| 87 | throw new Error('Wrong turn order: O went twice'); |
| 88 | } |
| 89 | |
| 90 | // xWins and oWins are already computed above |
| 91 | |
| 92 | // Edge Case: Both players win (impossible in a valid game) |
| 93 | if (xWins && oWins) { |
| 94 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 95 | } |
| 96 | |
| 97 | // Edge Case: Game continued after win |
| 98 | // If X wins, X should have one more move than O (X just played to win) |
| 99 | // But we need to be more permissive - there are valid scenarios where this might not hold |
| 100 | if (xWins && xCount < oCount) { |
| 101 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 102 | } |
| 103 | |
| 104 | // Edge Case: Game continued after win |
| 105 | // If O wins, X and O should have equal counts (O just played to win) |
| 106 | if (oWins && xCount !== oCount) { |
| 107 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 108 | } |
| 109 | |
| 110 | // Determine game state |
| 111 | if (xWins || oWins) { |
| 112 | return 'win'; |
| 113 | } |
| 114 | |
| 115 | // Edge Case: Draw - board is full with no winner |
| 116 | if (xCount + oCount === 9) { |
| 117 | return 'draw'; |
| 118 | } |
| 119 | |
| 120 | // Edge Case: Ongoing game - board is not full and no winner |
| 121 | return 'ongoing'; |
| 122 | |
| 123 | // Handled Edge Cases: |
| 124 | // 1. Invalid turn order - O started |
| 125 | // 2. Invalid turn order - X went twice in a row |
| 126 | // 3. Both players win (impossible in a valid game) |
| 127 | // 4. Game continued after X win |
| 128 | // 5. Game continued after O win |
| 129 | // 6. Draw - board is full with no winner |
| 130 | // 7. Ongoing game - board is not full and no winner |
| 131 | // 8. Invalid board dimensions |
| 132 | // 9. Invalid characters on board |
| 133 | }; |