Просмотр исходного кода

Merge pull request #171 from stkent/queen-attack

queen-attack: add to track
John Ryan 9 лет назад
Родитель
Сommit
5f83bc978e

+ 7
- 1
config.json Просмотреть файл

@@ -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 Просмотреть файл

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

+ 39
- 0
exercises/queen-attack/src/example/java/BoardCoordinate.java Просмотреть файл

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

+ 54
- 0
exercises/queen-attack/src/example/java/QueenAttackCalculator.java Просмотреть файл

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

+ 5
- 0
exercises/queen-attack/src/main/java/BoardCoordinate.java Просмотреть файл

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

+ 5
- 0
exercises/queen-attack/src/main/java/QueenAttackCalculator.java Просмотреть файл

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

+ 135
- 0
exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java Просмотреть файл

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

+ 1
- 0
exercises/settings.gradle Просмотреть файл

@@ -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'