| 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, each representing a row of the 3x3 board. |
| 10 | * Each character is either 'X', 'O', or ' ' (empty space). |
| 11 | * @returns {string} - Returns one of 'win', 'draw', or 'ongoing'. |
| 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 length 3'); |
| 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 |
| 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 (takes precedence over other errors) |
| 74 | // Both players have winning patterns - game continued after first win |
| 75 | if (xWins && oWins) { |
| 76 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 77 | } |
| 78 | |
| 79 | // X won but O kept playing (and potentially created their own win) |
| 80 | // This is invalid if O has a winning pattern or if O played after X's win |
| 81 | if (xWins) { |
| 82 | // Check if O continued playing after X's win |
| 83 | if (oCount > xCount - 1) { // X should have one more move than O at time of win |
| 84 | // Check if O created their own winning pattern |
| 85 | if (oWins) { |
| 86 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 87 | } |
| 88 | // If O just played one extra move but didn't win, it might still be valid |
| 89 | // But if O played multiple extra moves, it's invalid |
| 90 | if (oCount > xCount) { |
| 91 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // O won but X kept playing |
| 97 | if (oWins) { |
| 98 | // Check if X continued playing after O's win |
| 99 | if (xCount > oCount) { // X should have same number of moves as O at time of win |
| 100 | // Check if X created their own winning pattern |
| 101 | if (xWins) { |
| 102 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 103 | } |
| 104 | // If X played one extra move but didn't win, it might still be valid |
| 105 | // But if X played multiple extra moves, it's invalid |
| 106 | if (xCount > oCount + 1) { |
| 107 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Validate turn order based on move counts |
| 113 | // X starts first, so X count should be equal to or one more than O count |
| 114 | if (xCount < oCount || xCount > oCount + 1) { |
| 115 | // Determine specific error message |
| 116 | if (xCount < oCount) { |
| 117 | // O has more moves than X |
| 118 | throw new Error('Wrong turn order: O started'); |
| 119 | } else { |
| 120 | // X has more than one move more than O |
| 121 | throw new Error('Wrong turn order: X went twice'); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // For boards where O has moves, check if O started the game |
| 126 | if (oCount > 0) { |
| 127 | // Find the first move |
| 128 | let firstMove = ''; |
| 129 | outer: for (let i = 0; i < 3; i++) { |
| 130 | for (let j = 0; j < 3; j++) { |
| 131 | if (board[i][j] === 'X' || board[i][j] === 'O') { |
| 132 | firstMove = board[i][j]; |
| 133 | break outer; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // O started the game |
| 139 | if (firstMove === 'O') { |
| 140 | throw new Error('Wrong turn order: O started'); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Edge Case: Game continued after win (takes precedence over other errors) |
| 145 | // X won but O kept playing |
| 146 | if (xWins && oCount >= xCount) { |
| 147 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 148 | } |
| 149 | |
| 150 | // O won but X kept playing |
| 151 | if (oWins && xCount > oCount) { |
| 152 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 153 | } |
| 154 | |
| 155 | // Determine game state |
| 156 | if (xWins || oWins) { |
| 157 | return 'win'; |
| 158 | } |
| 159 | |
| 160 | // Edge Case: Game is a draw - board is full with no winner |
| 161 | if (xCount + oCount === 9) { |
| 162 | return 'draw'; |
| 163 | } |
| 164 | |
| 165 | // Edge Case: Game is ongoing - board is not full and no winner |
| 166 | return 'ongoing'; |
| 167 | |
| 168 | // Handled Edge Cases: |
| 169 | // - Invalid turn order (O started) |
| 170 | // - Invalid turn order (player went twice) |
| 171 | // - Game continued after win (both players won) |
| 172 | // - Game continued after win (X won but O played) |
| 173 | // - Game continued after win (O won but X played) |
| 174 | // - Game is a draw |
| 175 | // - Game is ongoing |
| 176 | }; |