-
-
Notifications
You must be signed in to change notification settings - Fork 738
Adding approach to Queen-Attack #2860
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
303523e
Adding approache to Queen-Attack
jagdish-15 80c4de0
Fixing style errors
jagdish-15 db87438
Fixing style errors
jagdish-15 e0f7eb5
Adding uuid for approach of queen-attack exercise
jagdish-15 2d683d8
Adding approache to Queen-Attack
jagdish-15 d19f7bf
Fixing style errors
jagdish-15 418ddb5
Fixing style errors
jagdish-15 c8acf18
Adding uuid for approach of queen-attack exercise
jagdish-15 dd94b36
Fixing minor issues
jagdish-15 3c4e52d
Merging with latest changes
jagdish-15 31ace8a
Fixing styling issues
jagdish-15 5885dc8
Chnaging the introduction.md for consistancy
jagdish-15 ddea7e4
Update exercises/practice/queen-attack/.approaches/introduction.md
jagdish-15 ec9e1e2
Update exercises/practice/queen-attack/.approaches/introduction.md
jagdish-15 a4d7438
Update exercises/practice/queen-attack/.approaches/simple-comparison/…
jagdish-15 d4f1b8e
Changing the name of the approach for queen-attack exercise and updat…
jagdish-15 3340b82
Updating introduction for approaches of queen-attack
jagdish-15 0715df7
Merge branch 'main' into add-approach-queen-attack
jagdish-15 7f4e980
Updating the approach for queen-attack after feedback
jagdish-15 b665c33
Merge branch 'add-approach-queen-attack' of https://github.com/jagdis…
jagdish-15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": "simple-comparison", | ||
| "title": "Simple Comparison Approach", | ||
| "blurb": "Use basic comparison checks to determine if queens can attack each other.", | ||
| "authors": [ | ||
| "jagdish-15" | ||
| ] | ||
| } | ||
| ] | ||
| } |
43 changes: 43 additions & 0 deletions
43
exercises/practice/queen-attack/.approaches/introduction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||||||||||
| # Queen Attack Exercise | ||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
|
|
||||||||||||||
| ## Genral Advice | ||||||||||||||
jagdish-15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| 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. | ||||||||||||||
|
||||||||||||||
| 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. | |
| 1. **Same Row**: The queens are on the same row. | |
| 2. **Same Column**: The queens are on the same column. | |
| 3. **Same Diagonal**: The queens are on the same diagonal, i.e., the absolute difference between their row and column positions is equal. |
jagdish-15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jagdish-15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
80 changes: 80 additions & 0 deletions
80
exercises/practice/queen-attack/.approaches/simple-comparison/content.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Queen Attack 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; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Approach Steps | ||
|
|
||
| 1. **Check if Queens are on the Same Row**: | ||
| - If both queens are on the same row, they can attack each other. This is checked by comparing their row values: | ||
|
|
||
| ```java | ||
| if (queen1.getRow() == queen2.getRow()) { | ||
| return true; | ||
| } | ||
jagdish-15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| 2. **Check if Queens are on the Same Column**: | ||
| - If both queens are on the same column, they can attack each other. This is checked by comparing their column values: | ||
|
|
||
| ```java | ||
| if (queen1.getColumn() == queen2.getColumn()) { | ||
| return true; | ||
| } | ||
| ``` | ||
|
|
||
| 3. **Check if Queens are on the Same Diagonal**: | ||
| - 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: | ||
|
|
||
| ```java | ||
| if (Math.abs(queen1.getRow() - queen2.getRow()) == Math.abs(queen1.getColumn() - queen2.getColumn())) { | ||
| return true; | ||
| } | ||
| ``` | ||
|
|
||
| 4. **Return the Result**: | ||
| - If any of the above checks return `true`, the queens can attack each other. Otherwise, they cannot. | ||
|
|
||
| ## Explanation | ||
|
|
||
| - **Row Check**: We first check if the queens are in the same row. If they are, they can attack each other. | ||
| - **Column Check**: Next, we check if the queens are in the same column. If they are, they can attack each other. | ||
| - **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. | ||
| - **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. | ||
|
|
||
| ## Additional Explanation for Code | ||
|
|
||
jagdish-15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 1. **Constructor**: | ||
|
|
||
| In the constructor of `QueenAttackCalculator`, we check if the queens are positioned at valid places. | ||
jagdish-15 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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. | ||
|
|
||
| 2. **Method (`canQueensAttackOneAnother`)**: | ||
|
|
||
| - This method computes the row and column differences between the two queens and then checks: | ||
| - If the row difference is zero (queens are on the same row). | ||
| - If the column difference is zero (queens are on the same column). | ||
| - If the row and column differences are equal (queens are on the same diagonal). | ||
| - If any of these checks are true, the method returns `true`, indicating that the queens can attack each other. | ||
5 changes: 5 additions & 0 deletions
5
exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.