| 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 X's and O's |
| 23 | let xCount = 0; |
| 24 | let oCount = 0; |
| 25 | |
| 26 | for (let row of board) { |
| 27 | for (let cell of row) { |
| 28 | if (cell === 'X') xCount++; |
| 29 | else if (cell === 'O') oCount++; |
| 30 | else if (cell !== ' ') { |
| 31 | throw new Error('Invalid board: contains invalid characters'); |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Check for wins first |
| 37 | const checkWin = (player) => { |
| 38 | // Check rows |
| 39 | for (let i = 0; i < 3; i++) { |
| 40 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 41 | return true; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Check columns |
| 46 | for (let i = 0; i < 3; i++) { |
| 47 | if (board[0][i] === player && board[1][i] === player && board[2][i] === player) { |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Check diagonals |
| 53 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | return false; |
| 62 | }; |
| 63 | |
| 64 | const xWins = checkWin('X'); |
| 65 | const oWins = checkWin('O'); |
| 66 | |
| 67 | // Now validate turn order based on win conditions |
| 68 | if (xWins && oWins) { |
| 69 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 70 | } |
| 71 | |
| 72 | if (xWins) { |
| 73 | // If X wins, X should have exactly one more than O |
| 74 | if (xCount !== oCount + 1) { |
| 75 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 76 | } |
| 77 | return 'win'; |
| 78 | } |
| 79 | |
| 80 | if (oWins) { |
| 81 | // If O wins, counts should be equal |
| 82 | if (xCount !== oCount) { |
| 83 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 84 | } |
| 85 | return 'win'; |
| 86 | } |
| 87 | |
| 88 | // If no wins, check turn order |
| 89 | // Validate turn order |
| 90 | if (xCount === 0 && oCount > 0) { |
| 91 | throw new Error('Wrong turn order: O started'); |
| 92 | } |
| 93 | |
| 94 | if (xCount > oCount + 1) { |
| 95 | throw new Error('Wrong turn order: X went twice'); |
| 96 | } |
| 97 | |
| 98 | if (oCount > xCount) { |
| 99 | throw new Error('Wrong turn order: O went twice'); |
| 100 | } |
| 101 | |
| 102 | // Determine game state |
| 103 | if (xWins) { |
| 104 | return 'win'; |
| 105 | } |
| 106 | |
| 107 | if (oWins) { |
| 108 | return 'win'; |
| 109 | } |
| 110 | |
| 111 | // Edge Case: Draw - board is full with no winner |
| 112 | if (xCount + oCount === 9) { |
| 113 | return 'draw'; |
| 114 | } |
| 115 | |
| 116 | // Edge Case: Ongoing game - board not full and no winner |
| 117 | return 'ongoing'; |
| 118 | |
| 119 | // Handled Edge Cases: |
| 120 | // 1. Invalid turn order - O started |
| 121 | // 2. Invalid turn order - X went twice |
| 122 | // 3. Both players win (impossible in valid game) |
| 123 | // 4. Game continued after X win |
| 124 | // 5. Game continued after O win |
| 125 | // 6. Draw - board is full with no winner |
| 126 | // 7. Ongoing game - board not full and no winner |
| 127 | // 8. Invalid board dimensions |
| 128 | // 9. Invalid characters on board |
| 12 | 129 | }; |