Skip to content

Commit dd94b36

Browse files
committed
Fixing minor issues
1 parent e0f7eb5 commit dd94b36

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
# Queen Attack Exercise
22

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.
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+
## Genral Advice
48

59
The problem boils down to checking three conditions:
610

711
1. **Same Row**: If the queens are on the same row.
812
2. **Same Column**: If the queens are on the same column.
913
3. **Same Diagonal**: If the queens are on the same diagonal, i.e., the absolute difference between their row and column positions is equal.
1014

11-
## Approach Overview
15+
## Approach: Simple Comparison Approach
1216

13-
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.
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.
1419

1520
### Why This Approach?
1621

17-
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.
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.
25+
26+
For more details on the implementation of this approach, check out the [Simple Comparison Approach][simple-comparison-approach].
1827

19-
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).
28+
[simple-comparison-approach]: https://exercism.org/tracks/java/exercises/queen-attack/approaches/simple-comparison

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

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
# Queen Attack Approach
22

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+
```
426

527
## Approach Steps
628

@@ -41,36 +63,13 @@ In this exercise, we determine if two queens on a chessboard can attack each oth
4163
- **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.
4264
- **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.
4365

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-
7066
## Additional Explanation for Code
7167

7268
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.
7473

7574
2. **Method (`canQueensAttackOneAnother`)**:
7675

0 commit comments

Comments
 (0)