Skip to content

Commit d4f1b8e

Browse files
committed
Changing the name of the approach for queen-attack exercise and updating the content for the same
1 parent 5885dc8 commit d4f1b8e

File tree

5 files changed

+52
-86
lines changed

5 files changed

+52
-86
lines changed

exercises/practice/queen-attack/.approaches/config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"approaches": [
88
{
99
"uuid": "b2e474c8-b778-41e7-83c0-8e41cc84af9e",
10-
"slug": "simple-comparison",
11-
"title": "Simple Comparison Approach",
12-
"blurb": "Use basic comparison checks to determine if queens can attack each other.",
10+
"slug": "difference-comparison",
11+
"title": "Difference Comparison Approach",
12+
"blurb": "Use difference comparison checks to determine if queens can attack each other.",
1313
"authors": [
1414
"jagdish-15"
1515
]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Difference Comparison Approach
2+
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+
```
26+
27+
## Explanation
28+
29+
1. **Constructor**:
30+
31+
The constructor takes two `Queen` objects, `queen1` and `queen2`, and stores them as instance variables after validating the following conditions:
32+
33+
- If either queen is `null`.
34+
- If both queens occupy the same position.
35+
36+
If either of these conditions is true, an exception is thrown.
37+
38+
2. **Method (`canQueensAttackOneAnother`)**:
39+
40+
This method calculates the row and column differences between the two queens and checks the following conditions:
41+
42+
- If the row difference is zero (the queens are on the same row).
43+
- If the column difference is zero (the queens are on the same column).
44+
- If the row and column differences are equal (the queens are on the same diagonal).
45+
46+
If any of these conditions are true, the method returns `true`, indicating that the queens can attack each other.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The problem boils down to checking three conditions:
1212
2. **Same Column**: If the queens are on the same column.
1313
3. **Same Diagonal**: If the queens are on the same diagonal, i.e., the absolute difference between their row and column positions is equal.
1414

15-
## Approach: Simple Comparison Approach
15+
## Approach: Difference Comparison Approach
1616

1717
```java
1818
class QueenAttackCalculator {
@@ -38,6 +38,6 @@ class QueenAttackCalculator {
3838
}
3939
```
4040

41-
For more details on the implementation of this approach, check out the [Simple Comparison Approach][simple-comparison-approach].
41+
For more details on the implementation of this approach, check out the [Difference Comparison Approach][difference-comparison-approach].
4242

43-
[simple-comparison-approach]: https://exercism.org/tracks/java/exercises/queen-attack/approaches/simple-comparison
43+
[difference-comparison-approach]: https://exercism.org/tracks/java/exercises/queen-attack/approaches/difference-comparison

exercises/practice/queen-attack/.approaches/simple-comparison/content.md

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)