-
-
Notifications
You must be signed in to change notification settings - Fork 680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding approach to Queen-Attack #2860
base: main
Are you sure you want to change the base?
Changes from all commits
303523e
80c4de0
db87438
e0f7eb5
2d683d8
d19f7bf
418ddb5
c8acf18
dd94b36
3c4e52d
31ace8a
5885dc8
ddea7e4
ec9e1e2
a4d7438
d4f1b8e
3340b82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"introduction": { | ||
"authors": [ | ||
"jagdish-15" | ||
] | ||
}, | ||
"approaches": [ | ||
{ | ||
"uuid": "b2e474c8-b778-41e7-83c0-8e41cc84af9e", | ||
"slug": "difference-comparison", | ||
"title": "Difference Comparison Approach", | ||
"blurb": "Use difference comparison checks to determine if queens can attack each other.", | ||
"authors": [ | ||
"jagdish-15" | ||
] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||||||||||
# Difference Comparison Approach | ||||||||||||||
|
||||||||||||||
```java | ||||||||||||||
class QueenAttackCalculator { | ||||||||||||||
private final Queen queen1; | ||||||||||||||
private final Queen queen2; | ||||||||||||||
|
||||||||||||||
QueenAttackCalculator(Queen queen1, Queen queen2) { | ||||||||||||||
if (queen1 == null || queen2 == null) { | ||||||||||||||
throw new IllegalArgumentException("You must supply valid positions for both Queens."); | ||||||||||||||
} | ||||||||||||||
if (queen1.getRow() == queen2.getRow() && queen1.getColumn() == queen2.getColumn()) { | ||||||||||||||
throw new IllegalArgumentException("Queens cannot occupy the same position."); | ||||||||||||||
} | ||||||||||||||
this.queen1 = queen1; | ||||||||||||||
this.queen2 = queen2; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
boolean canQueensAttackOneAnother() { | ||||||||||||||
int rowDifference = Math.abs(queen1.getRow() - queen2.getRow()); | ||||||||||||||
int columnDifference = Math.abs(queen1.getColumn() - queen2.getColumn()); | ||||||||||||||
return rowDifference == 0 || columnDifference == 0 || rowDifference == columnDifference; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
## Explanation | ||||||||||||||
|
||||||||||||||
1. **Constructor**: | ||||||||||||||
|
||||||||||||||
The constructor takes two `Queen` objects, `queen1` and `queen2`, and stores them as instance variables after validating the following conditions: | ||||||||||||||
|
||||||||||||||
- If either queen is `null`. | ||||||||||||||
- If both queens occupy the same position. | ||||||||||||||
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
If either of these conditions is true, an exception is thrown. | ||||||||||||||
|
||||||||||||||
2. **Method (`canQueensAttackOneAnother`)**: | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above, I think this is a heading? Also suggest rearranging the name to try make it read better.
Suggested change
|
||||||||||||||
|
||||||||||||||
This method calculates the row and column differences between the two queens and checks the following conditions: | ||||||||||||||
|
||||||||||||||
- If the row difference is zero (the queens are on the same row). | ||||||||||||||
- If the column difference is zero (the queens are on the same column). | ||||||||||||||
- If the row and column differences are equal (the queens are on the same diagonal). | ||||||||||||||
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
If any of these conditions are true, the method returns `true`, indicating that the queens can attack each other. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
boolean canQueensAttackOneAnother() { | ||
int rowDifference = Math.abs(queen1.getRow() - queen2.getRow()); | ||
int columnDifference = Math.abs(queen1.getColumn() - queen2.getColumn()); | ||
return rowDifference == 0 || columnDifference == 0 || rowDifference == columnDifference; | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,43 @@ | ||||||||||||||
# Introduction | ||||||||||||||
|
||||||||||||||
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. | ||||||||||||||
|
||||||||||||||
## General Guidance | ||||||||||||||
|
||||||||||||||
The problem boils down to checking three conditions: | ||||||||||||||
|
||||||||||||||
1. **Same Row**: If the queens are on the same row. | ||||||||||||||
2. **Same Column**: If the queens are on the same column. | ||||||||||||||
3. **Same Diagonal**: If the queens are on the same diagonal, i.e., the absolute difference between their row and column positions is equal. | ||||||||||||||
Comment on lines
+11
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
## Approach: Difference Comparison Approach | ||||||||||||||
|
||||||||||||||
```java | ||||||||||||||
class QueenAttackCalculator { | ||||||||||||||
private final Queen queen1; | ||||||||||||||
private final Queen queen2; | ||||||||||||||
|
||||||||||||||
QueenAttackCalculator(Queen queen1, Queen queen2) { | ||||||||||||||
if (queen1 == null || queen2 == null) { | ||||||||||||||
throw new IllegalArgumentException("You must supply valid positions for both Queens."); | ||||||||||||||
} | ||||||||||||||
if (queen1.getRow() == queen2.getRow() && queen1.getColumn() == queen2.getColumn()) { | ||||||||||||||
throw new IllegalArgumentException("Queens cannot occupy the same position."); | ||||||||||||||
} | ||||||||||||||
this.queen1 = queen1; | ||||||||||||||
this.queen2 = queen2; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
boolean canQueensAttackOneAnother() { | ||||||||||||||
int rowDifference = Math.abs(queen1.getRow() - queen2.getRow()); | ||||||||||||||
int columnDifference = Math.abs(queen1.getColumn() - queen2.getColumn()); | ||||||||||||||
return rowDifference == 0 || columnDifference == 0 || rowDifference == columnDifference; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
For more details on the implementation of this approach, check out the [Difference Comparison Approach][difference-comparison-approach]. | ||||||||||||||
|
||||||||||||||
[difference-comparison-approach]: https://exercism.org/tracks/java/exercises/queen-attack/approaches/difference-comparison |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is meant to be a sub-heading (
###
)? If so could you also fix move remove the indenting of the text in the section?