| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export const gamestate = (board) => { |
| 7 | | throw new Error('Remove this line and implement the function'); |
| 7 | // Edge Case: Board with incorrect dimensions |
| 8 | if (board.length !== 3 || board.some(row => row.length !== 3)) { |
| 9 | throw new Error('Invalid board dimensions'); |
| 10 | } |
| 11 | |
| 12 | // Edge Case: Board with invalid characters |
| 13 | const validChars = ['X', 'O', ' ']; |
| 14 | for (let i = 0; i < 3; i++) { |
| 15 | for (let j = 0; j < 3; j++) { |
| 16 | if (!validChars.includes(board[i][j])) { |
| 17 | throw new Error('Invalid character on board'); |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // Count Xs and Os |
| 23 | let xCount = 0; |
| 24 | let oCount = 0; |
| 25 | for (let i = 0; i < 3; i++) { |
| 26 | for (let j = 0; j < 3; j++) { |
| 27 | if (board[i][j] === 'X') xCount++; |
| 28 | if (board[i][j] === 'O') oCount++; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Edge Case: Wrong turn order (O starts or count mismatch) |
| 33 | if (oCount > xCount) { |
| 34 | throw new Error('Wrong turn order: O started'); |
| 35 | } |
| 36 | if (xCount > oCount + 1) { |
| 37 | throw new Error('Wrong turn order: X went twice'); |
| 38 | } |
| 39 | |
| 40 | // Check for wins |
| 41 | const checkWin = (player) => { |
| 42 | // Check rows |
| 43 | for (let i = 0; i < 3; i++) { |
| 44 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 45 | return true; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Check columns |
| 50 | for (let j = 0; j < 3; j++) { |
| 51 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 52 | return true; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Check diagonals |
| 57 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 58 | return true; |
| 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: Game was played after it already ended |
| 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 was played after it already ended |
| 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 was played after it already ended |
| 81 | if (oWins && xCount > oCount) { |
| 82 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 83 | } |
| 84 | |
| 85 | // Determine game state |
| 86 | if (xWins || oWins) { |
| 87 | return 'win'; |
| 88 | } |
| 89 | |
| 90 | if (xCount + oCount === 9) { |
| 91 | return 'draw'; |
| 92 | } |
| 93 | |
| 94 | return 'ongoing'; |
| 8 | 95 | }; |
| 96 | |
| 97 | // Handled Edge Cases: Board with incorrect dimensions, Board with invalid characters, Wrong turn order (O starts or count mismatch), Game was played after it already ended |