| 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 | // Edge Case: Wrong turn order - O started |
| 37 | if (xCount < oCount) { |
| 38 | throw new Error('Wrong turn order: O started'); |
| 39 | } |
| 40 | |
| 41 | // Edge Case: Wrong turn order - X went twice in a row |
| 42 | if (xCount > oCount + 1) { |
| 43 | throw new Error('Wrong turn order: X went twice'); |
| 44 | } |
| 45 | |
| 46 | // Check for wins |
| 47 | const wins = checkWins(board); |
| 48 | const xWins = wins.X; |
| 49 | const oWins = wins.O; |
| 50 | |
| 51 | // Edge Case: Both players have winning lines |
| 52 | if (xWins > 0 && oWins > 0) { |
| 53 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 54 | } |
| 55 | |
| 56 | // Edge Case: Game continued after a win |
| 57 | if (xWins > 0 && xCount === oCount) { |
| 58 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 59 | } |
| 60 | |
| 61 | // Edge Case: Game continued after a win |
| 62 | if (oWins > 0 && xCount > oCount) { |
| 63 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 64 | } |
| 65 | |
| 66 | // Determine game state |
| 67 | if (xWins > 0) return 'win'; |
| 68 | if (oWins > 0) return 'win'; |
| 69 | |
| 70 | // Edge Case: Full board with no winner (draw) |
| 71 | if (xCount + oCount === 9) return 'draw'; |
| 72 | |
| 73 | // Edge Case: Game is still ongoing |
| 74 | return 'ongoing'; |
| 12 | 75 | }; |
| 76 | |
| 77 | function checkWins(board) { |
| 78 | let xWins = 0; |
| 79 | let oWins = 0; |
| 80 | |
| 81 | // Check rows |
| 82 | for (let i = 0; i < 3; i++) { |
| 83 | if (board[i][0] === board[i][1] && board[i][1] === board[i][2]) { |
| 84 | if (board[i][0] === 'X') xWins++; |
| 85 | else if (board[i][0] === 'O') oWins++; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Check columns |
| 90 | for (let i = 0; i < 3; i++) { |
| 91 | if (board[0][i] === board[1][i] && board[1][i] === board[2][i]) { |
| 92 | if (board[0][i] === 'X') xWins++; |
| 93 | else if (board[0][i] === 'O') oWins++; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Check diagonals |
| 98 | if (board[0][0] === board[1][1] && board[1][1] === board[2][2]) { |
| 99 | if (board[0][0] === 'X') xWins++; |
| 100 | else if (board[0][0] === 'O') oWins++; |
| 101 | } |
| 102 | |
| 103 | if (board[0][2] === board[1][1] && board[1][1] === board[2][0]) { |
| 104 | if (board[0][2] === 'X') xWins++; |
| 105 | else if (board[0][2] === 'O') oWins++; |
| 106 | } |
| 107 | |
| 108 | return { X: xWins, O: oWins }; |
| 109 | } |
| 110 | |
| 111 | // Handled Edge Cases: |
| 112 | // - Wrong turn order (O started) |
| 113 | // - Wrong turn order (X went twice in a row) |
| 114 | // - Both players have winning lines |
| 115 | // - Game continued after a win (X won but O played) |
| 116 | // - Game continued after a win (O won but X played) |
| 117 | // - Full board with no winner (draw) |
| 118 | // - Game is still ongoing |
| 119 | // - Invalid board dimensions |
| 120 | // - Invalid characters on board |