Skip to content

Commit 5f83bc9

Browse files
authored
Merge pull request exercism#171 from stkent/queen-attack
queen-attack: add to track
2 parents d677085 + 4ed70af commit 5f83bc9

8 files changed

Lines changed: 263 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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public final class BoardCoordinate {
2+
3+
private final int rank;
4+
5+
private final int file;
6+
7+
public BoardCoordinate(final int rank, final int file) throws IllegalArgumentException {
8+
this.rank = rank;
9+
this.file = file;
10+
11+
validateInputs();
12+
}
13+
14+
public int getRank() {
15+
return rank;
16+
}
17+
18+
public int getFile() {
19+
return file;
20+
}
21+
22+
private void validateInputs() throws IllegalArgumentException {
23+
validateCoordinateComponent(rank, "rank");
24+
validateCoordinateComponent(file, "file");
25+
}
26+
27+
private void validateCoordinateComponent(final int value, final String componentName)
28+
throws IllegalArgumentException {
29+
30+
if (value < 0) {
31+
throw new IllegalArgumentException("Coordinate must have positive " + componentName + ".");
32+
}
33+
34+
if (value > 7) {
35+
throw new IllegalArgumentException("Coordinate must have " + componentName + " <= 7.");
36+
}
37+
}
38+
39+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public final class QueenAttackCalculator {
2+
3+
private final BoardCoordinate whiteQueenCoordinate;
4+
5+
private final BoardCoordinate blackQueenCoordinate;
6+
7+
public QueenAttackCalculator(final BoardCoordinate whiteQueenCoordinate, final BoardCoordinate blackQueenCoordinate)
8+
throws IllegalArgumentException {
9+
10+
this.whiteQueenCoordinate = whiteQueenCoordinate;
11+
this.blackQueenCoordinate = blackQueenCoordinate;
12+
13+
validateInputs();
14+
}
15+
16+
public boolean canQueensAttackOneAnother() {
17+
return queensShareFile() || queensShareRank() || queensShareDiagonal();
18+
}
19+
20+
private void validateInputs() throws IllegalArgumentException {
21+
if (whiteQueenCoordinate == null || blackQueenCoordinate == null) {
22+
throw new IllegalArgumentException("You must supply valid board coordinates for both Queens.");
23+
}
24+
25+
if (queensShareBoardCoordinate()) {
26+
throw new IllegalArgumentException("Queens may not occupy the same board coordinate.");
27+
}
28+
}
29+
30+
private boolean queensShareRank() {
31+
return differenceBetweenRanks() == 0;
32+
}
33+
34+
private boolean queensShareFile() {
35+
return differenceBetweenFiles() == 0;
36+
}
37+
38+
private boolean queensShareBoardCoordinate() {
39+
return queensShareRank() && queensShareFile();
40+
}
41+
42+
private boolean queensShareDiagonal() {
43+
return differenceBetweenRanks() == differenceBetweenFiles();
44+
}
45+
46+
private int differenceBetweenRanks() {
47+
return Math.abs(whiteQueenCoordinate.getRank() - blackQueenCoordinate.getRank());
48+
}
49+
50+
private int differenceBetweenFiles() {
51+
return Math.abs(whiteQueenCoordinate.getFile() - blackQueenCoordinate.getFile());
52+
}
53+
54+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public final class BoardCoordinate {
2+
3+
4+
5+
}
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: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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.assertFalse;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public final class QueenAttackCalculatorTest {
10+
11+
/*
12+
* See https://github.com/junit-team/junit4/wiki/Rules for information on JUnit Rules in general and
13+
* ExpectedExceptions in particular.
14+
*/
15+
@Rule
16+
public ExpectedException expectedException = ExpectedException.none();
17+
18+
@Ignore
19+
@Test
20+
public void testCoordinateWithNegativeRankNotAllowed() {
21+
expectedException.expect(IllegalArgumentException.class);
22+
expectedException.expectMessage("Coordinate must have positive rank.");
23+
24+
new BoardCoordinate(-2, 2);
25+
}
26+
27+
@Ignore
28+
@Test
29+
public void testCoordinateWithRankGreaterThan7NotAllowed() {
30+
expectedException.expect(IllegalArgumentException.class);
31+
expectedException.expectMessage("Coordinate must have rank <= 7.");
32+
33+
new BoardCoordinate(8, 4);
34+
}
35+
36+
@Ignore
37+
@Test
38+
public void testCoordinateWithNegativeFileNotAllowed() {
39+
expectedException.expect(IllegalArgumentException.class);
40+
expectedException.expectMessage("Coordinate must have positive file.");
41+
42+
new BoardCoordinate(2, -2);
43+
}
44+
45+
@Ignore
46+
@Test
47+
public void testCoordinateWithFileGreaterThan7NotAllowed() {
48+
expectedException.expect(IllegalArgumentException.class);
49+
expectedException.expectMessage("Coordinate must have file <= 7.");
50+
51+
new BoardCoordinate(4, 8);
52+
}
53+
54+
@Ignore
55+
@Test
56+
public void testNullCoordinateNotAllowed() {
57+
expectedException.expect(IllegalArgumentException.class);
58+
expectedException.expectMessage("You must supply valid board coordinates for both Queens.");
59+
60+
new QueenAttackCalculator(null, new BoardCoordinate(0, 7));
61+
}
62+
63+
@Ignore
64+
@Test
65+
public void testQueensMustNotOccupyTheSameSquare() {
66+
expectedException.expect(IllegalArgumentException.class);
67+
expectedException.expectMessage("Queens may not occupy the same board coordinate.");
68+
69+
new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(2, 2));
70+
}
71+
72+
@Ignore
73+
@Test
74+
public void testQueensThatDoNotShareRankFileOrDiagonalCannotAttack() {
75+
final QueenAttackCalculator calculator
76+
= new QueenAttackCalculator(new BoardCoordinate(2, 4), new BoardCoordinate(6, 6));
77+
78+
assertFalse(calculator.canQueensAttackOneAnother());
79+
}
80+
81+
@Ignore
82+
@Test
83+
public void testQueensCanAttackOnTheSameRank() {
84+
final QueenAttackCalculator calculator
85+
= new QueenAttackCalculator(new BoardCoordinate(2, 4), new BoardCoordinate(2, 6));
86+
87+
assertTrue(calculator.canQueensAttackOneAnother());
88+
}
89+
90+
@Ignore
91+
@Test
92+
public void testQueensCanAttackOnTheSameFile() {
93+
final QueenAttackCalculator calculator
94+
= new QueenAttackCalculator(new BoardCoordinate(4, 5), new BoardCoordinate(2, 5));
95+
96+
assertTrue(calculator.canQueensAttackOneAnother());
97+
}
98+
99+
@Ignore
100+
@Test
101+
public void testQueensCanAttackOnFirstDiagonal() {
102+
final QueenAttackCalculator calculator
103+
= new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(0, 4));
104+
105+
assertTrue(calculator.canQueensAttackOneAnother());
106+
}
107+
108+
@Ignore
109+
@Test
110+
public void testQueensCanAttackOnSecondDiagonal() {
111+
final QueenAttackCalculator calculator
112+
= new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(3, 1));
113+
114+
assertTrue(calculator.canQueensAttackOneAnother());
115+
}
116+
117+
@Ignore
118+
@Test
119+
public void testQueensCanAttackOnThirdDiagonal() {
120+
final QueenAttackCalculator calculator
121+
= new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(1, 1));
122+
123+
assertTrue(calculator.canQueensAttackOneAnother());
124+
}
125+
126+
@Ignore
127+
@Test
128+
public void testQueensCanAttackOnFourthDiagonal() {
129+
final QueenAttackCalculator calculator
130+
= new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(5, 5));
131+
132+
assertTrue(calculator.canQueensAttackOneAnother());
133+
}
134+
135+
}

‎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)