-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMinesweeper.java
61 lines (53 loc) · 2.25 KB
/
Minesweeper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
import java.util.List;
import java.util.function.IntBinaryOperator;
import java.util.function.IntUnaryOperator;
class Minesweeper {
private static final BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
private static final String SPACE = " ";
private static final int[][] p = new int[][] {
{ -1, -1 }, { 0, -1 }, { 1, -1 }, { -1, 0 },
{ 1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }
};
public static void main(String[] args) throws IOException {
int lineNum = 0;
String currentLine = INPUT_END;
while ((currentLine = reader.readLine()) != null) {
if (currentLine.equalsIgnoreCase("")) {
continue;
}
List<Integer> nm = stream(currentLine.split(SPACE))
.filter(x -> !x.equals("")).map(Integer::parseInt).collect(toList());;
int n = nm.get(0);
int m = nm.get(1);
if (n == 0 && m == 0) {
break;
}
final int[] field = reader.lines().limit(n)
.collect(joining()).chars().map(x -> x == '*' ? -1 : 0).toArray();
final IntBinaryOperator mine =
(x, y) -> (x < 0 || x > (n - 1) || y < 0 || y > (m - 1)) ? 0 : field[x * m + y];
final IntUnaryOperator count = (i) -> range(0, p.length)
.map(j -> Math.abs(mine.applyAsInt(i / m + p[j][0], i % m + p[j][1]))).sum();
int[] result = range(0, field.length)
.map(x -> field[x] >= 0 ? count.applyAsInt(x) : field[x]).toArray();
if (lineNum > 0) {
System.out.println();
}
System.out.println("Field #" + (++lineNum) + ":");
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
System.out.print(result[i * m + j] == -1 ? "*" : result[i * m + j]);
}
System.out.println();
}
}
}
}