-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMineSweeper.java
118 lines (112 loc) · 3.73 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
public class MineSweeper
{
public static void main(String[] args) {
int rows ;
int col;
double p;
StdOut.println("Input how much rows ");
rows = StdIn.readInt();
while (rows<=0){
StdOut.println("Good bye ! ");
break ;
}
StdOut.println("Input how colns ");
col = StdIn.readInt();
while (col<=0){
StdOut.println("Good bye ! ");
break ;
}
StdOut.println("Input the probility");
p = StdIn.readDouble();
while (p<=0 && p>=1){
StdOut.println("Good bye ! ");
break ;
}
// part 1
boolean [] [] arr = makeAbooleanBoard(rows,col,p);
// part 2
PrintBoard(arr);
StdOut.println();
// part 3
PrintBoardOfNeighbor(arr);
// part 4
StdOut.print("Input the index of the line (-1 to exit the function)");
int i = StdIn.readInt();
StdOut.print("Input the index of the row (-1 to exit the function)");
int j = StdIn.readInt();
while(i != -1)
{
printrCordinate(arr,i,j);
StdOut.println();
StdOut.print("Input the index of the line (-1 to exit the function)");
i = StdIn.readInt();
if(i==-1)
break;
StdOut.print("Input the index of the row (-1 to exit the function)");
j = StdIn.readInt();
}
}
public static boolean [] [] makeAbooleanBoard(int rows,int col,double p){
// make the mine board
boolean [] [] myArr = new boolean [rows][col];
for(int i = 0 ; i < rows ;i++)
{
for (int j = 0 ; j < col ;j ++)
{
if(Math.random()<=p)
myArr[i][j]=true;
}
}
return myArr;
}
public static void PrintBoard(boolean arr [][] ){
// print the board
for(int i = 0 ; i < arr.length;i++){
for(int j = 0 ;j <arr[0].length;j++){
if (arr[i][j]==true)
StdOut.print(" * ");
else
StdOut.print(" . ");
}
StdOut.println();
}
}
public static void PrintBoardOfNeighbor(boolean arr [][]) {
// print the neighboor board
for (int i = 0 ; i < arr.length;i++){
for(int j = 0 ; j < arr[0].length; j ++){
if(arr[i][j]==true)
StdOut.print(" * ");
else
StdOut.print(checkNeighbor(arr,i,j)+" ");
}
StdOut.println();
}
}
public static int checkNeighbor(boolean [][] arr,int i , int j) {
// check the place near the mine
int count = 0 ;
for (int k = i-1 ; k < i +1 ; k++){
for(int l = j -1 ; l < j+1 ; l++){
if(k>=0 && k < arr.length && l >= 0 && l < arr[0].length && arr[k][l])
count ++;
}
}
return count;
}
public static void printrCordinate(boolean arr [][],int i ,int j){
// print part 4
int count = 0 ;
if(arr[i][j])
StdOut.print("There is a mine in " +"["+ i + ","+j+"]");
else
for (int k = i-1 ; k < i +1 ; k++){
for(int l = j -1 ; l < j+1 ; l++){
if(k>=0 && k < arr.length && l >= 0 && l < arr[0].length && arr[k][l]){
count ++;
}
}
}
StdOut.println("There are " +count+ " mines around ["+ i +"," +j + "]");
}
}