| 11 | 11 | black: [blackRow, blackColumn] = [], |
| 12 | 12 | white: [whiteRow, whiteColumn] = [], |
| 13 | 13 | } = {}) { |
| 14 | | throw new Error('Remove this line and implement the function'); |
| 14 | // Edge Case: Missing positions - default to [0,0] for both queens |
| 15 | if (blackRow === undefined || blackColumn === undefined) { |
| 16 | blackRow = 0; |
| 17 | blackColumn = 0; |
| 18 | } |
| 19 | |
| 20 | if (whiteRow === undefined || whiteColumn === undefined) { |
| 21 | whiteRow = 0; |
| 22 | whiteColumn = 0; |
| 23 | } |
| 24 | |
| 25 | // Edge Case: Negative coordinates |
| 26 | if (blackRow < 0 || blackColumn < 0 || whiteRow < 0 || whiteColumn < 0) { |
| 27 | throw new Error("Queen must be placed on the board"); |
| 28 | } |
| 29 | |
| 30 | // Edge Case: Coordinates outside board boundaries |
| 31 | if (blackRow > 7 || blackColumn > 7 || whiteRow > 7 || whiteColumn > 7) { |
| 32 | throw new Error("Queen must be placed on the board"); |
| 33 | } |
| 34 | |
| 35 | // Edge Case: Queens on same square |
| 36 | if (blackRow === whiteRow && blackColumn === whiteColumn) { |
| 37 | throw new Error("Queens cannot share the same space"); |
| 38 | } |
| 39 | |
| 40 | this.black = [blackRow, blackColumn]; |
| 41 | this.white = [whiteRow, whiteColumn]; |
| 15 | 42 | } |
| 16 | 43 | |
| 17 | 44 | /** |
| 18 | 45 | * @returns {string} |
| 19 | 46 | */ |
| 20 | 47 | toString() { |
| 21 | | throw new Error('Remove this line and implement the function'); |
| 48 | const board = Array(8).fill().map(() => Array(8).fill('_')); |
| 49 | |
| 50 | board[this.white[0]][this.white[1]] = 'W'; |
| 51 | board[this.black[0]][this.black[1]] = 'B'; |
| 52 | |
| 53 | return board.map(row => row.join(' ')).join('\n'); |
| 22 | 54 | } |
| 23 | 55 | |
| 24 | 56 | /** |
| 25 | 57 | * @returns {boolean} |
| 26 | 58 | */ |
| 27 | 59 | get canAttack() { |
| 28 | | throw new Error('Remove this line and implement the function'); |
| 60 | const [whiteRow, whiteColumn] = this.white; |
| 61 | const [blackRow, blackColumn] = this.black; |
| 62 | |
| 63 | // Edge Case: Queens on same row |
| 64 | if (whiteRow === blackRow) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | // Edge Case: Queens on same column |
| 69 | if (whiteColumn === blackColumn) { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | // Edge Case: Queens on same diagonal |
| 74 | const rowDiff = Math.abs(whiteRow - blackRow); |
| 75 | const colDiff = Math.abs(whiteColumn - blackColumn); |
| 76 | |
| 77 | if (rowDiff === colDiff) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | return false; |
| 29 | 82 | } |
| 30 | 83 | } |
| 84 | |
| 85 | // Handled Edge Cases: queens on same square, queens on same row, queens on same column, queens on same diagonal, negative coordinates, coordinates outside board boundaries, missing positions |