|
1 | 1 | # Queen Attack Approach |
2 | 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. |
| 3 | +```java |
| 4 | +class QueenAttackCalculator { |
| 5 | + private final Queen queen1; |
| 6 | + private final Queen queen2; |
| 7 | + |
| 8 | + QueenAttackCalculator(Queen queen1, Queen queen2) { |
| 9 | + if (queen1 == null || queen2 == null) { |
| 10 | + throw new IllegalArgumentException("You must supply valid positions for both Queens."); |
| 11 | + } |
| 12 | + if (queen1.getRow() == queen2.getRow() && queen1.getColumn() == queen2.getColumn()) { |
| 13 | + throw new IllegalArgumentException("Queens cannot occupy the same position."); |
| 14 | + } |
| 15 | + this.queen1 = queen1; |
| 16 | + this.queen2 = queen2; |
| 17 | + } |
| 18 | + |
| 19 | + boolean canQueensAttackOneAnother() { |
| 20 | + int rowDifference = Math.abs(queen1.getRow() - queen2.getRow()); |
| 21 | + int columnDifference = Math.abs(queen1.getColumn() - queen2.getColumn()); |
| 22 | + return rowDifference == 0 || columnDifference == 0 || rowDifference == columnDifference; |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
4 | 26 |
|
5 | 27 | ## Approach Steps |
6 | 28 |
|
@@ -41,36 +63,13 @@ In this exercise, we determine if two queens on a chessboard can attack each oth |
41 | 63 | - **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. |
42 | 64 | - **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. |
43 | 65 |
|
44 | | -## Full Code Implementation |
45 | | - |
46 | | -```java |
47 | | -class QueenAttackCalculator { |
48 | | - private final Queen queen1; |
49 | | - private final Queen queen2; |
50 | | - |
51 | | - QueenAttackCalculator(Queen queen1, Queen queen2) { |
52 | | - if (queen1 == null || queen2 == null) { |
53 | | - throw new IllegalArgumentException("You must supply valid positions for both Queens."); |
54 | | - } |
55 | | - if (queen1.getRow() == queen2.getRow() && queen1.getColumn() == queen2.getColumn()) { |
56 | | - throw new IllegalArgumentException("Queens cannot occupy the same position."); |
57 | | - } |
58 | | - this.queen1 = queen1; |
59 | | - this.queen2 = queen2; |
60 | | - } |
61 | | - |
62 | | - boolean canQueensAttackOneAnother() { |
63 | | - int rowDifference = Math.abs(queen1.getRow() - queen2.getRow()); |
64 | | - int columnDifference = Math.abs(queen1.getColumn() - queen2.getColumn()); |
65 | | - return rowDifference == 0 || columnDifference == 0 || rowDifference == columnDifference; |
66 | | - } |
67 | | -} |
68 | | -``` |
69 | | - |
70 | 66 | ## Additional Explanation for Code |
71 | 67 |
|
72 | 68 | 1. **Constructor**: |
73 | | - 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. |
| 69 | + |
| 70 | + In the constructor of `QueenAttackCalculator`, we check if the queens are positioned at valid places. |
| 71 | + If either queen is `null`, or if both queens occupy the same position, an exception is thrown. |
| 72 | + The constructor takes two `Queen` objects, `queen1` and `queen2`, and stores them as instance variables. |
74 | 73 |
|
75 | 74 | 2. **Method (`canQueensAttackOneAnother`)**: |
76 | 75 |
|
|
0 commit comments