-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.java
177 lines (152 loc) · 4.45 KB
/
Grid.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class Grid
{
private Square[][] grid;
private int numUncovered;
private int numMines;
private int numRow;
private int numCol;
public Grid(int numRow, int numCol, int numMines) {
//Initialize grid
this.grid = new Square[numRow][numCol];
this.numUncovered = 0;
this.numRow = numRow;
this.numCol = numCol;
this.numMines = numMines;
//Fill in grid with default squares
for(int row=0; row<numRow; row++) {
for(int col=0; col<numCol; col++) {
grid[row][col] = new Square();
}
}
}
public void setRandomMineLocs(int numMines, int chosenIndex)
{
ArrayList<Integer> available = new ArrayList<Integer>();
for(int c = 0; c < grid.length * grid[0].length; c++) {
available.add(c);
}
available.remove(chosenIndex);
Random rand = new Random();
for(int m = 0; m < numMines; m++) {
int randIndex = rand.nextInt(available.size());
int a = available.get(randIndex);
grid[a/grid[0].length][a%grid[0].length].setMine();
available.remove(randIndex);
}
}
public void setMineNums() {
int count;
for(int r=0; r<numRow; r++) {
for(int c=0; c<numCol; c++) {
count=0;
for(Integer[] i : getAdjacentSquares(new Integer[]{r,c})) {
if(grid[i[0]][i[1]].isMine()==1) {
count++;
}
}
grid[r][c].setNumMines(count);
}
}
}
public ArrayList<Integer[]> getAdjacentSquares(Integer[] center) {
ArrayList<Integer[]> adjacencyList = new ArrayList<Integer[]>();
int row = center[0];
int col = center[1];
int lastRow = numRow-1;
int lastCol = numCol-1;
if(row!=0) {
adjacencyList.add(new Integer[]{row-1, col});
if(col!=0) {
adjacencyList.add(new Integer[]{row-1, col-1});
}
if(col!=lastCol) {
adjacencyList.add(new Integer[]{row-1, col+1});
}
}
if(col!=0) {
adjacencyList.add(new Integer[]{row, col-1});
if(row!=lastRow) {
adjacencyList.add(new Integer[]{row+1, col-1});
}
}
if(row!=lastRow) {
adjacencyList.add(new Integer[]{row+1, col});
if(col!=lastCol) {
adjacencyList.add(new Integer[]{row+1, col+1});
}
}
if(col!=lastCol) {
adjacencyList.add(new Integer[]{row, col+1});
}
return adjacencyList;
}
public Square[][] getGrid()
{
return grid;
}
public int getNumMines()
{
return numMines;
}
public void revealMines() {
for(int r=0; r<grid.length;r++) {
for(int c=0; c<grid[0].length;c++) {
if(grid[r][c].isMine()==1) {
grid[r][c].uncover();
}
}
}
}
//Returns 0 if location is a mine
//Otherwise, returns number of squares it uncovers
public ArrayList<Integer[]> uncoverSquares(Integer[] startLoc, ArrayList<Integer[]> updatedLocs)
{
int row = startLoc[0];
int col = startLoc[1];
ArrayList<Integer[]> v = new ArrayList<Integer[]>();
if(grid[row][col].isMine()==0 && grid[row][col].isCovered()) {
grid[row][col].uncover();
v.add(startLoc);
if(grid[row][col].getNumMines()==0) {
for(Integer[] t : getAdjacentSquares(startLoc)) {
if(!updatedLocs.contains(t)) {
for(Integer[] s : uncoverSquares(t, updatedLocs)) {
updatedLocs.add(s);
v.add(s);
}
}
}
}
}
return v;
}
public ArrayList<Integer[]> processMove(int clickNum, Integer[] loc)
{
ArrayList<Integer[]> updatedSquares=new ArrayList<Integer[]>();
if(clickNum==3) {
grid[loc[0]][loc[1]].changeFlag();
updatedSquares.add(loc);
} else if(clickNum==1) {
updatedSquares = uncoverSquares(loc, new ArrayList<Integer[]>());
numUncovered += updatedSquares.size();
if(checkVictory()) {
ArrayList<Integer[]> win = new ArrayList<Integer[]>();
win.add(new Integer[]{-2,0});
return win;
} else if(grid[loc[0]][loc[1]].isMine()==1) {
ArrayList<Integer[]> gameOver = new ArrayList<Integer[]>();
gameOver.add(new Integer[]{-1,0});
return gameOver;
} else {
return updatedSquares;
}
}
return updatedSquares;
}
public boolean checkVictory() {
return (numUncovered==numRow*numCol-numMines);
}
}