-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
256 lines (233 loc) · 9.69 KB
/
Copy pathBoard.java
File metadata and controls
256 lines (233 loc) · 9.69 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import java.util.Random;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
public class Board {
private int boardHeight = 10;
private int boardWidth = 20;
private int numMines = 15;
private int displayWidth = 42;
private int displayHeight = 28;
private int xPos = 0; //of top left part of screen in correspondence with the x pos on board
private int yPos = 0; //of top left part of screen in correspondence with the y pos on board
private int numFlagsPlaced = 0; //cannot exceed number of mines on the board
private boolean newGame = true;
Tile[][] board;
public Board(int boardHeight, int boardWidth, int numMines) {
this.boardHeight = boardHeight;
this.boardWidth = boardWidth;
this.numMines = numMines;
board = new Tile[boardHeight][boardWidth];
if(boardWidth < displayWidth)
displayWidth = boardWidth;
if(boardHeight < displayHeight)
displayHeight = boardHeight;
}
private void placeRandomMines() {
Random rand = new Random();
int numMinesPlaced = 0;
while(numMinesPlaced < numMines) {
int randomRow = rand.nextInt(boardHeight);
int randomColumn = rand.nextInt(boardWidth);
if(board[randomRow][randomColumn] == null) {
board[randomRow][randomColumn] = new Tile(true);
numMinesPlaced++;
}
}
}
//Sets the tiles' 'numMinesTouching' value to the correct value
private void readNumMinesTouching(int r, int c) {
int count = 0;
for(int x = c-1; x < c+2; x++) {
for(int y = r-1; y < r+2; y++) {
boolean isSameBlock = (x==c) && (y==r);
if(x >= 0 && x < boardWidth && y >= 0 && y < boardHeight && !isSameBlock) {
if(board[y][x].isMine())
count++;
}
}
}
board[r][c].setNumMinesTouching(count);
}
public boolean isNewGame() {
return newGame;
}
public void setFlagged(int r, int c) {
if(r >= 0 && r < boardHeight && c >= 0 && c < boardWidth) {
if(numFlagsPlaced + 1 <= numMines && !board[r][c].isFlagged() && !board[r][c].isVisible()) {
board[r][c].setFlagged();
numFlagsPlaced++;
}
}
}
public void setUnflagged(int r, int c) {
if(r >= 0 && r < boardHeight && c >= 0 && c < boardWidth) {
if(numFlagsPlaced - 1 >= 0 && board[r][c].isFlagged()) {
board[r][c].setUnflagged();
numFlagsPlaced--;
}
}
}
public void toggleFlagged(int r, int c) {
if(board[r][c].isFlagged())
setUnflagged(r, c);
else
setFlagged(r, c);
}
//returns if the player is safe
public boolean probe(int r, int c) {
if(newGame)
newProbe(r, c);
if(r >= 0 && r < boardHeight && c >= 0 && c < boardWidth && !board[r][c].isVisible() && !board[r][c].isFlagged()) {
if(board[r][c].isMine()) {
return false;
}
else if(board[r][c].getNumMinesTouching() > 0) {
board[r][c].setVisible();
return true;
}
else{ //when numMinesTouching == 0
board[r][c].setVisible();
probe(r-1, c);
probe(r-1, c-1);
probe(r+1, c);
probe(r+1, c+1);
probe(r, c-1);
probe(r+1, c-1);
probe(r, c+1);
probe(r-1, c+1);
return true;
}
}
else
return true;
}
public void newProbe(int r, int c) {
if(r >= 0 && r < boardHeight && c >= 0 && c < boardWidth) {
newGame = false;
emptyTile(r,c);
if((boardWidth * boardHeight) - numMines >= 9) {
emptyTile(r-1,c-1);
emptyTile(r-1,c);
emptyTile(r-1,c+1);
emptyTile(r,c-1);
emptyTile(r,c+1);
emptyTile(r+1,c-1);
emptyTile(r+1,c);
emptyTile(r+1,c+1);
}
placeRandomMines();
for(int i = 0; i < boardHeight; i++) {
for(int j = 0; j < boardWidth; j++) {
if(board[i][j] == null)
board[i][j] = new Tile(false);
}
}
for(int i = 0; i < boardHeight; i++) {
for(int j = 0; j < boardWidth; j++) {
readNumMinesTouching(i, j);
}
}
}
}
private boolean emptyTile(int r, int c) {
if(r >= 0 && r < boardHeight && c >= 0 && c < boardWidth) {
board[r][c] = new Tile(false);
}
return (r >= 0 && r < boardHeight && c >= 0 && c < boardWidth);
}
public void moveLeft() {
if(xPos > 0) {
xPos--;
}
}
public void moveRight() {
if(xPos + displayWidth < boardWidth) {
xPos++;
}
}
public void moveUp() {
if(yPos > 0) {
yPos--;
}
}
public void moveDown() {
if(yPos + displayHeight < boardHeight) {
yPos++;
}
}
public void display(boolean gameover, BufferedImage image) {
//Game.clearScreen(image);
for(int r = 0; r < displayHeight; r++) { //int r = 0; r < boardHeight; r++
for(int c = 0; c < displayWidth; c++) { //int c = 0; c < boardWidth; c++
if(board[r+yPos][c+xPos] != null && !gameover && board[r+yPos][c+xPos].isFlagged()) {
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.flagged)); //flag
}
else if(board[r+yPos][c+xPos] != null && gameover && board[r+yPos][c+xPos].isMine()) {
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.mine)); //is displayed during gameover
}
else if(board[r+yPos][c+xPos] == null || (!gameover && !board[r+yPos][c+xPos].isVisible())) {
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.undiscovered)); //is displayed for tiles that are not visible
}
else if(board[r+yPos][c+xPos].getNumMinesTouching() > 0) {
switch(board[r+yPos][c+xPos].getNumMinesTouching()) {
case 1:
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_1));
break;
case 2:
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_2));
break;
case 3:
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_3));
break;
case 4:
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_4));
break;
case 5:
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_5));
break;
case 6:
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_6));
break;
case 7:
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_7));
break;
case 8:
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_8));
break;
}
}
else if(board[r+yPos][c+xPos].getNumMinesTouching() == 0) { //board[r][c].getNumMinesTouching() == 0
drawImage(c, r, image, TextureUtilities.getTexture(TextureUtilities.TextureTag.discovered_empty)); //is displayed for tiles that have a 'numMinesTouching' value of 0
}
}
}
}
public boolean checkIfWon() {
int counter = (boardWidth * boardHeight) - numMines;
for(int r = 0; r < boardHeight; r++) {
for(int c = 0; c < boardWidth; c++) {
if(board[r][c].isVisible())
counter--;
}
}
return counter==0;
}
private void drawImage(int xPosition, int yPosition, BufferedImage image, BufferedImage texture) {
xPosition *= TextureUtilities.textureWidth;
yPosition *= TextureUtilities.textureHeight;
for(int x = 0; x < TextureUtilities.textureWidth; x++) {
for(int y = 0; y < TextureUtilities.textureHeight; y++) {
image.setRGB(Game.gameAreaOffset_left+xPosition+x, Game.gameAreaOffset_top+yPosition+y, texture.getRGB(x, y));
}
}
}
public int getXPos() {
return xPos;
}
public int getYPos() {
return yPos;
}
public void displayData(Graphics g) {
g.drawString("Flags: "+(numMines-numFlagsPlaced)+" "+xPos+", "+yPos, 10, 3 * g.getFontMetrics().getHeight());
}
}