-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineSweeper2.java
More file actions
74 lines (61 loc) · 1.93 KB
/
MineSweeper2.java
File metadata and controls
74 lines (61 loc) · 1.93 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
public class MineSweeper2{
public static void main(String[] args) {
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
boolean [][] board = new boolean [m+2][n+2];
for (int r = 1; r <= m ; r++){
for (int c = 1; c <= n; c++){
board[r][c] = p > Math.random();
}
}
for (int r = 1; r <= m ; r++){
for (int c = 1; c <= n; c++){
// System.out.print(board[r][c] ? "*" : ".");
if (board[r][c]){
System.out.print("* ");
}
else{
System.out.print(". ");
}
}
System.out.println();
}
System.out.print("\n");
for (int r = 1; r <= m ; r++){
for (int c = 1; c <= n; c++){
if (board[r][c]){
System.out.print("* ");
} else {
int count = 0;
if (board[r-1][c-1]){
count++;
}
if (board[r-1][c]){
count++;
}
if (board[r-1][c+1]){
count++;
}
if (board[r][c-1]){
count++;
}
if (board[r][c+1]){
count++;
}
if (board[r+1][c-1]){
count++;
}
if (board[r+1][c]){
count++;
}
if (board[r+1][c+1]){
count++;
}
System.out.print(count + " ");
}
}
System.out.println();
}
}
}