| 1 | | // |
| 2 | | // This is only a SKELETON file for the 'State of Tic Tac Toe' exercise. It's been provided as a |
| 3 | | // convenience to get you started writing code faster. |
| 4 | | // |
| 5 | | |
| 6 | | /** |
| 7 | | * @param {string[]} board |
| 8 | | * @returns {string} |
| 1 | /* |
| 2 | * Determines the state of a Tic-Tac-Toe game based on the given board. |
| 3 | * |
| 4 | * @param {string[]} board - An array of strings representing the game board. |
| 5 | * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state. |
| 6 | * @throws {Error} - Throws an error for invalid board states with specific messages. |
| 9 | 7 | */ |
| 10 | 8 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | | }; |
| 9 | // Validate board dimensions |
| 10 | if (board.length !== 3) { |
| 11 | throw new Error('Invalid board: must have 3 rows'); |
| 12 | } |
| 13 | |
| 14 | for (let row of board) { |
| 15 | if (row.length !== 3) { |
| 16 | throw new Error('Invalid board: each row must have 3 columns'); |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // Count X's and O's |
| 21 | let xCount = 0; |
| 22 | let oCount = 0; |
| 23 | |
| 24 | for (let row of board) { |
| 25 | for (let cell of row) { |
| 26 | if (cell === 'X') xCount++; |
| 27 | else if (cell === 'O') oCount++; |
| 28 | else if (cell !== ' ') { |
| 29 | throw new Error('Invalid board: contains invalid characters'); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Check for wins first |
| 35 | const winPatterns = [ |
| 36 | // Rows |
| 37 | [board[0][0], board[0][1], board[0][2]], |
| 38 | [board[1][0], board[1][1], board[1][2]], |
| 39 | [board[2][0], board[2][1], board[2][2]], |
| 40 | // Columns |
| 41 | [board[0][0], board[1][0], board[2][0]], |
| 42 | [board[0][1], board[1][1], board[2][1]], |
| 43 | [board[0][2], board[1][2], board[2][2]], |
| 44 | // Diagonals |
| 45 | [board[0][0], board[1][1], board[2][2]], |
| 46 | [board[0][2], board[1][1], board[2][0]] |
| 47 | ]; |
| 48 | |
| 49 | let xWins = false; |
| 50 | let oWins = false; |
| 51 | |
| 52 | for (const pattern of winPatterns) { |
| 53 | if (pattern[0] === 'X' && pattern[1] === 'X' && pattern[2] === 'X') { |
| 54 | xWins = true; |
| 55 | } |
| 56 | if (pattern[0] === 'O' && pattern[1] === 'O' && pattern[2] === 'O') { |
| 57 | oWins = true; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Validate turn order - X starts, players alternate |
| 62 | // Need to distinguish between O starting vs O going twice |
| 63 | |
| 64 | // First check for obvious turn order violations |
| 65 | if (xCount < oCount) { |
| 66 | // O has more moves than X - check who started |
| 67 | let firstMove = null; |
| 68 | outer: for (let i = 0; i < 3; i++) { |
| 69 | for (let j = 0; j < 3; j++) { |
| 70 | if (board[i][j] === 'X') { |
| 71 | firstMove = 'X'; |
| 72 | break outer; |
| 73 | } else if (board[i][j] === 'O') { |
| 74 | firstMove = 'O'; |
| 75 | break outer; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (firstMove === 'O') { |
| 81 | // O started the game |
| 82 | throw new Error('Wrong turn order: O started'); |
| 83 | } |
| 84 | // If first move was X but O has more moves, check if it's valid after a win |
| 85 | } |
| 86 | |
| 87 | if (xCount > oCount + 1) { |
| 88 | // X has more than one extra move - X went twice |
| 89 | throw new Error('Wrong turn order: X went twice'); |
| 90 | } |
| 91 | |
| 92 | // Now check win conditions and their interaction with turn order |
| 93 | if (xWins || oWins) { |
| 94 | // If there's a win, check if the turn order is valid for that win |
| 95 | if (xWins && oWins) { |
| 96 | // Both players win - check if it's due to too many moves first |
| 97 | if (xCount > oCount + 1) { |
| 98 | throw new Error('Wrong turn order: X went twice'); |
| 99 | } else if (oCount > xCount + 1) { |
| 100 | throw new Error('Wrong turn order: O went twice'); |
| 101 | } else { |
| 102 | // Both players win with valid counts - impossible board |
| 103 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (xWins) { |
| 108 | // X wins - O can have same count (O went last), one more (game continued), or one less (X went last) |
| 109 | // Valid cases: xCount == oCount (O went last), xCount == oCount + 1 (X went last) |
| 110 | // Exception: xCount == oCount - 1 (game continued after X win) is also valid |
| 111 | if (xCount < oCount - 1 || xCount > oCount + 1) { |
| 112 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (oWins) { |
| 117 | // O wins - X can have same count (X went last), one more (O went last), or one less (game continued) |
| 118 | // Valid cases: oCount == xCount (X went last), oCount == xCount + 1 (O went last) |
| 119 | // Exception: oCount == xCount - 1 (game continued after O win) is also valid |
| 120 | if (oCount < xCount - 1 || oCount > xCount + 1) { |
| 121 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 122 | } |
| 123 | } |
| 124 | } else { |
| 125 | // No win - check remaining turn order violations |
| 126 | if (xCount < oCount) { |
| 127 | // O went twice in a row (first move was X but O has more moves) |
| 128 | throw new Error('Wrong turn order: O went twice'); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Determine game state |
| 133 | if (xWins || oWins) { |
| 134 | return 'win'; |
| 135 | } |
| 136 | |
| 137 | // Edge Case: Draw - board is full with no winner |
| 138 | if (xCount + oCount === 9) { |
| 139 | return 'draw'; |
| 140 | } |
| 141 | |
| 142 | // Edge Case: Ongoing game - board is not full and no winner |
| 143 | return 'ongoing'; |
| 144 | |
| 145 | // Handled Edge Cases: |
| 146 | // - Invalid turn order (O started) |
| 147 | // - Invalid turn order (X went twice) |
| 148 | // - Both players win |
| 149 | // - Game continued after X win |
| 150 | // - Game continued after O win |
| 151 | // - Draw (board full with no winner) |
| 152 | // - Ongoing game (board not full and no winner) |
| 153 | // - Invalid board dimensions |
| 154 | // - Invalid characters on board |
| 155 | }; |