-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateMines.java
53 lines (43 loc) · 1.25 KB
/
generateMines.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
import java.util.*;
public class generateMines{
private static final int ROWS = 10;
private static final int COLM = 10;
public generateMines(int[][] minefield){
int BOMB_VALUE = 99;
int NUM_OF_MINES = 10;
myButton.bomb_trip = false;
System.out.println("Cell has been reset");
for(int [] row: minefield)
Arrays.fill(row, 0);
Random generator = new Random();
int num = generator.nextInt(100);
for(int i = 0; i < NUM_OF_MINES; i++){
minefield[num / 10][num % 10] = BOMB_VALUE;
num = generator.nextInt(100);
}
//marks the minefield to tell the user how
//many bombs surround this cell
for(int i = 0; i < ROWS; i++){
for(int j = 0; j < COLM; j++){
if(minefield[i][j] >= BOMB_VALUE){
if(j != 9) // East
minefield[i][j + 1] += 1;
if(j != 0) //West
minefield[i][j - 1] += 1;
if(i != 0) //North
minefield[i - 1][j] += 1;
if(i != 9) //South
minefield[i + 1][j] += 1;
if(i != 0 && j != 0 ) //North west
minefield[i - 1][j - 1] += 1;
if(i != 0 && j != 9) // North east
minefield[i - 1][j + 1] += 1;
if(i != 9 && j != 0) // South West
minefield[i + 1][j - 1] += 1;
if(i != 9 && j != 9)// South east
minefield[i + 1][j + 1] += 1;
}
}
}
}
}