Skip to content

Commit 4f4dbc8

Browse files
committed
Refactor queen-attack to use a Coordinate type
1 parent cda0e7b commit 4f4dbc8

4 files changed

Lines changed: 87 additions & 63 deletions

File tree

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: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import java.util.Arrays;
2-
31
public final class QueenAttackCalculator {
42

5-
private final int[] queen1Coordinates;
3+
private final BoardCoordinate whiteQueenCoordinate;
64

7-
private final int[] queen2Coordinates;
5+
private final BoardCoordinate blackQueenCoordinate;
86

9-
public QueenAttackCalculator(final int[] queen1Coordinates, final int[] queen2Coordinates)
7+
public QueenAttackCalculator(final BoardCoordinate whiteQueenCoordinate, final BoardCoordinate blackQueenCoordinate)
108
throws IllegalArgumentException {
119

12-
this.queen1Coordinates = queen1Coordinates;
13-
this.queen2Coordinates = queen2Coordinates;
10+
this.whiteQueenCoordinate = whiteQueenCoordinate;
11+
this.blackQueenCoordinate = blackQueenCoordinate;
1412

1513
validateInputs();
1614
}
@@ -20,42 +18,12 @@ public boolean canQueensAttackOneAnother() {
2018
}
2119

2220
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.");
21+
if (whiteQueenCoordinate == null || blackQueenCoordinate == null) {
22+
throw new IllegalArgumentException("You must supply valid board coordinates for both Queens.");
5523
}
5624

57-
if (queensShareSquare()) {
58-
throw new IllegalArgumentException("Queens must not occupy the same square.");
25+
if (queensShareBoardCoordinate()) {
26+
throw new IllegalArgumentException("Queens may not occupy the same board coordinate.");
5927
}
6028
}
6129

@@ -67,7 +35,7 @@ private boolean queensShareFile() {
6735
return differenceBetweenFiles() == 0;
6836
}
6937

70-
private boolean queensShareSquare() {
38+
private boolean queensShareBoardCoordinate() {
7139
return queensShareRank() && queensShareFile();
7240
}
7341

@@ -76,11 +44,11 @@ private boolean queensShareDiagonal() {
7644
}
7745

7846
private int differenceBetweenRanks() {
79-
return Math.abs(queen1Coordinates[0] - queen2Coordinates[0]);
47+
return Math.abs(whiteQueenCoordinate.getRank() - blackQueenCoordinate.getRank());
8048
}
8149

8250
private int differenceBetweenFiles() {
83-
return Math.abs(queen1Coordinates[1] - queen2Coordinates[1]);
51+
return Math.abs(whiteQueenCoordinate.getFile() - blackQueenCoordinate.getFile());
8452
}
8553

8654
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public final class Coordinate {
2+
3+
4+
5+
}

‎exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java‎

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,102 +12,114 @@ public final class QueenAttackCalculatorTest {
1212

1313
@Test
1414
public void testNoExceptionThrownIfBothQueenPositionsValid() {
15-
new QueenAttackCalculator(new int[]{2, 2}, new int[]{0, 7});
15+
new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(0, 7));
1616
}
1717

1818
@Ignore
1919
@Test
2020
public void testNullInputNotAllowed() {
2121
expectedException.expect(IllegalArgumentException.class);
22-
expectedException.expectMessage("You must supply valid 2-dimensional coordinate arrays for both Queens.");
22+
expectedException.expectMessage("You must supply valid board coordinates for both Queens.");
2323

24-
new QueenAttackCalculator(null, new int[]{3, 4});
24+
new QueenAttackCalculator(null, new BoardCoordinate(0, 7));
2525
}
2626

2727
@Ignore
2828
@Test
2929
public void testNegativeRankNotAllowed() {
3030
expectedException.expect(IllegalArgumentException.class);
31-
expectedException.expectMessage("Queens must both have positive rank.");
31+
expectedException.expectMessage("Coordinate must have positive rank.");
3232

33-
new QueenAttackCalculator(new int[]{-2, 2}, new int[]{2, 2});
33+
new BoardCoordinate(-2, 2);
3434
}
3535

3636
@Ignore
3737
@Test
3838
public void testRankGreaterThan7NotAllowed() {
3939
expectedException.expect(IllegalArgumentException.class);
40-
expectedException.expectMessage("Queens must both have rank <= 7.");
40+
expectedException.expectMessage("Coordinate must have rank <= 7.");
4141

42-
new QueenAttackCalculator(new int[]{8, 4}, new int[]{1, 6});
42+
new BoardCoordinate(8, 4);
4343
}
4444

4545
@Ignore
4646
@Test
4747
public void testNegativeFileNotAllowed() {
4848
expectedException.expect(IllegalArgumentException.class);
49-
expectedException.expectMessage("Queens must both have positive file.");
49+
expectedException.expectMessage("Coordinate must have positive file.");
5050

51-
new QueenAttackCalculator(new int[]{2, -2}, new int[]{5, 3});
51+
new BoardCoordinate(2, -2);
5252
}
5353

5454
@Ignore
5555
@Test
5656
public void testFileGreaterThan7NotAllowed() {
5757
expectedException.expect(IllegalArgumentException.class);
58-
expectedException.expectMessage("Queens must both have file <= 7.");
58+
expectedException.expectMessage("Coordinate must have file <= 7.");
5959

60-
new QueenAttackCalculator(new int[]{4, 8}, new int[]{7, 4});
60+
new BoardCoordinate(4, 8);
6161
}
6262

6363
@Ignore
6464
@Test
6565
public void testQueensMustNotOccupyTheSameSquare() {
6666
expectedException.expect(IllegalArgumentException.class);
67-
expectedException.expectMessage("Queens must not occupy the same square.");
67+
expectedException.expectMessage("Queens may not occupy the same board coordinate.");
6868

69-
new QueenAttackCalculator(new int[]{2, 2}, new int[]{2, 2});
69+
new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(2, 2));
7070
}
7171

7272
@Ignore
7373
@Test
7474
public void testQueensCanAttackOnTheSameRank() {
75-
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 4}, new int[]{2, 6});
75+
final QueenAttackCalculator calculator
76+
= new QueenAttackCalculator(new BoardCoordinate(2, 4), new BoardCoordinate(2, 6));
77+
7678
assertTrue(calculator.canQueensAttackOneAnother());
7779
}
7880

