| 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 | * Each string represents a row with 3 characters ('X', 'O', or ' '). |
| 6 | * @returns {string} - Returns 'win' if a player has won, 'draw' if the game ended in a draw, |
| 7 | * or 'ongoing' if the game is still in progress. |
| 8 | * @throws {Error} - Throws an error if the board is invalid due to incorrect turn order |
| 9 | * or if the game continued after a win. |
| 9 | 10 | */ |
| 10 | 11 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Validate board structure |
| 13 | if (!Array.isArray(board) || board.length !== 3) { |
| 14 | throw new Error('Invalid board: must have 3 rows'); |
| 15 | } |
| 16 | |
| 17 | for (let i = 0; i < 3; i++) { |
| 18 | if (typeof board[i] !== 'string' || board[i].length !== 3) { |
| 19 | throw new Error('Invalid board: each row must be a string of length 3'); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // Count Xs and Os |
| 24 | let xCount = 0; |
| 25 | let oCount = 0; |
| 26 | |
| 27 | for (let i = 0; i < 3; i++) { |
| 28 | for (let j = 0; j < 3; j++) { |
| 29 | const cell = board[i][j]; |
| 30 | if (cell === 'X') { |
| 31 | xCount++; |
| 32 | } else if (cell === 'O') { |
| 33 | oCount++; |
| 34 | } else if (cell !== ' ') { |
| 35 | throw new Error('Invalid board: contains invalid characters'); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Check for wins |
| 41 | const winForX = checkWin(board, 'X'); |
| 42 | const winForO = checkWin(board, 'O'); |
| 43 | |
| 44 | // Edge Case: Both players won (impossible in a valid game) |
| 45 | if (winForX && winForO) { |
| 46 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 47 | } |
| 48 | |
| 49 | // Edge Case: Invalid turn order |
| 50 | if (xCount < oCount) { |
| 51 | // O has more moves than X |
| 52 | if (xCount === 0 && oCount === 1) { |
| 53 | throw new Error('Wrong turn order: O started'); |
| 54 | } else if (oCount > xCount + 1) { |
| 55 | // O has more than one extra move |
| 56 | throw new Error('Wrong turn order: O went twice'); |
| 57 | } else if (!winForO) { |
| 58 | // O has one more move than X but no win for O |
| 59 | throw new Error('Wrong turn order: O went twice'); |
| 60 | } |
| 61 | // If oCount = xCount + 1 and O wins, it's valid |
| 62 | } else if (xCount > oCount + 1) { |
| 63 | // X has more than one extra move |
| 64 | throw new Error('Wrong turn order: X went twice'); |
| 65 | } |
| 66 | |
| 67 | // Edge Case: Game continued after a win - if X wins, X should have one more move than O |
| 68 | if (winForX && xCount !== oCount + 1) { |
| 69 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 70 | } |
| 71 | |
| 72 | // Edge Case: Game continued after a win - if O wins, both should have same number of moves |
| 73 | if (winForO && xCount !== oCount) { |
| 74 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 75 | } |
| 76 | |
| 77 | // Edge Case: Game continued after a win - if X wins, X should have one more move than O |
| 78 | if (winForX && xCount !== oCount + 1) { |
| 79 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 80 | } |
| 81 | |
| 82 | // Edge Case: Game continued after a win - if O wins, both should have same number of moves |
| 83 | if (winForO && xCount !== oCount) { |
| 84 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 85 | } |
| 86 | |
| 87 | // Determine game state |
| 88 | if (winForX || winForO) { |
| 89 | return 'win'; |
| 90 | } |
| 91 | |
| 92 | // Edge Case: Board is full (draw) |
| 93 | if (xCount + oCount === 9) { |
| 94 | return 'draw'; |
| 95 | } |
| 96 | |
| 97 | // Edge Case: Game is still ongoing |
| 98 | return 'ongoing'; |
| 99 | |
| 100 | // Handled Edge Cases: Invalid turn order (O started, X went twice), |
| 101 | // Game continued after a win, |
| 102 | // Both players won (impossible), |
| 103 | // Board is full (draw), |
| 104 | // Game is still ongoing |
| 12 | 105 | }; |
| 106 | |
| 107 | /* |
| 108 | * Checks if the specified player has won on the given board. |
| 109 | * |
| 110 | * @param {string[]} board - The game board. |
| 111 | * @param {string} player - The player to check for a win ('X' or 'O'). |
| 112 | * @returns {boolean} - True if the player has won, false otherwise. |
| 113 | */ |
| 114 | function checkWin(board, player) { |
| 115 | // Check rows |
| 116 | for (let i = 0; i < 3; i++) { |
| 117 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Check columns |
| 123 | for (let j = 0; j < 3; j++) { |
| 124 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Check diagonals |
| 130 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | |
| 140 | // Handled Edge Cases: All possible win conditions (rows, columns, diagonals) |
| 141 | } |