Selaa lähdekoodia

Merge pull request #665 from exercism/minesweeper-simplify

Minesweeper simplify
FridaTveit 9 vuotta sitten
vanhempi
commit
8433d12107

+ 5
- 41
exercises/minesweeper/src/example/java/MinesweeperBoard.java Näytä tiedosto

@@ -1,7 +1,5 @@
1 1
 import java.util.ArrayList;
2 2
 import java.util.List;
3
-import java.util.Set;
4
-import java.util.stream.Collectors;
5 3
 
6 4
 final class MinesweeperBoard {
7 5
 
@@ -16,33 +14,32 @@ final class MinesweeperBoard {
16 14
     private final int numberOfColumns;
17 15
 
18 16
     MinesweeperBoard(final List<String> rawRepresentation) {
19
-        validateInputBoard(rawRepresentation);
20 17
         this.rawRepresentation = rawRepresentation;
21 18
         this.numberOfRows = rawRepresentation.size();
22 19
         this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length();
23 20
     }
24 21
 
25
-    List<String> getAnnotatedRepresentation() throws IllegalArgumentException {
22
+    List<String> withNumbers() {
26 23
         final List<String> result = new ArrayList<>();
27 24
 
28 25
         for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
29
-            result.add(getAnnotatedRow(rowNumber));
26
+            result.add(getRowWithNumbers(rowNumber));
30 27
         }
31 28
 
32 29
         return result;
33 30
     }
34 31
 
35
-    private String getAnnotatedRow(final int rowNumber) {
32
+    private String getRowWithNumbers(final int rowNumber) {
36 33
         String result = "";
37 34
 
38 35
         for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
39
-            result += getCellAnnotation(rowNumber, columnNumber);
36
+            result += getCellNumber(rowNumber, columnNumber);
40 37
         }
41 38
 
42 39
         return result;
43 40
     }
44 41
 
45
-    private char getCellAnnotation(final int rowNumber, final int columnNumber) {
42
+    private char getCellNumber(final int rowNumber, final int columnNumber) {
46 43
         // If (rowNumber, columnNumber) is a mine, we're done.
47 44
         if (rawRepresentation.get(rowNumber).charAt(columnNumber) == MINE_CHAR) {
48 45
             return MINE_CHAR;
@@ -75,37 +72,4 @@ final class MinesweeperBoard {
75 72
         return result;
76 73
     }
77 74
 
78
-    private void validateInputBoard(final List<String> inputBoard) throws IllegalArgumentException {
79
-        validateInputBoardIsNotNull(inputBoard);
80
-
81
-        if (inputBoard.isEmpty()) {
82
-            return;
83
-        }
84
-
85
-        validateInputBoardCharacters(inputBoard);
86
-        validateInputBoardColumnCounts(inputBoard);
87
-    }
88
-
89
-    private void validateInputBoardIsNotNull(final List<String> inputBoard) throws IllegalArgumentException {
90
-        if (inputBoard == null) {
91
-            throw new IllegalArgumentException("Input board may not be null.");
92
-        }
93
-    }
94
-
95
-    private void validateInputBoardCharacters(final List<String> inputBoard) throws IllegalArgumentException {
96
-        final String allBoardCharacters = String.join("", inputBoard);
97
-
98
-        if (!allBoardCharacters.matches("^[ *]*$")) {
99
-            throw new IllegalArgumentException("Input board can only contain the characters ' ' and '*'.");
100
-        }
101
-    }
102
-
103
-    private void validateInputBoardColumnCounts(final List<String> inputBoard) throws IllegalArgumentException {
104
-        final Set<Integer> setOfColumnCounts = inputBoard.stream().map(String::length).collect(Collectors.toSet());
105
-
106
-        if (setOfColumnCounts.size() > 1) {
107
-            throw new IllegalArgumentException("Input board rows must all have the same number of columns.");
108
-        }
109
-    }
110
-
111 75
 }

+ 39
- 90
exercises/minesweeper/src/test/java/MinesweeperBoardTest.java Näytä tiedosto

@@ -1,7 +1,5 @@
1 1
 import org.junit.Ignore;
2
-import org.junit.Rule;
3 2
 import org.junit.Test;
4
-import org.junit.rules.ExpectedException;
5 3
 
6 4
 import java.util.Arrays;
7 5
 import java.util.Collections;
@@ -9,34 +7,28 @@ import java.util.List;
9 7
 
10 8
 import static org.junit.Assert.assertEquals;
11 9
 
10
+/*
11
+ * version: 1.0.0
12
+ */
12 13
 public class MinesweeperBoardTest {
13 14
 
14
-    /*
15
-     * See https://github.com/junit-team/junit4/wiki/Rules for information on JUnit Rules in general and
16
-     * ExpectedExceptions in particular.
17
-     */
18
-    @Rule
19
-    public ExpectedException expectedException = ExpectedException.none();
20
-
21 15
     @Test
22 16
     public void testInputBoardWithNoRowsAndNoColumns() {
23 17
         final List<String> inputBoard = Collections.emptyList();
24
-        final List<String> expectedAnnotatedRepresentation = Collections.emptyList();
25
-        final List<String> actualAnnotatedRepresentation
26
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
18
+        final List<String> expectedNumberedBoard = Collections.emptyList();
19
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
27 20
 
28
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
21
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
29 22
     }
30 23
 
31 24
     @Ignore("Remove to run test")
32 25
     @Test
33 26
     public void testInputBoardWithOneRowAndNoColumns() {
34 27
         final List<String> inputBoard = Collections.singletonList("");
35
-        final List<String> expectedAnnotatedRepresentation = Collections.singletonList("");
36
-        final List<String> actualAnnotatedRepresentation
37
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
28
+        final List<String> expectedNumberedBoard = Collections.singletonList("");
29
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
38 30
 
39
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
31
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
40 32
     }
41 33
 
42 34
     @Ignore("Remove to run test")
@@ -48,16 +40,15 @@ public class MinesweeperBoardTest {
48 40
                 "   "
49 41
         );
50 42
 
51
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
43
+        final List<String> expectedNumberedBoard = Arrays.asList(
52 44
                 "   ",
53 45
                 "   ",
54 46
                 "   "
55 47
         );
56 48
 
57
-        final List<String> actualAnnotatedRepresentation
58
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
49
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
59 50
 
60
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
51
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
61 52
     }
62 53
 
63 54
     @Ignore("Remove to run test")
@@ -69,16 +60,15 @@ public class MinesweeperBoardTest {
69 60
                 "***"
70 61
         );
71 62
 
72
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
63
+        final List<String> expectedNumberedBoard = Arrays.asList(
73 64
                 "***",
74 65
                 "***",
75 66
                 "***"
76 67
         );
77 68
 
78
-        final List<String> actualAnnotatedRepresentation
79
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
69
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
80 70
 
81
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
71
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
82 72
     }
83 73
 
84 74
     @Ignore("Remove to run test")
@@ -90,16 +80,15 @@ public class MinesweeperBoardTest {
90 80
                 "   "
91 81
         );
92 82
 
93
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
83
+        final List<String> expectedNumberedBoard = Arrays.asList(
94 84
                 "111",
95 85
                 "1*1",
96 86
                 "111"
97 87
         );
98 88
 
99
-        final List<String> actualAnnotatedRepresentation
100
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
89
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
101 90
 
102
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
91
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
103 92
     }
104 93
 
105 94
     @Ignore("Remove to run test")
@@ -111,16 +100,15 @@ public class MinesweeperBoardTest {
111 100
                 "***"
112 101
         );
113 102
 
114
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
103
+        final List<String> expectedNumberedBoard = Arrays.asList(
115 104
                 "***",
116 105
                 "*8*",
117 106
                 "***"
118 107
         );
119 108
 
120
-        final List<String> actualAnnotatedRepresentation
121
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
109
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
122 110
 
123
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
111
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
124 112
     }
125 113
 
126 114
     @Ignore("Remove to run test")
@@ -130,14 +118,13 @@ public class MinesweeperBoardTest {
130 118
                 " * * "
131 119
         );
132 120
 
133
-        final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
121
+        final List<String> expectedNumberedBoard = Collections.singletonList(
134 122
                 "1*2*1"
135 123
         );
136 124
 
137
-        final List<String> actualAnnotatedRepresentation
138
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
125
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
139 126
 
140
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
127
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
141 128
     }
