From 303523eb47cc4cfeb8ac4e5b14fe175942fbd314 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 00:21:35 +0530 Subject: [PATCH 01/16] Adding approache to Queen-Attack --- .../queen-attack/.approaches/config.json | 19 +++++ .../queen-attack/.approaches/introduction.md | 18 +++++ .../.approaches/simple-comparison/content.md | 78 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 exercises/practice/queen-attack/.approaches/config.json create mode 100644 exercises/practice/queen-attack/.approaches/introduction.md create mode 100644 exercises/practice/queen-attack/.approaches/simple-comparison/content.md diff --git a/exercises/practice/queen-attack/.approaches/config.json b/exercises/practice/queen-attack/.approaches/config.json new file mode 100644 index 000000000..20b951ff8 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/config.json @@ -0,0 +1,19 @@ +{ + "introduction": { + "authors": [ + "jagdish-15" + ] + }, + "approaches": [ + { + "uuid": "", + "slug": "simple-comparison", + "title": "Simple Comparison Approach", + "blurb": "Use basic comparison checks to determine if queens can attack each other.", + "authors": [ + "jagdish-15" + ] + } + ] + } + \ No newline at end of file diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md new file mode 100644 index 000000000..06ab7be07 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -0,0 +1,18 @@ +# 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. + +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. + +## Approach Overview + +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. + +### Why This Approach? + +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. + +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). diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md new file mode 100644 index 000000000..bc5a5c5b6 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md @@ -0,0 +1,78 @@ +# Queen Attack Approach + +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. + +## 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; + } + ``` + +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. + +## Full Code Implementation + +```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; + } +} +``` + +## Additional Explanation for Code: + +1. **Constructor**: + 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. + +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. From 80c4de01f687e8299fe0599e3af1dbc3c9f36a40 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 00:29:25 +0530 Subject: [PATCH 02/16] Fixing style errors --- exercises/practice/queen-attack/.approaches/introduction.md | 1 + .../queen-attack/.approaches/simple-comparison/content.md | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index 06ab7be07..0f1eb1983 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -3,6 +3,7 @@ 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. 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. diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md index bc5a5c5b6..fa9fcb1a4 100644 --- a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md @@ -6,6 +6,7 @@ In this exercise, we determine if two queens on a chessboard can attack each oth 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; @@ -14,6 +15,7 @@ In this exercise, we determine if two queens on a chessboard can attack each oth 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; @@ -22,6 +24,7 @@ In this exercise, we determine if two queens on a chessboard can attack each oth 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; @@ -64,7 +67,7 @@ class QueenAttackCalculator { } ``` -## Additional Explanation for Code: +## Additional Explanation for Code 1. **Constructor**: 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. From db874386b4be0147b35da579748341a805fb450a Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 00:30:59 +0530 Subject: [PATCH 03/16] Fixing style errors --- .../queen-attack/.approaches/simple-comparison/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md index fa9fcb1a4..efc3dbe00 100644 --- a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md @@ -69,7 +69,7 @@ class QueenAttackCalculator { ## Additional Explanation for Code -1. **Constructor**: +1. **Constructor**: 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. 2. **Method (`canQueensAttackOneAnother`)**: From e0f7eb53db7b8581314958e20398d469da450d92 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 23:20:40 +0530 Subject: [PATCH 04/16] Adding uuid for approach of queen-attack exercise --- .../queen-attack/.approaches/config.json | 29 +++++++++---------- .../.approaches/simple-comparison/snippet.txt | 5 ++++ 2 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt diff --git a/exercises/practice/queen-attack/.approaches/config.json b/exercises/practice/queen-attack/.approaches/config.json index 20b951ff8..fbfa109a1 100644 --- a/exercises/practice/queen-attack/.approaches/config.json +++ b/exercises/practice/queen-attack/.approaches/config.json @@ -1,19 +1,18 @@ { - "introduction": { + "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" ] - }, - "approaches": [ - { - "uuid": "", - "slug": "simple-comparison", - "title": "Simple Comparison Approach", - "blurb": "Use basic comparison checks to determine if queens can attack each other.", - "authors": [ - "jagdish-15" - ] - } - ] - } - \ No newline at end of file + } + ] +} diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt b/exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt new file mode 100644 index 000000000..90f224025 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt @@ -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; +} \ No newline at end of file From 2d683d86f80669446498d60cf328942bee6f2fec Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 00:21:35 +0530 Subject: [PATCH 05/16] Adding approache to Queen-Attack --- .../queen-attack/.approaches/config.json | 19 +++++ .../queen-attack/.approaches/introduction.md | 18 +++++ .../.approaches/simple-comparison/content.md | 78 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 exercises/practice/queen-attack/.approaches/config.json create mode 100644 exercises/practice/queen-attack/.approaches/introduction.md create mode 100644 exercises/practice/queen-attack/.approaches/simple-comparison/content.md diff --git a/exercises/practice/queen-attack/.approaches/config.json b/exercises/practice/queen-attack/.approaches/config.json new file mode 100644 index 000000000..20b951ff8 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/config.json @@ -0,0 +1,19 @@ +{ + "introduction": { + "authors": [ + "jagdish-15" + ] + }, + "approaches": [ + { + "uuid": "", + "slug": "simple-comparison", + "title": "Simple Comparison Approach", + "blurb": "Use basic comparison checks to determine if queens can attack each other.", + "authors": [ + "jagdish-15" + ] + } + ] + } + \ No newline at end of file diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md new file mode 100644 index 000000000..06ab7be07 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -0,0 +1,18 @@ +# 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. + +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. + +## Approach Overview + +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. + +### Why This Approach? + +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. + +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). diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md new file mode 100644 index 000000000..bc5a5c5b6 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md @@ -0,0 +1,78 @@ +# Queen Attack Approach + +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. + +## 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; + } + ``` + +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. + +## Full Code Implementation + +```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; + } +} +``` + +## Additional Explanation for Code: + +1. **Constructor**: + 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. + +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. From d19f7bf1a518d5986ccc957bc192b269de537fdc Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 00:29:25 +0530 Subject: [PATCH 06/16] Fixing style errors --- exercises/practice/queen-attack/.approaches/introduction.md | 1 + .../queen-attack/.approaches/simple-comparison/content.md | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index 06ab7be07..0f1eb1983 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -3,6 +3,7 @@ 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. 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. diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md index bc5a5c5b6..fa9fcb1a4 100644 --- a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md @@ -6,6 +6,7 @@ In this exercise, we determine if two queens on a chessboard can attack each oth 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; @@ -14,6 +15,7 @@ In this exercise, we determine if two queens on a chessboard can attack each oth 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; @@ -22,6 +24,7 @@ In this exercise, we determine if two queens on a chessboard can attack each oth 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; @@ -64,7 +67,7 @@ class QueenAttackCalculator { } ``` -## Additional Explanation for Code: +## Additional Explanation for Code 1. **Constructor**: 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. From 418ddb57a4e531724b1511b7d960287864769ee1 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 10 Nov 2024 00:30:59 +0530 Subject: [PATCH 07/16] Fixing style errors --- .../queen-attack/.approaches/simple-comparison/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md index fa9fcb1a4..efc3dbe00 100644 --- a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md @@ -69,7 +69,7 @@ class QueenAttackCalculator { ## Additional Explanation for Code -1. **Constructor**: +1. **Constructor**: 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. 2. **Method (`canQueensAttackOneAnother`)**: From c8acf184511ae4d7e1be1f02a71d92ca10060149 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Thu, 14 Nov 2024 23:20:40 +0530 Subject: [PATCH 08/16] Adding uuid for approach of queen-attack exercise --- .../queen-attack/.approaches/config.json | 29 +++++++++---------- .../.approaches/simple-comparison/snippet.txt | 5 ++++ 2 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt diff --git a/exercises/practice/queen-attack/.approaches/config.json b/exercises/practice/queen-attack/.approaches/config.json index 20b951ff8..fbfa109a1 100644 --- a/exercises/practice/queen-attack/.approaches/config.json +++ b/exercises/practice/queen-attack/.approaches/config.json @@ -1,19 +1,18 @@ { - "introduction": { + "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" ] - }, - "approaches": [ - { - "uuid": "", - "slug": "simple-comparison", - "title": "Simple Comparison Approach", - "blurb": "Use basic comparison checks to determine if queens can attack each other.", - "authors": [ - "jagdish-15" - ] - } - ] - } - \ No newline at end of file + } + ] +} diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt b/exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt new file mode 100644 index 000000000..90f224025 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt @@ -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; +} \ No newline at end of file From dd94b36ce5cce1e4016d3dba6f97fddff294bbcb Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Fri, 15 Nov 2024 23:14:09 +0530 Subject: [PATCH 09/16] Fixing minor issues --- .../queen-attack/.approaches/introduction.md | 19 +++++-- .../.approaches/simple-comparison/content.md | 55 +++++++++---------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index 0f1eb1983..48cede57a 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -1,6 +1,10 @@ # 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. +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 The problem boils down to checking three conditions: @@ -8,12 +12,17 @@ The problem boils down to checking three conditions: 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. -## Approach Overview +## Approach: Simple Comparison Approach -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. +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. ### Why This Approach? -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. +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. + +For more details on the implementation of this approach, check out the [Simple Comparison Approach][simple-comparison-approach]. -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). +[simple-comparison-approach]: https://exercism.org/tracks/java/exercises/queen-attack/approaches/simple-comparison diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md index efc3dbe00..a29d18e92 100644 --- a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md @@ -1,6 +1,28 @@ # Queen Attack Approach -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. +```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 @@ -41,36 +63,13 @@ In this exercise, we determine if two queens on a chessboard can attack each oth - **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. -## Full Code Implementation - -```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; - } -} -``` - ## Additional Explanation for Code 1. **Constructor**: - 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. + + 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. 2. **Method (`canQueensAttackOneAnother`)**: From 31ace8aecd9fc8720f97118628ad7cceed18c018 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Fri, 15 Nov 2024 23:20:56 +0530 Subject: [PATCH 10/16] Fixing styling issues --- exercises/practice/queen-attack/.approaches/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index 48cede57a..5fbfaefb9 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -4,7 +4,7 @@ In this exercise, we determine if two queens on a chessboard can attack each oth 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 +## Genral Advice The problem boils down to checking three conditions: From 5885dc83279e30a31db7e858c56b3d14e5c59806 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 19 Nov 2024 00:42:36 +0530 Subject: [PATCH 11/16] Chnaging the introduction.md for consistancy --- .../queen-attack/.approaches/introduction.md | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index 5fbfaefb9..bec2a4d29 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -14,14 +14,29 @@ The problem boils down to checking three conditions: ## Approach: Simple Comparison Approach -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. - -### Why This Approach? - -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. +```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 [Simple Comparison Approach][simple-comparison-approach]. From ddea7e44d2b8c798d62f36f98bb3315e548ea783 Mon Sep 17 00:00:00 2001 From: jagdish-15 Date: Tue, 19 Nov 2024 22:23:07 +0530 Subject: [PATCH 12/16] Update exercises/practice/queen-attack/.approaches/introduction.md Co-authored-by: Kah Goh --- exercises/practice/queen-attack/.approaches/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index bec2a4d29..c6a4c77c3 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -1,4 +1,4 @@ -# Queen Attack Exercise +# 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. From ec9e1e22316b4d1c02d7a63be660db9babb3c8f0 Mon Sep 17 00:00:00 2001 From: jagdish-15 Date: Tue, 19 Nov 2024 22:23:18 +0530 Subject: [PATCH 13/16] Update exercises/practice/queen-attack/.approaches/introduction.md Co-authored-by: Kah Goh --- exercises/practice/queen-attack/.approaches/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index c6a4c77c3..460d792bf 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -4,7 +4,7 @@ In this exercise, we determine if two queens on a chessboard can attack each oth 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 +## General Guidance The problem boils down to checking three conditions: From a4d74388f79f8c55a21385659ad88c555803cb0f Mon Sep 17 00:00:00 2001 From: jagdish-15 Date: Tue, 19 Nov 2024 22:26:12 +0530 Subject: [PATCH 14/16] Update exercises/practice/queen-attack/.approaches/simple-comparison/content.md Co-authored-by: Kah Goh --- .../queen-attack/.approaches/simple-comparison/content.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md index a29d18e92..b996933bb 100644 --- a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md +++ b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md @@ -67,7 +67,7 @@ class QueenAttackCalculator { 1. **Constructor**: - In the constructor of `QueenAttackCalculator`, we check if the queens are positioned at valid places. + The constructor of `QueenAttackCalculator` takes two `Queen` objects and checks if they 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. From d4f1b8e66194c09f066184bc52f351ec54b06912 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Tue, 19 Nov 2024 23:17:14 +0530 Subject: [PATCH 15/16] Changing the name of the approach for queen-attack exercise and updating the content for the same --- .../queen-attack/.approaches/config.json | 6 +- .../difference-comparison/content.md | 46 +++++++++++ .../snippet.txt | 0 .../queen-attack/.approaches/introduction.md | 6 +- .../.approaches/simple-comparison/content.md | 80 ------------------- 5 files changed, 52 insertions(+), 86 deletions(-) create mode 100644 exercises/practice/queen-attack/.approaches/difference-comparison/content.md rename exercises/practice/queen-attack/.approaches/{simple-comparison => difference-comparison}/snippet.txt (100%) delete mode 100644 exercises/practice/queen-attack/.approaches/simple-comparison/content.md diff --git a/exercises/practice/queen-attack/.approaches/config.json b/exercises/practice/queen-attack/.approaches/config.json index fbfa109a1..fd78dc118 100644 --- a/exercises/practice/queen-attack/.approaches/config.json +++ b/exercises/practice/queen-attack/.approaches/config.json @@ -7,9 +7,9 @@ "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.", + "slug": "difference-comparison", + "title": "Difference Comparison Approach", + "blurb": "Use difference comparison checks to determine if queens can attack each other.", "authors": [ "jagdish-15" ] diff --git a/exercises/practice/queen-attack/.approaches/difference-comparison/content.md b/exercises/practice/queen-attack/.approaches/difference-comparison/content.md new file mode 100644 index 000000000..a33e93c66 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/difference-comparison/content.md @@ -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. + + If either of these conditions is true, an exception is thrown. + +2. **Method (`canQueensAttackOneAnother`)**: + + 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). + + If any of these conditions are true, the method returns `true`, indicating that the queens can attack each other. diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt b/exercises/practice/queen-attack/.approaches/difference-comparison/snippet.txt similarity index 100% rename from exercises/practice/queen-attack/.approaches/simple-comparison/snippet.txt rename to exercises/practice/queen-attack/.approaches/difference-comparison/snippet.txt diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index bec2a4d29..f4fe1e185 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -12,7 +12,7 @@ The problem boils down to checking three conditions: 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. -## Approach: Simple Comparison Approach +## Approach: Difference Comparison Approach ```java class QueenAttackCalculator { @@ -38,6 +38,6 @@ class QueenAttackCalculator { } ``` -For more details on the implementation of this approach, check out the [Simple Comparison Approach][simple-comparison-approach]. +For more details on the implementation of this approach, check out the [Difference Comparison Approach][difference-comparison-approach]. -[simple-comparison-approach]: https://exercism.org/tracks/java/exercises/queen-attack/approaches/simple-comparison +[difference-comparison-approach]: https://exercism.org/tracks/java/exercises/queen-attack/approaches/difference-comparison diff --git a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md b/exercises/practice/queen-attack/.approaches/simple-comparison/content.md deleted file mode 100644 index a29d18e92..000000000 --- a/exercises/practice/queen-attack/.approaches/simple-comparison/content.md +++ /dev/null @@ -1,80 +0,0 @@ -# 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; - } - ``` - -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 - -1. **Constructor**: - - 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. - -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. From 7f4e9808d68349a341c8937498fede2aec8012fa Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sat, 23 Nov 2024 20:20:51 +0530 Subject: [PATCH 16/16] Updating the approach for queen-attack after feedback --- .../queen-attack/.approaches/config.json | 4 ++++ .../difference-comparison/content.md | 22 +++++++++---------- .../queen-attack/.approaches/introduction.md | 6 ++--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/exercises/practice/queen-attack/.approaches/config.json b/exercises/practice/queen-attack/.approaches/config.json index fd78dc118..e087317ce 100644 --- a/exercises/practice/queen-attack/.approaches/config.json +++ b/exercises/practice/queen-attack/.approaches/config.json @@ -2,6 +2,10 @@ "introduction": { "authors": [ "jagdish-15" + ], + "contributors": [ + "kahgoh", + "tasxatzial" ] }, "approaches": [ diff --git a/exercises/practice/queen-attack/.approaches/difference-comparison/content.md b/exercises/practice/queen-attack/.approaches/difference-comparison/content.md index a33e93c66..56b3cdfb6 100644 --- a/exercises/practice/queen-attack/.approaches/difference-comparison/content.md +++ b/exercises/practice/queen-attack/.approaches/difference-comparison/content.md @@ -26,21 +26,21 @@ class QueenAttackCalculator { ## Explanation -1. **Constructor**: +### Constructor - The constructor takes two `Queen` objects, `queen1` and `queen2`, and stores them as instance variables after validating the following conditions: +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. +- Either queen is `null`. +- Both queens occupy the same position. - If either of these conditions is true, an exception is thrown. +If either of these conditions is true, an exception is thrown. -2. **Method (`canQueensAttackOneAnother`)**: +### `canQueensAttackOneAnother` Method - This method calculates the row and column differences between the two queens and checks the following conditions: +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). +- The row difference is zero (the queens are on the same row). +- The column difference is zero (the queens are on the same column). +- The row and column differences are equal (the queens are on the same diagonal). - If any of these conditions are true, the method returns `true`, indicating that the queens can attack each other. +If any of these conditions are true, the method returns `true`, indicating that the queens can attack each other. diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md index c0e02d337..3c2d6d765 100644 --- a/exercises/practice/queen-attack/.approaches/introduction.md +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -8,9 +8,9 @@ The task is to check if two queens, placed on specific coordinates, can attack e 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**: 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. ## Approach: Difference Comparison Approach