| 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 provided 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 i = 0; i < 3; i++) { |
| 15 | if (board[i].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 i = 0; i < 3; i++) { |
| 25 | for (let j = 0; j < 3; j++) { |
| 26 | const cell = board[i].charAt(j); |
| 27 | if (cell === 'X') { |
| 28 | xCount++; |
| 29 | } else if (cell === 'O') { |
| 30 | oCount++; |
| 31 | } else if (cell !== ' ') { |
| 32 | throw new Error('Invalid board: contains invalid characters'); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Check for wins |
| 38 | const checkWin = (player) => { |
| 39 | // Check rows |
| 40 | for (let i = 0; i < 3; i++) { |
| 41 | if (board[i].charAt(0) === player && board[i].charAt(1) === player && board[i].charAt(2) === player) { |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Check columns |
| 47 | for (let j = 0; j < 3; j++) { |
| 48 | if (board[0].charAt(j) === player && board[1].charAt(j) === player && board[2].charAt(j) === player) { |
| 49 | return true; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Check diagonals |
| 54 | if (board[0].charAt(0) === player && board[1].charAt(1) === player && board[2].charAt(2) === player) { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | if (board[0].charAt(2) === player && board[1].charAt(1) === player && board[2].charAt(0) === player) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | }; |
| 64 | |
| 65 | const xWins = checkWin('X'); |
| 66 | const oWins = checkWin('O'); |
| 67 | |
| 68 | // Edge Case: Both players win (impossible in valid games) |
| 69 | if (xWins && oWins) { |
| 70 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 71 | } |
| 72 | |
| 73 | // Check if game continued after win BEFORE validating turn order |
| 74 | if (xWins && oCount >= xCount) { |
| 75 | // X won but O continued playing |
| 76 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 77 | } |
| 78 | |
| 79 | if (oWins && xCount > oCount) { |
| 80 | // O won but X continued playing |
| 81 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 82 | } |
| 83 | |
| 84 | // Validate turn order based on who won |
| 85 | if (xWins) { |
| 86 | // If X won, X should have exactly one more move than O |
| 87 | if (xCount !== oCount + 1) { |
| 88 | throw new Error('Wrong turn order: X went twice'); |
| 89 | } |
| 90 | } else if (oWins) { |
| 91 | // If O won, X and O should have equal moves |
| 92 | if (xCount !== oCount) { |
| 93 | throw new Error('Wrong turn order: O went twice'); |
| 94 | } |
| 95 | } else { |
| 96 | // No winner yet - validate turn order for ongoing game |
| 97 | if (oCount > xCount) { |
| 98 | // O has more moves than X - this is always invalid |
| 99 | if (xCount === 0) { |
| 100 | throw new Error('Wrong turn order: O started'); |
| 101 | } else { |
| 102 | throw new Error('Wrong turn order: O went twice'); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (xCount > oCount + 1) { |
| 107 | // X has more than 1 extra move - this is always invalid |
| 108 | throw new Error('Wrong turn order: X went twice'); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Determine game state |
| 113 | if (xWins || oWins) { |
| 114 | return 'win'; |
| 115 | } |
| 116 | |
| 117 | // Edge Case: Draw - board is full with no winner |
| 118 | if (xCount + oCount === 9) { |
| 119 | return 'draw'; |
| 120 | } |
| 121 | |
| 122 | // Edge Case: Ongoing game - board not full and no winner |
| 123 | return 'ongoing'; |
| 124 | |
| 125 | // Handled Edge Cases: |
| 126 | // - Invalid board dimensions |
| 127 | // - Invalid characters on board |
| 128 | // - Wrong turn order (O started) |
| 129 | // - Wrong turn order (X went twice) |
| 130 | // - Both players winning (impossible) |
| 131 | // - Game continued after win (X won but O played) |
| 132 | // - Game continued after win (O won but X played) |
| 133 | // - Draw (board full with no winner) |
| 134 | // - Ongoing game (board not full and no winner) |
| 135 | }; |