7981
@Ignore
8082
@Test
8183
public void testQueensCanAttackOnTheSameFile() {
82-
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{4, 5}, new int[]{2, 5});
84+
final QueenAttackCalculator calculator
85+
= new QueenAttackCalculator(new BoardCoordinate(4, 5), new BoardCoordinate(2, 5));
86+
8387
assertTrue(calculator.canQueensAttackOneAnother());
8488
}
8589

8690
@Ignore
8791
@Test
8892
public void testQueensCanAttackOnFirstDiagonal() {
89-
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 2}, new int[]{0, 4});
93+
final QueenAttackCalculator calculator
94+
= new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(0, 4));
95+
9096
assertTrue(calculator.canQueensAttackOneAnother());
9197
}
9298

9399
@Ignore
94100
@Test
95101
public void testQueensCanAttackOnSecondDiagonal() {
96-
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 2}, new int[]{3, 1});
102+
final QueenAttackCalculator calculator
103+
= new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(3, 1));
104+
97105
assertTrue(calculator.canQueensAttackOneAnother());
98106
}
99107

100108
@Ignore
101109
@Test
102110
public void testQueensCanAttackOnThirdDiagonal() {
103-
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 2}, new int[]{1, 1});
111+
final QueenAttackCalculator calculator
112+
= new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(1, 1));
113+
104114
assertTrue(calculator.canQueensAttackOneAnother());
105115
}
106116

107117
@Ignore
108118
@Test
109119
public void testQueensCanAttackOnFourthDiagonal() {
110-
final QueenAttackCalculator calculator = new QueenAttackCalculator(new int[]{2, 2}, new int[]{5, 5});
120+
final QueenAttackCalculator calculator
121+
= new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(5, 5));
122+
111123
assertTrue(calculator.canQueensAttackOneAnother());
112124
}
113125

0 commit comments

Comments
 (0)