Pārlūkot izejas kodu

Merge pull request #665 from exercism/minesweeper-simplify

Minesweeper simplify
FridaTveit 9 gadus atpakaļ
vecāks
revīzija
8433d12107

+ 5
- 41
exercises/minesweeper/src/example/java/MinesweeperBoard.java Parādīt failu

1
 import java.util.ArrayList;
1
 import java.util.ArrayList;
2
 import java.util.List;
2
 import java.util.List;
3
-import java.util.Set;
4
-import java.util.stream.Collectors;
5
 
3
 
6
 final class MinesweeperBoard {
4
 final class MinesweeperBoard {
7
 
5
 
16
     private final int numberOfColumns;
14
     private final int numberOfColumns;
17
 
15
 
18
     MinesweeperBoard(final List<String> rawRepresentation) {
16
     MinesweeperBoard(final List<String> rawRepresentation) {
19
-        validateInputBoard(rawRepresentation);
20
         this.rawRepresentation = rawRepresentation;
17
         this.rawRepresentation = rawRepresentation;
21
         this.numberOfRows = rawRepresentation.size();
18
         this.numberOfRows = rawRepresentation.size();
22
         this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length();
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
         final List<String> result = new ArrayList<>();
23
         final List<String> result = new ArrayList<>();
27
 
24
 
28
         for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
25
         for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
29
-            result.add(getAnnotatedRow(rowNumber));
26
+            result.add(getRowWithNumbers(rowNumber));
30
         }
27
         }
31
 
28
 
32
         return result;
29
         return result;
33
     }
30
     }
34
 
31
 
35
-    private String getAnnotatedRow(final int rowNumber) {
32
+    private String getRowWithNumbers(final int rowNumber) {
36
         String result = "";
33
         String result = "";
37
 
34
 
38
         for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
35
         for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
39
-            result += getCellAnnotation(rowNumber, columnNumber);
36
+            result += getCellNumber(rowNumber, columnNumber);
40
         }
37
         }
41
 
38
 
42
         return result;
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
         // If (rowNumber, columnNumber) is a mine, we're done.
43
         // If (rowNumber, columnNumber) is a mine, we're done.
47
         if (rawRepresentation.get(rowNumber).charAt(columnNumber) == MINE_CHAR) {
44
         if (rawRepresentation.get(rowNumber).charAt(columnNumber) == MINE_CHAR) {
48
             return MINE_CHAR;
45
             return MINE_CHAR;
75
         return result;
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 Parādīt failu

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
-import org.junit.Rule;
3
 import org.junit.Test;
2
 import org.junit.Test;
4
-import org.junit.rules.ExpectedException;
5
 
3
 
6
 import java.util.Arrays;
4
 import java.util.Arrays;
7
 import java.util.Collections;
5
 import java.util.Collections;
9
 
7
 
10
 import static org.junit.Assert.assertEquals;
8
 import static org.junit.Assert.assertEquals;
11
 
9
 
10
+/*
11
+ * version: 1.0.0
12
+ */
12
 public class MinesweeperBoardTest {
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
     @Test
15
     @Test
22
     public void testInputBoardWithNoRowsAndNoColumns() {
16
     public void testInputBoardWithNoRowsAndNoColumns() {
23
         final List<String> inputBoard = Collections.emptyList();
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
     @Ignore("Remove to run test")
24
     @Ignore("Remove to run test")
32
     @Test
25
     @Test
33
     public void testInputBoardWithOneRowAndNoColumns() {
26
     public void testInputBoardWithOneRowAndNoColumns() {
34
         final List<String> inputBoard = Collections.singletonList("");
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
     @Ignore("Remove to run test")
34
     @Ignore("Remove to run test")
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
     @Ignore("Remove to run test")
54
     @Ignore("Remove to run test")
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
     @Ignore("Remove to run test")
74
     @Ignore("Remove to run test")
90
                 "   "
80
                 "   "
91
         );
81
         );
92
 
82
 
93
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
83
+        final List<String> expectedNumberedBoard = Arrays.asList(
94
                 "111",
84
                 "111",
95
                 "1*1",
85
                 "1*1",
96
                 "111"
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
     @Ignore("Remove to run test")
94
     @Ignore("Remove to run test")
111
                 "***"
100
                 "***"
112
         );
101
         );
113
 
102
 
114
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
103
+        final List<String> expectedNumberedBoard = Arrays.asList(
115
                 "***",
104
                 "***",
116
                 "*8*",
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
     @Ignore("Remove to run test")
114
     @Ignore("Remove to run test")
130
                 " * * "
118
                 " * * "
131
         );
119
         );
132
 
120
 
133
-        final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
121
+        final List<String> expectedNumberedBoard = Collections.singletonList(
134
                 "1*2*1"
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
     @Ignore("Remove to run test")
130
     @Ignore("Remove to run test")
147
                 "*   *"
134
                 "*   *"
148
         );
135
         );
149
 
136
 
150
-        final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
137
+        final List<String> expectedNumberedBoard = Collections.singletonList(
151
                 "*1 1*"
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
     @Ignore("Remove to run test")
146
     @Ignore("Remove to run test")
168
                 " "
154
                 " "
169
         );
155
         );
170
 
156
 
171
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
157
+        final List<String> expectedNumberedBoard = Arrays.asList(
172
                 "1",
158
                 "1",
173
                 "*",
159
                 "*",
174
                 "2",
160
                 "2",
176
                 "1"
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
     @Ignore("Remove to run test")
170
     @Ignore("Remove to run test")
193
                 "*"
178
                 "*"
194
         );
179
         );
195
 
180
 
196
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
181
+        final List<String> expectedNumberedBoard = Arrays.asList(
197
                 "*",
182
                 "*",
198
                 "1",
183
                 "1",
199
                 " ",
184
                 " ",
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
     @Ignore("Remove to run test")
194
     @Ignore("Remove to run test")
218
                 "  *  "
202
                 "  *  "
219
         );
203
         );
220
 
204
 
221
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
205
+        final List<String> expectedNumberedBoard = Arrays.asList(
222
                 " 2*2 ",
206
                 " 2*2 ",
223
                 "25*52",
207
                 "25*52",
224
                 "*****",
208
                 "*****",
226
                 " 2*2 "
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
     @Ignore("Remove to run test")
218
     @Ignore("Remove to run test")
244
                 "      "
227
                 "      "
245
         );
228
         );
246
 
229
 
247
-        final List<String> expectedAnnotatedRepresentation = Arrays.asList(
230
+        final List<String> expectedNumberedBoard = Arrays.asList(
248
                 "1*22*1",
231
                 "1*22*1",
249
                 "12*322",
232
                 "12*322",
250
                 " 123*2",
233
                 " 123*2",
253
                 "111111"
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
 }