Skip to content

Commit 5e5d444

Browse files
authored
Add approach for Queen-Attack (#2860)
1 parent bf8a4c3 commit 5e5d444

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"introduction": {
3+
"authors": [
4+
"jagdish-15"
5+
],
6+
"contributors": [
7+
"kahgoh",
8+
"tasxatzial"
9+
]
10+
},
11+
"approaches": [
12+
{
13+
"uuid": "b2e474c8-b778-41e7-83c0-8e41cc84af9e",
14+
"slug": "difference-comparison",
15+
"title": "Difference Comparison Approach",
16+
"blurb": "Use difference comparison checks to determine if queens can attack each other.",
17+
"authors": [
18+
"jagdish-15"
19+
]
20+
}
21+
]
22+
}
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+
### 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+
- Either queen is `null`.
34+
- Both queens occupy the same position.
35+
36+
If either of these conditions is true, an exception is thrown.
37+
38+
### `canQueensAttackOneAnother` Method
39+
40+
This method calculates the row and column differences between the two queens and checks the following conditions:
41+
42+
- The row difference is zero (the queens are on the same row).
43+
- The column difference is zero (the queens are on the same column).
44+
- 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.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
boolean canQueensAttackOneAnother() {
2+
int rowDifference = Math.abs(queen1.getRow() - queen2.getRow());
3+
int columnDifference = Math.abs(queen1.getColumn() - queen2.getColumn());
4+
return rowDifference == 0 || columnDifference == 0 || rowDifference == columnDifference;
5+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Introduction
2+
3+
In this exercise, we determine if two queens on a chessboard can attack each other based on their positions.
4+
A queen in chess can move any number of squares horizontally, vertically, or diagonally.
5+
The task is to check if two queens, placed on specific coordinates, can attack each other.
6+
7+
## General Guidance
8+
9+
The problem boils down to checking three conditions:
10+
11+
1. **Same Row**: The queens are on the same row.
12+
2. **Same Column**: The queens are on the same column.
13+
3. **Same Diagonal**: The queens are on the same diagonal, i.e., the absolute difference between their row and column positions is equal.
14+
15+
## Approach: Difference Comparison Approach
16+
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+
```
40+
41+
For more details on the implementation of this approach, check out the [Difference Comparison Approach][difference-comparison-approach].
42+
43+
[difference-comparison-approach]: https://exercism.org/tracks/java/exercises/queen-attack/approaches/difference-comparison

0 commit comments

Comments
 (0)