From 5e5d4440703de00fe7d30c89978e238ec407c326 Mon Sep 17 00:00:00 2001 From: jagdish-15 Date: Mon, 25 Nov 2024 05:21:43 +0530 Subject: [PATCH] Add approach for Queen-Attack (#2860) --- .../queen-attack/.approaches/config.json | 22 +++++++++ .../difference-comparison/content.md | 46 +++++++++++++++++++ .../difference-comparison/snippet.txt | 5 ++ .../queen-attack/.approaches/introduction.md | 43 +++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 exercises/practice/queen-attack/.approaches/config.json create mode 100644 exercises/practice/queen-attack/.approaches/difference-comparison/content.md create mode 100644 exercises/practice/queen-attack/.approaches/difference-comparison/snippet.txt create mode 100644 exercises/practice/queen-attack/.approaches/introduction.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..e087317ce --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/config.json @@ -0,0 +1,22 @@ +{ + "introduction": { + "authors": [ + "jagdish-15" + ], + "contributors": [ + "kahgoh", + "tasxatzial" + ] + }, + "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" + ] + } + ] +} 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..56b3cdfb6 --- /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 + +### Constructor + +The constructor takes two `Queen` objects, `queen1` and `queen2`, and stores them as instance variables after validating the following conditions: + +- Either queen is `null`. +- Both queens occupy the same position. + +If either of these conditions is true, an exception is thrown. + +### `canQueensAttackOneAnother` Method + +This method calculates the row and column differences between the two queens and checks the following conditions: + +- 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. diff --git a/exercises/practice/queen-attack/.approaches/difference-comparison/snippet.txt b/exercises/practice/queen-attack/.approaches/difference-comparison/snippet.txt new file mode 100644 index 000000000..90f224025 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/difference-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 diff --git a/exercises/practice/queen-attack/.approaches/introduction.md b/exercises/practice/queen-attack/.approaches/introduction.md new file mode 100644 index 000000000..3c2d6d765 --- /dev/null +++ b/exercises/practice/queen-attack/.approaches/introduction.md @@ -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**: 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 + +```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