| 5 | 5 | |
| 6 | 6 | export class QueenAttack { |
| 7 | 7 | constructor({ |
| 8 | | black: [blackRow, blackColumn] = [], |
| 9 | | white: [whiteRow, whiteColumn] = [], |
| 8 | black = [0, 3], |
| 9 | white = [7, 3], |
| 10 | 10 | } = {}) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 11 | // Extract coordinates |
| 12 | const [blackRow, blackColumn] = black; |
| 13 | const [whiteRow, whiteColumn] = white; |
| 14 | |
| 15 | // Edge Case: Missing queen positions |
| 16 | if (blackRow === undefined || blackColumn === undefined) { |
| 17 | throw new Error('queen must have a row and column'); |
| 18 | } |
| 19 | |
| 20 | if (whiteRow === undefined || whiteColumn === undefined) { |
| 21 | throw new Error('queen must have a row and column'); |
| 22 | } |
| 23 | |
| 24 | // Edge Case: Invalid chessboard coordinates (out of bounds) |
| 25 | if (blackRow < 0 || blackRow > 7 || blackColumn < 0 || blackColumn > 7) { |
| 26 | throw new Error('Queen must be placed on the board'); |
| 27 | } |
| 28 | |
| 29 | if (whiteRow < 0 || whiteRow > 7 || whiteColumn < 0 || whiteColumn > 7) { |
| 30 | throw new Error('Queen must be placed on the board'); |
| 31 | } |
| 32 | |
| 33 | // Edge Case: Queens on the same square |
| 34 | if (blackRow === whiteRow && blackColumn === whiteColumn) { |
| 35 | throw new Error('Queens cannot share the same space'); |
| 36 | } |
| 37 | |
| 38 | this.black = [blackRow, blackColumn]; |
| 39 | this.white = [whiteRow, whiteColumn]; |
| 12 | 40 | } |
| 13 | 41 | |
| 14 | 42 | toString() { |
| 15 | | throw new Error('Remove this line and implement the function'); |
| 43 | const board = Array(8).fill().map(() => Array(8).fill('_')); |
| 44 | |
| 45 | // Place queens on the board |
| 46 | board[this.white[0]][this.white[1]] = 'W'; |
| 47 | board[this.black[0]][this.black[1]] = 'B'; |
| 48 | |
| 49 | // Convert board to string representation |
| 50 | return board.map(row => row.join(' ')).join('\n') + '\n'; |
| 16 | 51 | } |
| 17 | 52 | |
| 18 | 53 | get canAttack() { |
| 19 | | throw new Error('Remove this line and implement the function'); |
| 54 | const [whiteRow, whiteColumn] = this.white; |
| 55 | const [blackRow, blackColumn] = this.black; |
| 56 | |
| 57 | // Edge Case: Queens on same row |
| 58 | if (whiteRow === blackRow) { |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | // Edge Case: Queens on same column |
| 63 | if (whiteColumn === blackColumn) { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | // Edge Case: Queens on same diagonal |
| 68 | const rowDiff = Math.abs(whiteRow - blackRow); |
| 69 | const colDiff = Math.abs(whiteColumn - blackColumn); |
| 70 | |
| 71 | if (rowDiff === colDiff) { |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | // Edge Case: Queens cannot attack each other |
| 76 | return false; |
| 20 | 77 | } |
| 21 | 78 | } |
| 79 | |
| 80 | // Handled Edge Cases: queens on the same square, queens on same row, queens on same column, queens on same diagonal, invalid chessboard coordinates, queens cannot attack each other |