Skip to content

Commit 5885dc8

Browse files
committed
Chnaging the introduction.md for consistancy
1 parent 31ace8a commit 5885dc8

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

exercises/practice/queen-attack/.approaches/introduction.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,29 @@ The problem boils down to checking three conditions:
1414

1515
## Approach: Simple Comparison Approach
1616

17-
This exercise is solved using a **Simple Comparison Approach**.
18-
By comparing the row, column, and diagonal positions of the two queens, we can easily determine if they can attack each other.
19-
20-
### Why This Approach?
21-
22-
The Simple Comparison Approach is chosen for its clarity and simplicity.
23-
It uses basic comparison checks to determine if two queens are in attacking positions.
24-
This method works well for small scenarios like this one, where performance isn't a major concern.
17+
```java
18+
class QueenAttackCalculator {
19+
private final Queen queen1;
20+
private final Queen queen2;
21+
22+
QueenAttackCalculator(Queen queen1, Queen queen2) {
23+
if (queen1 == null || queen2 == null) {
24+
throw new IllegalArgumentException("You must supply valid positions for both Queens.");
25+
}
26+
if (queen1.getRow() == queen2.getRow() && queen1.getColumn() == queen2.getColumn()) {
27+
throw new IllegalArgumentException("Queens cannot occupy the same position.");
28+
}
29+
this.queen1 = queen1;
30+
this.queen2 = queen2;
31+
}
32+
33+
boolean canQueensAttackOneAnother() {
34+
int rowDifference = Math.abs(queen1.getRow() - queen2.getRow());
35+
int columnDifference = Math.abs(queen1.getColumn() - queen2.getColumn());
36+
return rowDifference == 0 || columnDifference == 0 || rowDifference == columnDifference;
37+
}
38+
}
39+
```
2540

2641
For more details on the implementation of this approach, check out the [Simple Comparison Approach][simple-comparison-approach].
2742

0 commit comments

Comments
 (0)