| 1 | /* eslint-disable no-plusplus */ |
| 1 | 2 | // |
| 2 | 3 | // This is only a SKELETON file for the 'State of Tic Tac Toe' exercise. It's been provided as a |
| 3 | 4 | // convenience to get you started writing code faster. |
| 4 | 5 | // |
| 5 | 6 | |
| 6 | 7 | /** |
| 7 | | * @param {string[]} board |
| 8 | | * @returns {string} |
| 8 | * Determines the state of a Tic-Tac-Toe game based on the given board. |
| 9 | * @param {string[]} board - An array of strings representing the game board. |
| 10 | * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state. |
| 11 | * @throws {Error} - Throws an error for invalid board states with specific messages. |
| 9 | 12 | */ |
| 10 | 13 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 14 | // Validate board dimensions |
| 15 | if (board.length !== 3) { |
| 16 | throw new Error('Invalid board: must have 3 rows'); |
| 17 | } |
| 18 | |
| 19 | for (let i = 0; i < 3; i++) { |
| 20 | if (board[i].length !== 3) { |
| 21 | throw new Error('Invalid board: each row must have 3 columns'); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Count X's and O's |
| 26 | let xCount = 0; |
| 27 | let oCount = 0; |
| 28 | |
| 29 | for (let i = 0; i < 3; i++) { |
| 30 | for (let j = 0; j < 3; j++) { |
| 31 | const cell = board[i][j]; |
| 32 | if (cell === 'X') { |
| 33 | xCount++; |
| 34 | } else if (cell === 'O') { |
| 35 | oCount++; |
| 36 | } else if (cell !== ' ') { |
| 37 | throw new Error('Invalid board: contains invalid characters'); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Edge Case: Invalid turn order - O started the game |
| 43 | if (oCount > xCount) { |
| 44 | throw new Error('Wrong turn order: O started'); |
| 45 | } |
| 46 | |
| 47 | // Edge Case: Invalid turn order - X went twice in a row (X has more than 1 move than O) |
| 48 | if (xCount > oCount + 1) { |
| 49 | throw new Error('Wrong turn order: X went twice'); |
| 50 | } |
| 51 | |
| 52 | // Check for wins |
| 53 | const winPatterns = [ |
| 54 | // Rows |
| 55 | [[0, 0], [0, 1], [0, 2]], |
| 56 | [[1, 0], [1, 1], [1, 2]], |
| 57 | [[2, 0], [2, 1], [2, 2]], |
| 58 | // Columns |
| 59 | [[0, 0], [1, 0], [2, 0]], |
| 60 | [[0, 1], [1, 1], [2, 1]], |
| 61 | [[0, 2], [1, 2], [2, 2]], |
| 62 | // Diagonals |
| 63 | [[0, 0], [1, 1], [2, 2]], |
| 64 | [[0, 2], [1, 1], [2, 0]], |
| 65 | ]; |
| 66 | |
| 67 | let xWins = false; |
| 68 | let oWins = false; |
| 69 | |
| 70 | for (const pattern of winPatterns) { |
| 71 | const [a, b, c] = pattern; |
| 72 | const cellA = board[a[0]][a[1]]; |
| 73 | const cellB = board[b[0]][b[1]]; |
| 74 | const cellC = board[c[0]][c[1]]; |
| 75 | |
| 76 | if (cellA === 'X' && cellB === 'X' && cellC === 'X') { |
| 77 | xWins = true; |
| 78 | } |
| 79 | if (cellA === 'O' && cellB === 'O' && cellC === 'O') { |
| 80 | oWins = true; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Edge Case: Game continued after win - Both players win or win with extra moves |
| 85 | if (xWins && oWins) { |
| 86 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 87 | } |
| 88 | |
| 89 | if (xWins && xCount === oCount) { |
| 90 | // X won but O made the same number of moves - impossible |
| 91 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 92 | } |
| 93 | |
| 94 | if (oWins && xCount > oCount) { |
| 95 | // O won but X made more moves - impossible |
| 96 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 97 | } |
| 98 | |
| 99 | // Determine game state |
| 100 | if (xWins || oWins) { |
| 101 | return 'win'; |
| 102 | } |
| 103 | |
| 104 | // Edge Case: Game ended in a draw - Board is full with no winner |
| 105 | if (xCount + oCount === 9) { |
| 106 | return 'draw'; |
| 107 | } |
| 108 | |
| 109 | // Edge Case: Game is ongoing - Board is not full and no winner |
| 110 | return 'ongoing'; |
| 111 | |
| 112 | // Handled Edge Cases: |
| 113 | // 1. Invalid turn order - O started the game |
| 114 | // 2. Invalid turn order - X went twice in a row |
| 115 | // 3. Game continued after win - Both players win |
| 116 | // 4. Game continued after win - X wins but O made the same number of moves |
| 117 | // 5. Game continued after win - O wins but X made more moves |
| 118 | // 6. Game ended in a draw - Board is full with no winner |
| 119 | // 7. Game is ongoing - Board is not full and no winner |
| 12 | 120 | }; |