142 129
 
143 130
     @Ignore("Remove to run test")
@@ -147,14 +134,13 @@ public class MinesweeperBoardTest {
147 134
                 "*   *"
148 135
         );
149 136
 
150
-        final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
137
+        final List<String> expectedNumberedBoard = Collections.singletonList(
151 138
                 "*1 1*"
152 139
         );
153 140
 
154
-        final List<String> actualAnnotatedRepresentation
155
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
141
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
156 142
 
157
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
143
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
158 144
     }
159 145
 
160 146
     @Ignore("Remove to run test")
@@ -168,7 +154,7 @@ public class MinesweeperBoardTest {
168 154
                 " "
169 155
         );
170 156
 
171
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
157
+        final List<String> expectedNumberedBoard = Arrays.asList(
172 158
                 "1",
173 159
                 "*",
174 160
                 "2",
@@ -176,10 +162,9 @@ public class MinesweeperBoardTest {
176 162
                 "1"
177 163
         );
178 164
 
179
-        final List<String> actualAnnotatedRepresentation
180
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
165
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
181 166
 
182
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
167
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
183 168
     }
184 169
 
185 170
     @Ignore("Remove to run test")
@@ -193,7 +178,7 @@ public class MinesweeperBoardTest {
193 178
                 "*"
194 179
         );
