Skip to content

Commit 2d683d8

Browse files
committed
Adding approache to Queen-Attack
1 parent e28da7b commit 2d683d8

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"introduction": {
3+
"authors": [
4+
"jagdish-15"
5+
]
6+
},
7+
"approaches": [
8+
{
9+
"uuid": "",
10+
"slug": "simple-comparison",
11+
"title": "Simple Comparison Approach",
12+
"blurb": "Use basic comparison checks to determine if queens can attack each other.",
13+
"authors": [
14+
"jagdish-15"
15+
]
16+
}
17+
]
18+
}
19+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Queen Attack Exercise
2+
3+
In this exercise, we determine if two queens on a chessboard can attack each other based on their positions. A queen in chess can move any number of squares horizontally, vertically, or diagonally. The task is to check if two queens, placed on specific coordinates, can attack each other.
4+
5+
The problem boils down to checking three conditions:
6+
1. **Same Row**: If the queens are on the same row.
7+
2. **Same Column**: If the queens are on the same column.
8+
3. **Same Diagonal**: If the queens are on the same diagonal, i.e., the absolute difference between their row and column positions is equal.
9+
10+
## Approach Overview
11+
12+
This exercise is solved using a **Simple Comparison Approach**. By comparing the row, column, and diagonal positions of the two queens, we can easily determine if they can attack each other.
13+
14+
### Why This Approach?
15+
16+
The Simple Comparison Approach is chosen for its clarity and simplicity. It uses basic comparison checks to determine if two queens are in attacking positions. This method works well for small scenarios like this one, where performance isn't a major concern.
17+
18+
For more details on the implementation of this approach, check out the [Simple Comparison Approach](https://exercism.org/tracks/java/exercises/queen-attack/approaches/simple-comparison).
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)