-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
119 lines (101 loc) · 3.72 KB
/
Grid.java
File metadata and controls
119 lines (101 loc) · 3.72 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
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
import java.util.Random;
public class Grid {
private int numRows;
private int numColumns;
private int numBombs;
private boolean[][] bombGrid;
private int[][] countGrid;
public Grid() {
numRows = 10;
numColumns = 10;
numBombs = 25;
bombGrid = createBombGrid();
countGrid = createCountGrid();
}
public Grid(int numRows, int numColumns) {
this.numRows = numRows;
this.numColumns = numColumns;
numBombs = 25;
bombGrid = createBombGrid();
countGrid = createCountGrid();
}
public Grid(int numRows, int numColumns, int numBombs) {
this.numRows = numRows;
this.numColumns = numColumns;
this.numBombs = numBombs;
bombGrid = createBombGrid();
countGrid = createCountGrid();
}
public int getNumRows() {
return numRows;
}
public int getNumColumns() {
return numColumns;
}
public int getNumBombs() {
return numBombs;
}
public boolean[][] getBombGrid() {
boolean [][] copy = new boolean[bombGrid.length][];
for(int i = 0; i < bombGrid.length; i++) {
copy[i] = bombGrid[i].clone();
}
return copy;
}
public int[][] getCountGrid() {
int [][] copy = new int[countGrid.length][];
for(int i = 0; i < countGrid.length; i++) {
copy[i] = countGrid[i].clone();
}
return copy;
}
public boolean isBombAtLocation(int row, int column) {
if (row >= 0 && row < countGrid.length && column >= 0 && column < countGrid[row].length) {
if (bombGrid[row][column]) {
return true;
} }
return false;
}
public int getCountAtLocation(int row, int column) {
if (row >= 0 && row < countGrid.length && column >= 0 && column < countGrid[row].length) {
return countGrid[row][column];
}
return -1;
}
public boolean[][] createBombGrid() {
boolean[][] bombGrid = new boolean[numRows][numColumns];
for (int i = 0; i < numBombs; i++) {
Random rand = new Random();
int row = rand.nextInt(numRows);
int col = rand.nextInt(numColumns);
while(bombGrid[row][col]) {
row = rand.nextInt(numRows);
col = rand.nextInt(numColumns);
}
bombGrid[row][col] = true;
}
return bombGrid;
}
public int[][] createCountGrid() {
int[][] countGrid = new int[numRows][numColumns];
for (int row = 0; row < countGrid.length; ++row) {
for (int col = 0; col < countGrid[row].length; ++col) {
countGrid[row][col] = ((row - 1 >= 0) && (col - 1 >= 0) && bombGrid[row - 1][col - 1]) ? countGrid[row][col] + 1 : countGrid[row][col];
countGrid[row][col] = ((row - 1 >= 0) && bombGrid[row - 1][col]) ? countGrid[row][col] + 1 : countGrid[row][col];
countGrid[row][col] = ((row - 1 >= 0) && (col + 1 < countGrid.length) && bombGrid[row - 1][col + 1]) ? countGrid[row][col] + 1 : countGrid[row][col];
countGrid[row][col] = ((col - 1 >= 0) && bombGrid[row][col - 1]) ? countGrid[row][col] + 1 : countGrid[row][col];
countGrid[row][col] = (bombGrid[row][col]) ? countGrid[row][col] + 1 : countGrid[row][col];
countGrid[row][col] = ((col + 1 < countGrid.length) && bombGrid[row][col + 1]) ? countGrid[row][col] + 1 : countGrid[row][col];
countGrid[row][col] = ((row + 1 < countGrid.length) && (col - 1 >= 0) && bombGrid[row + 1][col - 1]) ? countGrid[row][col] + 1 : countGrid[row][col];
countGrid[row][col] = ((row + 1 < countGrid.length) && bombGrid[row + 1][col]) ? countGrid[row][col] + 1 : countGrid[row][col];
countGrid[row][col] = ((row + 1 < countGrid.length) && (col + 1 < countGrid.length) && bombGrid[row + 1][col + 1]) ? countGrid[row][col] + 1 : countGrid[row][col];
}
}
return countGrid;
}
// (row - 1, col - 1) (row - 1, col) (row - 1, col + 1)
// (row, col - 1) (row, col) (row, col + 1)
// (row + 1, col - 1) (row + 1, col) (row + 1, col + 1)
public static void main(String[] args) {
}
}