195 180
 
196
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
181
+        final List<String> expectedNumberedBoard = Arrays.asList(
197 182
                 "*",
198 183
                 "1",
199 184
                 " ",
@@ -201,10 +186,9 @@ public class MinesweeperBoardTest {
201 186
                 "*"
202 187
         );
203 188
 
204
-        final List<String> actualAnnotatedRepresentation
205
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
189
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
206 190
 
207
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
191
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
208 192
     }
209 193
 
210 194
     @Ignore("Remove to run test")
@@ -218,7 +202,7 @@ public class MinesweeperBoardTest {
218 202
                 "  *  "
219 203
         );
220 204
 
221
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
205
+        final List<String> expectedNumberedBoard = Arrays.asList(
222 206
                 " 2*2 ",
223 207
                 "25*52",
224 208
                 "*****",
@@ -226,10 +210,9 @@ public class MinesweeperBoardTest {
226 210
                 " 2*2 "
227 211
         );
228 212
 
229
-        final List<String> actualAnnotatedRepresentation
230
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
213
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
231 214
 
232
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
215
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
233 216
     }
234 217
 
235 218
     @Ignore("Remove to run test")
@@ -244,7 +227,7 @@ public class MinesweeperBoardTest {
244 227
                 "      "
245 228
         );
246 229
 
247
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
230
+        final List<String> expectedNumberedBoard = Arrays.asList(
248 231
                 "1*22*1",
249 232
                 "12*322",
250 233
                 " 123*2",
@@ -253,43 +236,9 @@ public class MinesweeperBoardTest {
253 236
                 "111111"
254 237
         );
255 238
 
256
-        final List<String> actualAnnotatedRepresentation
257
-                = new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
258
-
259
-        assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
260
-    }
261
-
262
-    @Ignore("Remove to run test")
263
-    @Test
264
-    public void testNullInputBoardIsRejected() {
265
-        expectedException.expect(IllegalArgumentException.class);
266
-        expectedException.expectMessage("Input board may not be null.");
267
-
268
-        new MinesweeperBoard(null);
269
-    }
239
+        final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
270 240
 
271
-    @Ignore("Remove to run test")
272
-    @Test
273
-    public void testInputBoardWithInvalidSymbolsIsRejected() {
274
-        expectedException.expect(IllegalArgumentException.class);
275
-        expectedException.expectMessage("Input board can only contain the characters ' ' and '*'.");
276
-
277
-        new MinesweeperBoard(Collections.singletonList(" * & "));
278
-    }
279
-
280
-    @Ignore("Remove to run test")
281
-    @Test
282
-    public void testInputBoardWithInconsistentRowLengthsIsRejected() {
283
-        expectedException.expect(IllegalArgumentException.class);
284
-        expectedException.expectMessage("Input board rows must all have the same number of columns.");
285
-
286
-        new MinesweeperBoard(Arrays.asList(
287
-                "*",
288
-                "**",
289
-                "* *",
290
-                "*  *",
291
-                "*   *"
292
-        ));
241
+        assertEquals(expectedNumberedBoard, actualNumberedBoard);
293 242
     }
294 243
 
295 244
 }