Selaa lähdekoodia

queen-attack: add to track

Stuart Kent 10 vuotta sitten
vanhempi
commit
cda0e7b91d

+ 7
- 1
config.json Näytä tiedosto

@@ -44,7 +44,8 @@
44 44
     "pascals-triangle",
45 45
     "beer-song",
46 46
     "difference-of-squares",
47
-    "largest-series-product"
47
+    "largest-series-product",
48
+    "queen-attack"
48 49
   ],
49 50
   "exercises": [
50 51
     {
@@ -251,6 +252,11 @@
251 252
       "slug": "largest-series-product",
252 253
       "difficulty": 1,
253 254
       "topics": []
255
+    },
256
+    {
257
+      "slug": "queen-attack",
258
+      "difficulty": 1,
259
+      "topics": []
254 260
     }
255 261
   ],
256 262
   "deprecated": [

+ 17
- 0
exercises/queen-attack/build.gradle Näytä tiedosto

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

+ 86
- 0
exercises/queen-attack/src/example/java/QueenAttackCalculator.java Näytä tiedosto

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

+ 5
- 0
exercises/queen-attack/src/main/java/QueenAttackCalculator.java Näytä tiedosto

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

+ 114
- 0
exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java Näytä tiedosto

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

+ 1
- 0
exercises/settings.gradle Näytä tiedosto

@@ -25,6 +25,7 @@ include 'pangram'
25 25
 include 'pascals-triangle'
26 26
 include 'phone-number'
27 27
 include 'pig-latin'
28
+include 'queen-attack'
28 29
 include 'raindrops'
29 30
 include 'rna-transcription'
30 31
 include 'robot-name'