| 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 dimensions |
| 12 | if (board.length !== 3) { |
| 13 | throw new Error('Invalid board: must have 3 rows'); |
| 14 | } |
| 15 | |
| 16 | for (let row of board) { |
| 17 | if (row.length !== 3) { |
| 18 | throw new Error('Invalid board: each row must have 3 columns'); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Count Xs and Os |
| 23 | let countX = 0; |
| 24 | let countO = 0; |
| 25 | |
| 26 | for (let row of board) { |
| 27 | for (let cell of row) { |
| 28 | if (cell === 'X') countX++; |
| 29 | else if (cell === 'O') countO++; |
| 30 | else if (cell !== ' ') { |
| 31 | throw new Error('Invalid board: contains invalid characters'); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Check for wins first |
| 37 | const winX = checkWin(board, 'X'); |
| 38 | const winO = checkWin(board, 'O'); |
| 39 | |
| 40 | // Edge Case: Both players win (impossible) |
| 41 | if (winX && winO) { |
| 42 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 43 | } |
| 44 | |
| 45 | // If there's a winner, return win (don't check turn order for win scenarios) |
| 46 | if (winX || winO) { |
| 47 | return 'win'; |
| 48 | } |
| 49 | |
| 50 | // Only check turn order for ongoing games |
| 51 | // Edge Case: Wrong turn order - O started |
| 52 | if (countO > countX && countX === 0) { |
| 53 | throw new Error('Wrong turn order: O started'); |
| 54 | } |
| 55 | |
| 56 | // Edge Case: Wrong turn order - O went twice in a row |
| 57 | if (countO > countX && countX > 0) { |
| 58 | throw new Error('Wrong turn order: O went twice'); |
| 59 | } |
| 60 | |
| 61 | // Edge Case: Wrong turn order - X went twice in a row |
| 62 | if (countX > countO + 1) { |
| 63 | throw new Error('Wrong turn order: X went twice'); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | // Edge Case: Board is full with no winner (draw) |
| 69 | if (countX + countO === 9) { |
| 70 | return 'draw'; |
| 71 | } |
| 72 | |
| 73 | // Edge Case: Game is not finished yet (ongoing) |
| 74 | return 'ongoing'; |
| 12 | 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Helper function to check if a player has won |
| 79 | * @param {string[]} board |
| 80 | * @param {string} player |
| 81 | * @returns {boolean} |
| 82 | */ |
| 83 | const checkWin = (board, player) => { |
| 84 | // Check rows |
| 85 | for (let i = 0; i < 3; i++) { |
| 86 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Check columns |
| 92 | for (let i = 0; i < 3; i++) { |
| 93 | if (board[0][i] === player && board[1][i] === player && board[2][i] === player) { |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Check diagonals |
| 99 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | return false; |
| 108 | }; |
| 109 | |
| 110 | // Handled Edge Cases: |
| 111 | // 1. Invalid board dimensions |
| 112 | // 2. Invalid characters on board |
| 113 | // 3. Wrong turn order - O started |
| 114 | // 4. Wrong turn order - X went twice in a row |
| 115 | // 5. Both players win (impossible) |
| 116 | // 6. Game continued after X win |
| 117 | // 7. Game continued after O win |
| 118 | // 8. Board is full with no winner (draw) |
| 119 | // 9. Game is not finished yet (ongoing) |