-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.java
More file actions
218 lines (206 loc) · 8.51 KB
/
Grid.java
File metadata and controls
218 lines (206 loc) · 8.51 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import objectdraw.*;
import java.awt.*;
/**
* Write the game logic in this class.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Grid
{
private GridCell[][] gridArray = new GridCell[10][10];//10*10 grid
private DrawingCanvas canvas; //the canvas
private int clickedCells = 0;
private int[] mineArray = new int[10];//the mine array
private int flagCount;
public Grid(double x, double y, double width, double height, DrawingCanvas canvas){
this.canvas = canvas;
double cellWdith = width/10.0; //the width of one cell
double cellHeight = height/10.0; //the height of one cell
//create the grid
double curY = y; //the starting x location
double curX = x; //the starting y locstion
for(int i = 0; i < gridArray.length; i ++){
for (int j = 0; j < gridArray[i].length; j ++){
//contructing 10*10 gird
gridArray[i][j] = new GridCell (curX + i*cellWdith, curY + j*cellHeight,
cellWdith, cellHeight, canvas);
}
}
//create the lines
for(int n = 1; n < gridArray.length; n ++){
//vertical line
new Line(curX + n*cellWdith, y, curX + n*cellWdith, y + height, canvas);
//horizontal line
new Line(x, curY + n*cellHeight, x + width, curY + n*cellHeight, canvas);
}
}
public void placeAllMines(){
placeMine();//place the mines on the grid
//show ten mines on the grid
for(int row = 0; row < gridArray.length; row ++){
for(int col = 0; col < gridArray[row].length; col ++){
if(gridArray[row][col].isMine()){
gridArray[row][col].setAMine();//show the mine
}
}
}
checkMineOnGrid();
}
public void placeMine(){
//add ten mines on grid
for (int i = 0; i < mineArray.length; i ++){
addOneMine(); //set one mine ten times
}
}
//add one mine on the grid
public void addOneMine(){
//generate one random cell
GridCell curCell;
RandomIntGenerator randomMineGen = new RandomIntGenerator (0, 9);
int row, col;
do{
row = randomMineGen.nextValue();//the row
col = randomMineGen.nextValue();//the col
curCell = gridArray[row][col];
}while(curCell.isMine());
curCell.setAMine();//put the mine on the cell
}
public int neighborMines(int row, int col, GridCell selected){
int rowStart = 0;
int colStart = 0;
int neighbor = 0;
if(row > 0){
//if it's not the first row, go up one row
rowStart = row -1;
}
if(col > 0){
//if it's not the first col, go left one row
colStart = col - 1;
}
//use for loop to check boundary and check eight neighbors
for(int i = rowStart; i <= row + 1 && i < gridArray.length; i++){
for(int k = colStart; k <= col + 1 && k < gridArray[i].length; k++){
if(gridArray[i][k].isMine() && gridArray[i][k] != gridArray[row][col]){
neighbor++;
}
}
}
return neighbor;//return the neighbor mine number
}
public void checkMineOnGrid(){
//check every cell on the grid
for(int row = 0; row < gridArray.length; row ++){
for(int col = 0; col < gridArray[row].length; col ++){
int x = neighborMines(row, col, gridArray[row][col]);
gridArray[row][col].setNeighbor(x);
}
}
}
public void placeMineNum(){
//show ten mines on the grid
int neighbor = 0;
for(int row = 0; row < gridArray.length; row ++){
for(int col = 0; col < gridArray[row].length; col ++){
neighbor = neighborMines(row, col, gridArray[row][col]);
if(neighbor != 0 && gridArray[row][col].isMine() == false){
//don't show 0 and if the cell has mine don't show the number
gridArray[row][col].mineAround(neighbor);
}
}
}
}
public void checkGrid(Location loc){
placeMineNum();
for(int row = 0; row < gridArray.length; row++){
for(int col = 0; col < gridArray[row].length; col ++){
if(gridArray[row][col].exposeCell() == false){
if(gridArray[row][col].contains(loc)){
if (gridArray[row][col].isMine() == true){
//if the cell is mine
gridArray[row][col].explodeAMine();//explode the mine
loseGame();//show lost the game
showAllMines();//show all the mines
}else if (gridArray[row][col].getNei() != 0){
//if the cell has number but not 0
gridArray[row][col].showTheCount();//show the count
clickedCells ++;//exposed cell plus one
}else{
//if the cell has no mine and it's 0
gridArray[row][col].showCell();//change the background
clickedCells ++;//exposed cell plus one
}
}
}
}
}
}
public void showAllMines(){
//show every mine
for(int row = 0; row < gridArray.length; row ++){
for(int col = 0; col < gridArray[row].length; col ++){
if(gridArray[row][col].isMine() == true){//go through every cell to chaeck if it has mine
gridArray[row][col].showAMine();//if the cell has mine, show the rest of the mines
}
}
}
}
public void loseGame(){
//the text of losing the game
Text result = new Text("YOU LOSE", 0, 0, canvas);
result.setFontSize(80);//the size
result.setColor(Color.ORANGE);//the color of the text
double xLoc = canvas.getWidth()/2 - result.getWidth()/2;//the X location
double yLoc = canvas.getHeight()/2 - result.getHeight()/2;//the Y location
result.moveTo(xLoc, yLoc);//move to the right location
}
public void winGame(){
//the text of winning the game
Text result = new Text("YOU WIN", 0, 0, canvas);
result.setFontSize(80);//the size
result.setColor(Color.YELLOW);//the color of the text
double xLoc = canvas.getWidth()/2 - result.getWidth()/2;//the X location
double yLoc = canvas.getHeight()/2 - result.getHeight()/2;//the Y location
result.moveTo(xLoc, yLoc);//move to the right location
}
public void flagCells(Location loc){
for(int row = 0; row < gridArray.length; row ++){
for(int col = 0; col < gridArray[row].length; col ++){
if(gridArray[row][col].exposeCell() == false){//to check if the cell is exposed
if(gridArray[row][col].contains(loc)){//to check if the cell is clicked
gridArray[row][col].setAFlag(); //flage the cell
}
}
}
}
}
public int getFlagCount(){
int flagCount = 0;
for(int row = 0; row < gridArray.length; row ++){
for(int col = 0; col < gridArray[row].length; col ++){
if(gridArray[row][col].isAFlag() == true){//if the cell is flagged
flagCount ++; //+1
}
}
}
return flagCount;
}
public void winTheGame(){
int minesFound = 0;
for(int row = 0; row < gridArray.length; row ++){
for(int col = 0; col < gridArray[row].length; col ++){
if(gridArray[row][col].isAFlag() == true && gridArray[row][col].isMine() == true){
//if the cell is mine and is flagged
minesFound++;//+1
}
if(clickedCells == gridArray.length*gridArray.length - mineArray.length ){
//if all the cells are exposed except the mines
if(minesFound == mineArray.length){
//if the flagged mines are the actual mines
winGame();//then win the game
}
}
}
}
}
}