| 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 representing the rows of the board. |
| 10 | * Each string contains 3 characters: 'X', 'O', or ' '. |
| 11 | * @returns {string} - Returns 'win', 'draw', or 'ongoing' based on the game state. |
| 12 | * @throws {Error} - Throws an error if the board is invalid. |
| 9 | 13 | */ |
| 10 | 14 | export const gamestate = (board) => { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 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 3 characters'); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // Count Xs and Os |
| 27 | let countX = 0; |
| 28 | let countO = 0; |
| 29 | |
| 30 | for (let i = 0; i < 3; i++) { |
| 31 | for (let j = 0; j < 3; j++) { |
| 32 | const cell = board[i][j]; |
| 33 | if (cell === 'X') { |
| 34 | countX++; |
| 35 | } else if (cell === 'O') { |
| 36 | countO++; |
| 37 | } else if (cell !== ' ') { |
| 38 | throw new Error('Invalid board: contains invalid characters'); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // Check for wins |
| 44 | const winForX = checkWin(board, 'X'); |
| 45 | const winForO = checkWin(board, 'O'); |
| 46 | |
| 47 | // Edge Case: Both players won (impossible) |
| 48 | if (winForX && winForO) { |
| 49 | throw new Error('Impossible board: game should have ended after the game was won'); |
| 50 | } |
| 51 | |
| 52 | // If there's a win, return the appropriate state |
| 53 | if (winForX || winForO) { |
| 54 | return 'win'; |
| 55 | } |
| 56 | |
| 57 | // Check for basic turn order violations |
| 58 | if (countO > countX) { |
| 59 | throw new Error('Wrong turn order: O started'); |
| 60 | } |
| 61 | |
| 62 | if (countX > countO + 1) { |
| 63 | throw new Error('Wrong turn order: X went twice'); |
| 64 | } |
| 65 | |
| 66 | // Check if board is full (draw) or ongoing |
| 67 | if (countX + countO === 9) { |
| 68 | return 'draw'; |
| 69 | } else { |
| 70 | return 'ongoing'; |
| 71 | } |
| 72 | |
| 73 | // Determine game state |
| 74 | if (winForX || winForO) { |
| 75 | return 'win'; |
| 76 | } |
| 77 | |
| 78 | // Edge Case: Draw - board is full with no winner |
| 79 | if (countX + countO === 9) { |
| 80 | return 'draw'; |
| 81 | } |
| 82 | |
| 83 | // Edge Case: Ongoing game - board is not full and no winner |
| 84 | return 'ongoing'; |
| 85 | |
| 86 | // Handled Edge Cases: Invalid board structure, invalid characters, O started, X went twice, |
| 87 | // both players won, game continued after win, draw, ongoing game |
| 12 | 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * Checks if the given player has won on the board. |
| 92 | * |
| 93 | * @param {string[]} board - The game board. |
| 94 | * @param {string} player - The player to check for win ('X' or 'O'). |
| 95 | * @returns {boolean} - True if the player has won, false otherwise. |
| 96 | */ |
| 97 | function checkWin(board, player) { |
| 98 | // Check rows |
| 99 | for (let i = 0; i < 3; i++) { |
| 100 | if (board[i][0] === player && board[i][1] === player && board[i][2] === player) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Check columns |
| 106 | for (let j = 0; j < 3; j++) { |
| 107 | if (board[0][j] === player && board[1][j] === player && board[2][j] === player) { |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Check diagonals |
| 113 | if (board[0][0] === player && board[1][1] === player && board[2][2] === player) { |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | if (board[0][2] === player && board[1][1] === player && board[2][0] === player) { |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | |
| 123 | // Handled Edge Cases: All possible win conditions (rows, columns, diagonals) |
| 124 | } |