Skip to content

Commit cda0e7b

Browse files
committed
queen-attack: add to track
1 parent d677085 commit cda0e7b

6 files changed

Lines changed: 230 additions & 1 deletion

File tree

‎config.json‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"pascals-triangle",
4545
"beer-song",
4646
"difference-of-squares",
47-
"largest-series-product"
47+
"largest-series-product",
48+
"queen-attack"
4849
],
4950
"exercises": [
5051
{
@@ -251,6 +252,11 @@
251252
"slug": "largest-series-product",
252253
"difficulty": 1,
253254
"topics": []
255+
},
256+
{
257+
"slug": "queen-attack",
258+
"difficulty": 1,
259+
"topics": []
254260
}
255261
],
256262
"deprecated": [
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
test {
13+
testLogging {
14+
exceptionFormat = 'full'
15+
events = ["passed", "failed", "skipped"]
16+
}
17+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.Arrays;
2+
3+
public final class QueenAttackCalculator {
4+
5+
private final int[] queen1Coordinates;
6+
7+
private final int[] queen2Coordinates;
8+
9+
public QueenAttackCalculator(final int[] queen1Coordinates, final int[] queen2Coordinates)
10+
throws IllegalArgumentException {
11+
12+
this.queen1Coordinates = queen1Coordinates;
13+
this.queen2Coordinates = queen2Coordinates;
14+
15+
validateInputs();
16+
}
17+
18+
public boolean canQueensAttackOneAnother() {
19+
return queensShareFile() || queensShareRank() || queensShareDiagonal();
20+
}
21+
22+
private void validateInputs() throws IllegalArgumentException {
23+
if (queen1Coordinates == null
24+
|| queen2Coordinates == null
25+
|| queen1Coordinates.length != 2
26+
|| queen2Coordinates.length != 2) {
27+
28+
throw new IllegalArgumentException("You must supply valid 2-dimensional coordinate arrays for both Queens.");
29+
}
30+
31+
final int[] rankCoordinates = new int[2];
32+
rankCoordinates[0] = queen1Coordinates[0];
33+
rankCoordinates[1] = queen2Coordinates[0];
34+
Arrays.sort(rankCoordinates);
35+
36+
if (rankCoordinates[0] < 0) {
37+
throw new IllegalArgumentException("Queens must both have positive rank.");
38+
}
39+
40+
if (rankCoordinates[1] > 7) {
41+
throw new IllegalArgumentException("Queens must both have rank <= 7.");
42+
}
43+
44+
final int[] fileCoordinates = new int[2];
45+
fileCoordinates[0] = queen1Coordinates[1];
46+
fileCoordinates[1] = queen2Coordinates[1];
47+
Arrays.sort(fileCoordinates);
48+
49+
if (fileCoordinates[0] < 0) {
50+
throw new IllegalArgumentException("Queens must both have positive file.");
51+
}
52+
53+
if (fileCoordinates[1] > 7) {
54+
throw new IllegalArgumentException("Queens must both have file <= 7.");
55+
}
56+
57+
if (queensShareSquare()) {
58+
throw new IllegalArgumentException("Queens must not occupy the same square.");
59+
}
60+
}
61+
62+
private boolean queensShareRank() {
63+
return differenceBetweenRanks() == 0;
64+
}
65+
66+
private boolean queensShareFile() {
67+
return differenceBetweenFiles() == 0;
68+
}
69+
70+
private boolean queensShareSquare() {
71+
return queensShareRank() && queensShareFile();
72+
}
73+
74+
private boolean queensShareDiagonal() {
75+
return differenceBetweenRanks() == differenceBetweenFiles();
76+
}
77+
78+
private int differenceBetweenRanks() {
79+
return Math.abs(queen1Coordinates[0] - queen2Coordinates[0]);
80+
}
81+
82+
private int differenceBetweenFiles() {
83+
return Math.abs(queen1Coordinates[1] - queen2Coordinates[1]);
84+
}
85+
86+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public final class QueenAttackCalculator {
2+
3+
4+
5+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import org.junit.Ignore;
2+
import org.junit.Rule;
3+
import org.junit.Test;
4+
import org.junit.rules.ExpectedException;
5+
6+
import static org.junit.Assert.assertTrue;
7+
8+
public final class QueenAttackCalculatorTest {
9+
10+
@Rule
11+
public ExpectedException expectedException = ExpectedException.none();
12+
13+
@Test
14+
public void testNoExceptionThrownIfBothQueenPositionsValid() {
15+
new QueenAttackCalculator(new int[]{2, 2}, new int[]{0, 7});
16+
}
17+
18+
@Ignore
19+
@Test
20+
public void testNullInputNotAllowed() {
21+
expectedException.expect(IllegalArgumentException.class);
22+
expectedException.expectMessage("You must supply valid 2-dimensional coordinate arrays for both Queens.");
23+
24+
new QueenAttackCalculator(null, new int[]{3, 4});
25+
}
26+
27+
@Ignore
28+
@Test
29+
public void testNegativeRankNotAllowed() {
30+
expectedException.expect(IllegalArgumentException.class);
31+
expectedException.expectMessage("Queens must both have positive rank.");
32+
33+
new QueenAttackCalculator(new int[]{-2, 2}, new int[]{2, 2});
34+
}
35+
36+
@Ignore
37+
@Test
38+
public void testRankGreaterThan7NotAllowed() {
39+
expectedException.expect(IllegalArgumentException.class);
40+
expectedException.expectMessage("Queens must both have rank <= 7.");
41+
42+
new QueenAttackCalculator(new int[]{8, 4}, new int[]{1, 6});
43+
}
44+
45+
@Ignore
46+
@Test
47+
public void testNegativeFileNotAllowed() {
48+
expectedException.expect(IllegalArgumentException.class);
49+
expectedException.expectMessage("Queens must both have positive file.");
50+
51+
new QueenAttackCalculator(new int[]{2, -2}, new int[]{5, 3});
52+
}
53+
54+
@Ignore
55+
@Test
56+
public void testFileGreaterThan7NotAllowed() {
57+
expectedException.expect(IllegalArgumentException.class);
58+
expectedException.expectMessage("Queens must both have file <= 7.");
59+
60+
new QueenAttackCalculator(new int[]{4, 8}, new int[]{7, 4});
61+
}
62+
63+
@Ignore
64+
@Test
65+
public void testQueensMustNotOccupyTheSameSquare() {
66+
expectedException.expect(IllegalArgumentException.class);
67+
expectedException.expectMessage("Queens must not occupy the same square.");
68+
69+
new QueenAttackCalculator(new int[]{2, 2}, new int[]{2, 2});
70+
}
71+
72+
@Ignore
73+
@Test
74+
public void testQueensCanAttackOnTheSameRank() {
75+
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 4}, new int[]{2, 6});
76+
assertTrue(calculator.canQueensAttackOneAnother());
77+
}
78+
79+
@Ignore
80+
@Test
81+
public void testQueensCanAttackOnTheSameFile() {
82+
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{4, 5}, new int[]{2, 5});
83+
assertTrue(calculator.canQueensAttackOneAnother());
84+
}
85+
86+
@Ignore
87+
@Test
88+
public void testQueensCanAttackOnFirstDiagonal() {
89+
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 2}, new int[]{0, 4});
90+
assertTrue(calculator.canQueensAttackOneAnother());
91+
}
92+
93+
@Ignore
94+
@Test
95+
public void testQueensCanAttackOnSecondDiagonal() {
96+
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 2}, new int[]{3, 1});
97+
assertTrue(calculator.canQueensAttackOneAnother());
98+
}
99+
100+
@Ignore
101+
@Test
102+
public void testQueensCanAttackOnThirdDiagonal() {
103+
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 2}, new int[]{1, 1});
104+
assertTrue(calculator.canQueensAttackOneAnother());
105+
}
106+
107+
@Ignore
108+
@Test
109+
public void testQueensCanAttackOnFourthDiagonal() {
110+
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 2}, new int[]{5, 5});
111+
assertTrue(calculator.canQueensAttackOneAnother());
112+
}
113+
114+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ include 'pangram'
2525
include 'pascals-triangle'
2626
include 'phone-number'
2727
include 'pig-latin'
28+
include 'queen-attack'
2829
include 'raindrops'
2930
include 'rna-transcription'
3031
include 'robot-name'

0 commit comments

Comments
 (0)