Skip to content

Commit 8433d12

Browse files
authored
Merge pull request exercism#665 from exercism/minesweeper-simplify
Minesweeper simplify
2 parents cf8f6f4 + 270336e commit 8433d12

2 files changed

Lines changed: 44 additions & 131 deletions

File tree

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import java.util.ArrayList;
22
import java.util.List;
3-
import java.util.Set;
4-
import java.util.stream.Collectors;
53

64
final class MinesweeperBoard {
75

@@ -16,33 +14,32 @@ final class MinesweeperBoard {
1614
private final int numberOfColumns;
1715

1816
MinesweeperBoard(final List<String> rawRepresentation) {
19-
validateInputBoard(rawRepresentation);
2017
this.rawRepresentation = rawRepresentation;
2118
this.numberOfRows = rawRepresentation.size();
2219
this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length();
2320
}
2421

25-
List<String> getAnnotatedRepresentation() throws IllegalArgumentException {
22+
List<String> withNumbers() {
2623
final List<String> result = new ArrayList<>();
2724

2825
for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
29-
result.add(getAnnotatedRow(rowNumber));
26+
result.add(getRowWithNumbers(rowNumber));
3027
}
3128

3229
return result;
3330
}
3431

35-
private String getAnnotatedRow(final int rowNumber) {
32+
private String getRowWithNumbers(final int rowNumber) {
3633
String result = "";
3734

3835
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
39-
result += getCellAnnotation(rowNumber, columnNumber);
36+
result += getCellNumber(rowNumber, columnNumber);
4037
}
4138

4239
return result;
4340
}
4441

45-
private char getCellAnnotation(final int rowNumber, final int columnNumber) {
42+
private char getCellNumber(final int rowNumber, final int columnNumber) {
4643
// If (rowNumber, columnNumber) is a mine, we're done.
4744
if (rawRepresentation.get(rowNumber).charAt(columnNumber) == MINE_CHAR) {
4845
return MINE_CHAR;
@@ -75,37 +72,4 @@ private int computeMineCountAround(final int rowNumber, final int columnNumber)
7572
return result;
7673
}
7774

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-
11175
}
Lines changed: 39 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
11
import org.junit.Ignore;
2-
import org.junit.Rule;
32
import org.junit.Test;
4-
import org.junit.rules.ExpectedException;
53

64
import java.util.Arrays;
75
import java.util.Collections;
86
import java.util.List;
97

108
import static org.junit.Assert.assertEquals;
119

10+
/*
11+
* version: 1.0.0
12+
*/
1213
public class MinesweeperBoardTest {
1314

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-
2115
@Test
2216
public void testInputBoardWithNoRowsAndNoColumns() {
2317
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();
2720

28-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
21+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
2922
}
3023

3124
@Ignore("Remove to run test")
3225
@Test
3326
public void testInputBoardWithOneRowAndNoColumns() {
3427
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();
3830

39-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
31+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
4032
}
4133

4234
@Ignore("Remove to run test")
@@ -48,16 +40,15 @@ public void testInputBoardWithNoMines() {
4840
" "
4941
);
5042

51-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
43+
final List<String> expectedNumberedBoard = Arrays.asList(
5244
" ",
5345
" ",
5446
" "
5547
);
5648

57-
final List<String> actualAnnotatedRepresentation
58-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
49+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
5950

60-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
51+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
6152
}
6253

6354
@Ignore("Remove to run test")
@@ -69,16 +60,15 @@ public void testInputBoardWithOnlyMines() {
6960
"***"
7061
);
7162

72-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
63+
final List<String> expectedNumberedBoard = Arrays.asList(
7364
"***",
7465
"***",
7566
"***"
7667
);
7768

78-
final List<String> actualAnnotatedRepresentation
79-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
69+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
8070

81-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
71+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
8272
}
8373

8474
@Ignore("Remove to run test")
@@ -90,16 +80,15 @@ public void testInputBoardWithSingleMineAtCenter() {
9080
" "
9181
);
9282

93-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
83+
final List<String> expectedNumberedBoard = Arrays.asList(
9484
"111",
9585
"1*1",
9686
"111"
9787
);
9888

99-
final List<String> actualAnnotatedRepresentation
100-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
89+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
10190

102-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
91+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
10392
}
10493

10594
@Ignore("Remove to run test")
@@ -111,16 +100,15 @@ public void testInputBoardWithMinesAroundPerimeter() {
111100
"***"
112101
);
113102

114-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
103+
final List<String> expectedNumberedBoard = Arrays.asList(
115104
"***",
116105
"*8*",
117106
"***"
118107
);
119108

120-
final List<String> actualAnnotatedRepresentation
121-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
109+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
122110

123-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
111+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
124112
}
125113

