|
| 1 | +# Queen Attack Approach |
| 2 | + |
| 3 | +In this exercise, we determine if two queens on a chessboard can attack each other based on their positions. A queen can attack another queen if they are on the same row, column, or diagonal. |
| 4 | + |
| 5 | +## Approach Steps |
| 6 | + |
| 7 | +1. **Check if Queens are on the Same Row**: |
| 8 | + - If both queens are on the same row, they can attack each other. This is checked by comparing their row values: |
| 9 | + ```java |
| 10 | + if (queen1.getRow() == queen2.getRow()) { |
| 11 | + return true; |
| 12 | + } |
| 13 | + ``` |
| 14 | + |
| 15 | +2. **Check if Queens are on the Same Column**: |
| 16 | + - If both queens are on the same column, they can attack each other. This is checked by comparing their column values: |
| 17 | + ```java |
| 18 | + if (queen1.getColumn() == queen2.getColumn()) { |
| 19 | + return true; |
| 20 | + } |
| 21 | + ``` |
| 22 | + |
| 23 | +3. **Check if Queens are on the Same Diagonal**: |
| 24 | + - If both queens are on the same diagonal, they can attack each other. This is checked by comparing the absolute difference between their row and column values: |
| 25 | + ```java |
| 26 | + if (Math.abs(queen1.getRow() - queen2.getRow()) == Math.abs(queen1.getColumn() - queen2.getColumn())) { |
| 27 | + return true; |
| 28 | + } |
| 29 | + ``` |
| 30 | + |
| 31 | +4. **Return the Result**: |
| 32 | + - If any of the above checks return `true`, the queens can attack each other. Otherwise, they cannot. |
| 33 | + |
| 34 | +## Explanation |
| 35 | + |
| 36 | +- **Row Check**: We first check if the queens are in the same row. If they are, they can attack each other. |
| 37 | +- **Column Check**: Next, we check if the queens are in the same column. If they are, they can attack each other. |
| 38 | +- **Diagonal Check**: Lastly, we check if the queens are positioned on the same diagonal. This is determined by comparing the absolute differences in their row and column positions. |
| 39 | +- **Final Decision**: If any of these checks return `true`, the queens can attack each other. If none of these conditions are met, they cannot attack each other. |
| 40 | + |
| 41 | +## Full Code Implementation |
| 42 | + |
| 43 | +```java |
| 44 | +class QueenAttackCalculator { |
| 45 | + private final Queen queen1; |
| 46 | + private final Queen queen2; |
| 47 | + |
| 48 | + QueenAttackCalculator(Queen queen1, Queen queen2) { |
| 49 | + if (queen1 == null || queen2 == null) { |
| 50 | + throw new IllegalArgumentException("You must supply valid positions for both Queens."); |
| 51 | + } |
| 52 | + if (queen1.getRow() == queen2.getRow() && queen1.getColumn() == queen2.getColumn()) { |
| 53 | + throw new IllegalArgumentException("Queens cannot occupy the same position."); |
| 54 | + } |
| 55 | + this.queen1 = queen1; |
| 56 | + this.queen2 = queen2; |
| 57 | + } |
| 58 | + |
| 59 | + boolean canQueensAttackOneAnother() { |
| 60 | + int rowDifference = Math.abs(queen1.getRow() - queen2.getRow()); |
| 61 | + int columnDifference = Math.abs(queen1.getColumn() - queen2.getColumn()); |
| 62 | + return rowDifference == 0 || columnDifference == 0 || rowDifference == columnDifference; |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Additional Explanation for Code: |
| 68 | + |
| 69 | +1. **Constructor**: |
| 70 | + In the constructor of `QueenAttackCalculator`, we check if the queens are positioned at valid places. If either queen is `null`, or if both queens occupy the same position, an exception is thrown. The constructor takes two `Queen` objects, `queen1` and `queen2`, and stores them as instance variables. |
| 71 | + |
| 72 | +2. **Method (`canQueensAttackOneAnother`)**: |
| 73 | + |
| 74 | + - This method computes the row and column differences between the two queens and then checks: |
| 75 | + - If the row difference is zero (queens are on the same row). |
| 76 | + - If the column difference is zero (queens are on the same column). |
| 77 | + - If the row and column differences are equal (queens are on the same diagonal). |
| 78 | + - If any of these checks are true, the method returns `true`, indicating that the queens can attack each other. |
0 commit comments