| 8 | 8 | * @returns {string} |
| 9 | 9 | */ |
| 10 | 10 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 11 | // Validate board structure |
| 12 | if (!board || 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] || board[i].length !== 3) { |
| 18 | throw new Error('Invalid board: each row must have 3 columns'); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Count X's and O's |
| 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 |
| 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 | // Edge Case: Both players won (impossible) |
| 71 | if (xWins && oWins) { |
| 72 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 73 | } |
| 74 | |
| 75 | // Edge Case: Game continued after X won |
| 76 | if (xWins && oCount >= xCount) { |
| 77 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 78 | } |
| 79 | |
| 80 | // Edge Case: Game continued after O won |
| 81 | if (oWins && xCount > oCount) { |
| 82 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 83 | } |
| 84 | |
| 85 | // Edge Case: Invalid turn order - O started (only check if no one won) |
| 86 | if (!xWins && !oWins && xCount < oCount) { |
| 87 | throw new Error('Wrong turn order: O started'); |
| 88 | } |
| 89 | |
| 90 | // Edge Case: Invalid turn order - X went twice more than O |
| 91 | if (xCount > oCount + 1) { |
| 92 | throw new Error('Wrong turn order: X went twice'); |
| 93 | } |
| 94 | |
| 95 | // Determine game state |
| 96 | if (xWins || oWins) { |
| 97 | return 'win'; |
| 98 | } |
| 99 | |
| 100 | // Edge Case: Board is full (draw) |
| 101 | if (xCount + oCount === 9) { |
| 102 | return 'draw'; |
| 103 | } |
| 104 | |
| 105 | // Edge Case: Game is ongoing |
| 106 | return 'ongoing'; |
| 107 | |
| 108 | // Handled Edge Cases: |
| 109 | // - Invalid turn order (O starts) |
| 110 | // - Invalid turn order (X plays twice consecutively) |
| 111 | // - Game continues after win condition |
| 112 | // - Both players win (impossible state) |
| 113 | // - Board is full (draw) |
| 114 | // - Game is ongoing |
| 115 | // - Invalid board dimensions |
| 116 | // - Invalid characters on board |
| 12 | 117 | }; |