126114
@Ignore("Remove to run test")
@@ -130,14 +118,13 @@ public void testInputBoardWithSingleRowAndTwoMines() {
130118
" * * "
131119
);
132120

133-
final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
121+
final List<String> expectedNumberedBoard = Collections.singletonList(
134122
"1*2*1"
135123
);
136124

137-
final List<String> actualAnnotatedRepresentation
138-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
125+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
139126

140-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
127+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
141128
}
142129

143130
@Ignore("Remove to run test")
@@ -147,14 +134,13 @@ public void testInputBoardWithSingleRowAndTwoMinesAtEdges() {
147134
"* *"
148135
);
149136

150-
final List<String> expectedAnnotatedRepresentation = Collections.singletonList(
137+
final List<String> expectedNumberedBoard = Collections.singletonList(
151138
"*1 1*"
152139
);
153140

154-
final List<String> actualAnnotatedRepresentation
155-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
141+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
156142

157-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
143+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
158144
}
159145

160146
@Ignore("Remove to run test")
@@ -168,18 +154,17 @@ public void testInputBoardWithSingleColumnAndTwoMines() {
168154
" "
169155
);
170156

171-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
157+
final List<String> expectedNumberedBoard = Arrays.asList(
172158
"1",
173159
"*",
174160
"2",
175161
"*",
176162
"1"
177163
);
178164

179-
final List<String> actualAnnotatedRepresentation
180-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
165+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
181166

182-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
167+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
183168
}
184169

185170
@Ignore("Remove to run test")
@@ -193,18 +178,17 @@ public void testInputBoardWithSingleColumnAndTwoMinesAtEdges() {
193178
"*"
194179
);
195180

196-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
181+
final List<String> expectedNumberedBoard = Arrays.asList(
197182
"*",
198183
"1",
199184
" ",
200185
"1",
201186
"*"
202187
);
203188

204-
final List<String> actualAnnotatedRepresentation
205-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
189+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
206190

207-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
191+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
208192
}
209193

210194
@Ignore("Remove to run test")
@@ -218,18 +202,17 @@ public void testInputBoardWithMinesInCross() {
218202
" * "
219203
);
220204

221-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
205+
final List<String> expectedNumberedBoard = Arrays.asList(
222206
" 2*2 ",
223207
"25*52",
224208
"*****",
225209
"25*52",
226210
" 2*2 "
227211
);
228212

229-
final List<String> actualAnnotatedRepresentation
230-
= new MinesweeperBoard(inputBoard).getAnnotatedRepresentation();
213+
final List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();
231214

232-
assertEquals(expectedAnnotatedRepresentation, actualAnnotatedRepresentation);
215+
assertEquals(expectedNumberedBoard, actualNumberedBoard);
233216
}
234217

235218
@Ignore("Remove to run test")
@@ -244,7 +227,7 @@ public void testLargeInputBoard() {
244227
" "
245228
);
246229

247-
final List<String> expectedAnnotatedRepresentation = Arrays.asList(
230+
final List<String> expectedNumberedBoard = Arrays.asList(
248231
"1*22*1",
249232
"12*322",
250233
" 123*2",
@@ -253,43 +236,9 @@ public void testLargeInputBoard() {
253236
"111111"
254237
);
255238

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();
270240

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);
293242
}
294243

295244
}

0 commit comments

Comments
 (0)