| 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 with specific messages for invalid board states. |
| 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 | for (let j = 0; j < 3; j++) { |
| 25 | const char = board[i][j]; |
| 26 | if (char !== 'X' && char !== 'O' && char !== ' ') { |
| 27 | throw new Error('Invalid board: contains invalid characters'); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Count X's and O's |
| 33 | let xCount = 0; |
| 34 | let oCount = 0; |
| 35 | |
| 36 | for (let i = 0; i < 3; i++) { |
| 37 | for (let j = 0; j < 3; j++) { |
| 38 | if (board[i][j] === 'X') xCount++; |
| 39 | if (board[i][j] === 'O') oCount++; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Check for wins first |
| 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') xWins = true; |
| 69 | if (valA === 'O') oWins = true; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Edge Case: Game continued after win - both players have winning lines |
| 74 | if (xWins && oWins) { |
| 75 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 76 | } |
| 77 | |
| 78 | // Edge Case: Game continued after win - X won but O made another move |
| 79 | if (xWins && oCount > xCount) { |
| 80 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 81 | } |
| 82 | |
| 83 | // Edge Case: Game continued after win - O won but X made another move |
| 84 | if (oWins && xCount > oCount) { |
| 85 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 86 | } |
| 87 | |
| 88 | // For boards with wins, we need to check if the game continued after the win |
| 89 | // For boards without wins, we validate turn order |
| 90 | if (!xWins && !oWins) { |
| 91 | // Validate turn order for non-winning boards |
| 92 | // Special case: if X hasn't played but O has, then O started (invalid) |
| 93 | if (xCount === 0 && oCount > 0) { |
| 94 | throw new Error('Wrong turn order: O started'); |
| 95 | } |
| 96 | |
| 97 | // Special case: if O has more moves than X and X has played, then O went twice (invalid) |
| 98 | if (oCount > xCount && xCount > 0) { |
| 99 | throw new Error('Wrong turn order: O went twice'); |
| 100 | } |
| 101 | |
| 102 | // If X has more than one move more than O, then X went twice (invalid) |
| 103 | if (xCount > oCount + 1) { |
| 104 | throw new Error('Wrong turn order: X went twice'); |
| 105 | } |
| 106 | } |
| 107 | // Determine game state |
| 108 | if (xWins || oWins) { |
| 109 | return 'win'; |
| 110 | } |
| 111 | |
| 112 | // Edge Case: Game is a draw - board is full with no winner |
| 113 | if (xCount + oCount === 9) { |
| 114 | return 'draw'; |
| 115 | } |
| 116 | |
| 117 | // Edge Case: Game is ongoing - board is not full and no winner |
| 118 | return 'ongoing'; |
| 119 | |
| 120 | // Handled Edge Cases: |
| 121 | // - Invalid turn order: O started the game |
| 122 | // - Invalid turn order: X went twice in a row |
| 123 | // - Game continued after win: both players have winning lines |
| 124 | // - Game continued after win: X won but O made another move |
| 125 | // - Game continued after win: O won but X made another move |
| 126 | // - Game is a draw: board is full with no winner |
| 127 | // - Game is ongoing: board is not full and no winner |
| 128 | }; |