| 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 | * @param {string[]} board - An array of strings representing the 3x3 game board. |
| 4 | * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state. |
| 5 | * @throws {Error} - Throws an error for invalid board states with specific messages. |
| 9 | 6 | */ |
| 10 | 7 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 8 | // Validate board dimensions |
| 9 | if (board.length !== 3 || board.some(row => row.length !== 3)) { |
| 10 | throw new Error('Invalid board: must be 3x3'); |
| 11 | } |
| 12 | |
| 13 | // Count X's and O's |
| 14 | let xCount = 0; |
| 15 | let oCount = 0; |
| 16 | |
| 17 | for (let i = 0; i < 3; i++) { |
| 18 | for (let j = 0; j < 3; j++) { |
| 19 | const cell = board[i][j]; |
| 20 | if (cell === 'X') xCount++; |
| 21 | else if (cell === 'O') oCount++; |
| 22 | else if (cell !== ' ') { |
| 23 | // Edge Case: Invalid characters on the board |
| 24 | throw new Error('Invalid board: contains invalid characters'); |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // Edge Case: Wrong turn order validation |
| 30 | // In a valid game where X starts: |
| 31 | // - X count should be equal to O count (O just played) or |
| 32 | // - X count should be one more than O count (X just played) |
| 33 | |
| 34 | // Check for no X's but O's exist |
| 35 | if (xCount === 0 && oCount > 0) { |
| 36 | if (oCount === 1) { |
| 37 | // Single O with no X's - O started the game |
| 38 | throw new Error('Wrong turn order: O started'); |
| 39 | } else { |
| 40 | // Multiple O's with no X's - O went twice |
| 41 | throw new Error('Wrong turn order: O went twice'); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Check if O started the game (first move was O and there are X's) |
| 46 | if (board[0][0] === 'O' && xCount > 0) { |
| 47 | throw new Error('Wrong turn order: O started'); |
| 48 | } |
| 49 | |
| 50 | // Check for O going twice (O has more moves than X, but X started) |
| 51 | if (oCount > xCount) { |
| 52 | throw new Error('Wrong turn order: O went twice'); |
| 53 | } |
| 54 | |
| 55 | // Check for X going twice (X has more than one extra move) |
| 56 | if (xCount > oCount + 1) { |
| 57 | throw new Error('Wrong turn order: X went twice'); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | // Check for wins |
| 63 | const wins = checkWins(board); |
| 64 | const xWins = wins.X; |
| 65 | const oWins = wins.O; |
| 66 | |
| 67 | // Edge Case: Both players win (impossible in a valid game) |
| 68 | if (xWins && oWins) { |
| 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 |
| 73 | if (xWins && xCount === oCount) { |
| 74 | // X won but O played the same number of times (O played after X won) |
| 75 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 76 | } |
| 77 | |
| 78 | if (oWins && xCount > oCount) { |
| 79 | // O won but X played more times (X played after O won) |
| 80 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 81 | } |
| 82 | |
| 83 | // Determine game state |
| 84 | if (xWins || oWins) { |
| 85 | return 'win'; |
| 86 | } |
| 87 | |
| 88 | // Edge Case: Board is full with no winner (draw) |
| 89 | if (xCount + oCount === 9) { |
| 90 | return 'draw'; |
| 91 | } |
| 92 | |
| 93 | // Edge Case: Game is not finished (ongoing) |
| 94 | return 'ongoing'; |
| 95 | |
| 96 | // Handled Edge Cases: Invalid characters, O starting, X going twice, both players winning, playing after win, full board with no winner, ongoing game |
| 12 | 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * Checks for wins on the board for both players. |
| 101 | * @param {string[]} board - The game board. |
| 102 | * @returns {Object} - Object with boolean properties X and O indicating if that player won. |
| 103 | */ |
| 104 | function checkWins(board) { |
| 105 | const wins = { X: false, O: false }; |
| 106 | |
| 107 | // Check rows |
| 108 | for (let i = 0; i < 3; i++) { |
| 109 | if (board[i][0] !== ' ' && board[i][0] === board[i][1] && board[i][1] === board[i][2]) { |
| 110 | wins[board[i][0]] = true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Check columns |
| 115 | for (let j = 0; j < 3; j++) { |
| 116 | if (board[0][j] !== ' ' && board[0][j] === board[1][j] && board[1][j] === board[2][j]) { |
| 117 | wins[board[0][j]] = true; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Check diagonals |
| 122 | if (board[0][0] !== ' ' && board[0][0] === board[1][1] && board[1][1] === board[2][2]) { |
| 123 | wins[board[0][0]] = true; |
| 124 | } |
| 125 | |
| 126 | if (board[0][2] !== ' ' && board[0][2] === board[1][1] && board[1][1] === board[2][0]) { |
| 127 | wins[board[0][2]] = true; |
| 128 | } |
| 129 | |
| 130 | return wins; |
| 131 | // Handled Edge Cases: All possible win conditions (rows, columns, diagonals) |
| 132 | } |