| 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 has 3 characters which can be 'X', 'O', or ' '. |
| 11 | * @returns {string} - Returns '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 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 3 characters'); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // Count X's and O's |
| 27 | let countX = 0; |
| 28 | let countO = 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 | countX++; |
| 35 | } else if (char === 'O') { |
| 36 | countO++; |
| 37 | } else if (char !== ' ') { |
| 38 | throw new Error('Invalid board: contains invalid characters'); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Check for wins |
| 44 | const winPatterns = [ |
| 45 | // Rows |
| 46 | [[0,0], [0,1], [0,2]], |
| 47 | [[1,0], [1,1], [1,2]], |
| 48 | [[2,0], [2,1], [2,2]], |
| 49 | // Columns |
| 50 | [[0,0], [1,0], [2,0]], |
| 51 | [[0,1], [1,1], [2,1]], |
| 52 | [[0,2], [1,2], [2,2]], |
| 53 | // Diagonals |
| 54 | [[0,0], [1,1], [2,2]], |
| 55 | [[0,2], [1,1], [2,0]] |
| 56 | ]; |
| 57 | |
| 58 | let xWins = false; |
| 59 | let oWins = false; |
| 60 | |
| 61 | for (const pattern of winPatterns) { |
| 62 | const [a, b, c] = pattern; |
| 63 | const valA = board[a[0]][a[1]]; |
| 64 | const valB = board[b[0]][b[1]]; |
| 65 | const valC = board[c[0]][c[1]]; |
| 66 | |
| 67 | if (valA !== ' ' && valA === valB && valB === valC) { |
| 68 | if (valA === 'X') { |
| 69 | xWins = true; |
| 70 | } else if (valA === 'O') { |
| 71 | oWins = true; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Edge Case: Game continued after win |
| 77 | if (xWins && oWins) { |
| 78 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 79 | } |
| 80 | |
| 81 | // Edge Case: Game continued after win (X won but O kept playing) |
| 82 | if (xWins && countO === countX) { |
| 83 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 84 | } |
| 85 | |
| 86 | // Edge Case: Game continued after win (O won but X kept playing) |
| 87 | if (oWins && countX > countO) { |
| 88 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 89 | } |
| 90 | |
| 91 | // Edge Case: Invalid turn order - O started the game |
| 92 | if (countO > countX) { |
| 93 | throw new Error('Wrong turn order: O started'); |
| 94 | } |
| 95 | |
| 96 | // Edge Case: Invalid turn order - X went twice in a row |
| 97 | if (countX > countO + 1) { |
| 98 | throw new Error('Wrong turn order: X went twice'); |
| 99 | } |
| 100 | |
| 101 | // Edge Case: Invalid turn order - O started (equal counts when X should have one more) |
| 102 | if (countO === countX && countX > 0 && !xWins && !oWins) { |
| 103 | throw new Error('Wrong turn order: O started'); |
| 104 | } |
| 105 | |
| 106 | // Determine game state |
| 107 | if (xWins || oWins) { |
| 108 | return 'win'; |
| 109 | } |
| 110 | |
| 111 | // Edge Case: Board is full (draw) |
| 112 | if (countX + countO === 9) { |
| 113 | return 'draw'; |
| 114 | } |
| 115 | |
| 116 | return 'ongoing'; |
| 117 | |
| 118 | // Handled Edge Cases: |
| 119 | // 1. Invalid turn order - O started the game |
| 120 | // 2. Invalid turn order - X went twice in a row |
| 121 | // 3. Game continued after win (both players won) |
| 122 | // 4. Game continued after win (X won but O kept playing) |
| 123 | // 5. Game continued after win (O won but X kept playing) |
| 124 | // 6. Board is full (draw) |
| 125 | // 7. Invalid board structure |
| 126 | // 8. Invalid characters on board |
| 127 | }; |