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