Procházet zdrojové kódy

Refactor queen-attack to use a Coordinate type

Stuart Kent před 9 roky
rodič
revize
4f4dbc8c73

+ 39
- 0
exercises/queen-attack/src/example/java/BoardCoordinate.java Zobrazit soubor

@@ -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
+}

+ 12
- 44
exercises/queen-attack/src/example/java/QueenAttackCalculator.java Zobrazit soubor

@@ -1,16 +1,14 @@
1
-import java.util.Arrays;
2
-
3 1
 public final class QueenAttackCalculator {
4 2
 
5
-    private final int[] queen1Coordinates;
3
+    private final BoardCoordinate whiteQueenCoordinate;
6 4
 
7
-    private final int[] queen2Coordinates;
5
+    private final BoardCoordinate blackQueenCoordinate;
8 6
 
9
-    public QueenAttackCalculator(final int[] queen1Coordinates, final int[] queen2Coordinates)
7
+    public QueenAttackCalculator(final BoardCoordinate whiteQueenCoordinate, final BoardCoordinate blackQueenCoordinate)
10 8
             throws IllegalArgumentException {
11 9
 
12
-        this.queen1Coordinates = queen1Coordinates;
13
-        this.queen2Coordinates = queen2Coordinates;
10
+        this.whiteQueenCoordinate = whiteQueenCoordinate;
11
+        this.blackQueenCoordinate = blackQueenCoordinate;
14 12
 
15 13
         validateInputs();
16 14
     }
@@ -20,42 +18,12 @@ public final class QueenAttackCalculator {
20 18
     }
21 19
 
22 20
     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.");
55 23
         }
56 24
 
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.");
59 27
         }
60 28
     }
61 29
 
@@ -67,7 +35,7 @@ public final class QueenAttackCalculator {
67 35
         return differenceBetweenFiles() == 0;
68 36
     }
69 37
 
70
-    private boolean queensShareSquare() {
38
+    private boolean queensShareBoardCoordinate() {
71 39
         return queensShareRank() && queensShareFile();
72 40
     }
73 41
 
@@ -76,11 +44,11 @@ public final class QueenAttackCalculator {
76 44
     }
77 45
 
78 46
     private int differenceBetweenRanks() {
79
-        return Math.abs(queen1Coordinates[0] - queen2Coordinates[0]);
47
+        return Math.abs(whiteQueenCoordinate.getRank() - blackQueenCoordinate.getRank());
80 48
     }
81 49
 
82 50
     private int differenceBetweenFiles() {
83
-        return Math.abs(queen1Coordinates[1] - queen2Coordinates[1]);
51
+        return Math.abs(whiteQueenCoordinate.getFile() - blackQueenCoordinate.getFile());
84 52
     }
85 53
 
86 54
 }

+ 5
- 0
exercises/queen-attack/src/main/java/Coordinate.java Zobrazit soubor

@@ -0,0 +1,5 @@
1
+public final class Coordinate {
2
+
3
+
4
+
5
+}

+ 31
- 19
exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java Zobrazit soubor

@@ -12,102 +12,114 @@ public final class QueenAttackCalculatorTest {
12 12
 
13 13
     @Test
14 14
     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));
16 16
     }
17 17
 
18 18
     @Ignore
19 19
     @Test
20 20
     public void testNullInputNotAllowed() {
21 21
         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.");
23 23
 
24
-        new QueenAttackCalculator(null, new int[]{3, 4});
24
+        new QueenAttackCalculator(null, new BoardCoordinate(0, 7));
25 25
     }
26 26
 
27 27
     @Ignore
28 28
     @Test
29 29
     public void testNegativeRankNotAllowed() {
30 30
         expectedException.expect(IllegalArgumentException.class);
31
-        expectedException.expectMessage("Queens must both have positive rank.");
31
+        expectedException.expectMessage("Coordinate must have positive rank.");
32 32
 
33
-        new QueenAttackCalculator(new int[]{-2, 2}, new int[]{2, 2});
33
+        new BoardCoordinate(-2, 2);
34 34
     }
35 35
 
36 36
     @Ignore
37 37
     @Test
38 38
     public void testRankGreaterThan7NotAllowed() {
39 39
         expectedException.expect(IllegalArgumentException.class);
40
-        expectedException.expectMessage("Queens must both have rank <= 7.");
40
+        expectedException.expectMessage("Coordinate must have rank <= 7.");
41 41
 
42
-        new QueenAttackCalculator(new int[]{8, 4}, new int[]{1, 6});
42
+        new BoardCoordinate(8, 4);
43 43
     }
44 44
 
45 45
     @Ignore
46 46
     @Test
47 47
     public void testNegativeFileNotAllowed() {
48 48
         expectedException.expect(IllegalArgumentException.class);
49
-        expectedException.expectMessage("Queens must both have positive file.");
49
+        expectedException.expectMessage("Coordinate must have positive file.");
50 50
 
51
-        new QueenAttackCalculator(new int[]{2, -2}, new int[]{5, 3});
51
+        new BoardCoordinate(2, -2);
52 52
     }
53 53
 
54 54
     @Ignore
55 55
     @Test
56 56
     public void testFileGreaterThan7NotAllowed() {
57 57
         expectedException.expect(IllegalArgumentException.class);
58
-        expectedException.expectMessage("Queens must both have file <= 7.");
58
+        expectedException.expectMessage("Coordinate must have file <= 7.");
59 59
 
60
-        new QueenAttackCalculator(new int[]{4, 8}, new int[]{7, 4});
60
+        new BoardCoordinate(4, 8);
61 61
     }
62 62
 
63 63
     @Ignore
64 64
     @Test
65 65
     public void testQueensMustNotOccupyTheSameSquare() {
66 66
         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.");
68 68
 
69
-        new QueenAttackCalculator(new int[]{2, 2}, new int[]{2, 2});
69
+        new QueenAttackCalculator(new BoardCoordinate(2, 2), new BoardCoordinate(2, 2));
70 70
     }
71 71
 
72 72
     @Ignore
73 73
     @Test
74 74
     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
+
76 78
         assertTrue(calculator.canQueensAttackOneAnother());
77 79
     }
78 80
 
79 81
     @Ignore
80 82
     @Test
81 83
     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
+
83 87
         assertTrue(calculator.canQueensAttackOneAnother());
84 88
     }
85 89
 
86 90
     @Ignore
87 91
     @Test
88 92
     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
+
90 96
         assertTrue(calculator.canQueensAttackOneAnother());
91 97
     }
92 98
 
93 99
     @Ignore
94 100
     @Test
95 101
     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
+
97 105
         assertTrue(calculator.canQueensAttackOneAnother());
98 106
     }
99 107
 
100 108
     @Ignore
101 109
     @Test
102 110
     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
+
104 114
         assertTrue(calculator.canQueensAttackOneAnother());
105 115
     }
106 116
 
107 117
     @Ignore
108 118
     @Test
109 119
     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
+
111 123
         assertTrue(calculator.canQueensAttackOneAnother());
112 124
     }
113 125