| 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 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 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 | // Edge Case: Invalid characters on the board |
| 32 | throw new Error('Invalid board: contains invalid characters'); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Edge Case: Wrong turn order - O started |
| 38 | if (oCount > xCount) { |
| 39 | throw new Error('Wrong turn order: O started'); |
| 40 | } |
| 41 | |
| 42 | // Edge Case: Wrong turn order - X went twice |
| 43 | if (xCount > oCount + 1) { |
| 44 | throw new Error('Wrong turn order: X went twice'); |
| 45 | } |
| 46 | |
| 47 | // Check for wins |
| 48 | const winPatterns = [ |
| 49 | // Rows |
| 50 | [[0,0], [0,1], [0,2]], |
| 51 | [[1,0], [1,1], [1,2]], |
| 52 | [[2,0], [2,1], [2,2]], |
| 53 | // Columns |
| 54 | [[0,0], [1,0], [2,0]], |
| 55 | [[0,1], [1,1], [2,1]], |
| 56 | [[0,2], [1,2], [2,2]], |
| 57 | // Diagonals |
| 58 | [[0,0], [1,1], [2,2]], |
| 59 | [[0,2], [1,1], [2,0]] |
| 60 | ]; |
| 61 | |
| 62 | let xWins = false; |
| 63 | let oWins = false; |
| 64 | |
| 65 | for (const pattern of winPatterns) { |
| 66 | const [a, b, c] = pattern; |
| 67 | const [rowA, colA] = a; |
| 68 | const [rowB, colB] = b; |
| 69 | const [rowC, colC] = c; |
| 70 | |
| 71 | const cellA = board[rowA][colA]; |
| 72 | const cellB = board[rowB][colB]; |
| 73 | const cellC = board[rowC][colC]; |
| 74 | |
| 75 | if (cellA === 'X' && cellB === 'X' && cellC === 'X') { |
| 76 | xWins = true; |
| 77 | } |
| 78 | if (cellA === 'O' && cellB === 'O' && cellC === 'O') { |
| 79 | oWins = true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Edge Case: Both players win (impossible in a valid game) |
| 84 | if (xWins && oWins) { |
| 85 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 86 | } |
| 87 | |
| 88 | // Edge Case: Game continued after win |
| 89 | if (xWins && oCount >= xCount) { |
| 90 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 91 | } |
| 92 | |
| 93 | // Edge Case: Game continued after win |
| 94 | if (oWins && xCount > oCount) { |
| 95 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 96 | } |
| 97 | |
| 98 | // Determine game state |
| 99 | if (xWins) { |
| 100 | return 'win'; |
| 101 | } |
| 102 | |
| 103 | if (oWins) { |
| 104 | return 'win'; |
| 105 | } |
| 106 | |
| 107 | // Edge Case: Draw - board is full with no winner |
| 108 | if (xCount + oCount === 9) { |
| 109 | return 'draw'; |
| 110 | } |
| 111 | |
| 112 | // Edge Case: Ongoing game - board is not full and no winner |
| 113 | return 'ongoing'; |
| 114 | |
| 115 | // Handled Edge Cases: |
| 116 | // 1. Invalid characters on the board |
| 117 | // 2. Wrong turn order - O started |
| 118 | // 3. Wrong turn order - X went twice |
| 119 | // 4. Both players win (impossible in a valid game) |
| 120 | // 5. Game continued after X win |
| 121 | // 6. Game continued after O win |
| 122 | // 7. Draw - board is full with no winner |
| 123 | // 8. Ongoing game - board is not full and no winner |
| 124 | // 9. Invalid board dimensions |
| 125 | }; |