-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.java
335 lines (300 loc) · 7.94 KB
/
Map.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package map;
import java.lang.Math;
import game.GameConstants;
import bomb.Bomb;
import items.Item;
import player.Player;
/**
* Core class of the game. It is composed of Cell, and store a matrix of size
* CELL_NUM_Y*CELL_NUM_X.<br>
* Provides methods with params x, y to get access to the state of a certain
* cell.
*
* @author Zhuofan Chen
* @version 1.0
*/
public class Map implements GameConstants {
/*
* Define map matrix and its size. In matrix _map, the first index refers to the
* y axis position and the second index refers to the x axis position.
*/
private Cell[][] _map;
private int ySize = CELL_NUM_Y;
private int xSize = CELL_NUM_X;
/**
* Default construction method
*/
public Map() {
set_map(new Cell[ySize][xSize]); // initialize _map
for (int i = 0; i < ySize; i++)
for (int j = 0; j < xSize; j++) {
get_map()[i][j] = new Cell();
}
}
/**
* Construction method for given y size and x size
*/
public Map(int xSize, int ySize) {
this.ySize = ySize;
this.xSize = xSize;
set_map(new Cell[ySize][xSize]); // initialize _map;
for (int i = 0; i < ySize; i++)
for (int j = 0; j < xSize; j++) {
get_map()[i][j] = new Cell();
}
}
/**
* Construction method for given MapMatrix
*/
public Map(MapMatrix mmat) {
xSize = mmat.getXSize();
ySize = mmat.getYSize();
set_map(new Cell[ySize][xSize]);
for (int i = 0; i < ySize; i++)
for (int j = 0; j < xSize; j++) {
get_map()[i][j] = new Cell();
if (mmat.isWithDestructibleWall(i, j))
get_map()[i][j].setWall(true);
if (mmat.isWithIndestructibleWall(i, j))
get_map()[i][j].setWall(false);
}
}
/**
* Construction method for given int[][]
*/
public Map(int[][] wallMatrix) {
ySize = wallMatrix.length;
xSize = wallMatrix[0].length;
set_map(new Cell[ySize][xSize]);
for (int i = 0; i < ySize; i++)
for (int j = 0; j < xSize; j++) {
get_map()[i][j] = new Cell();
if (wallMatrix[i][j] == DESTRUCTIBLE)
get_map()[i][j].setWall(true);
if (wallMatrix[i][j] == INDESTRUCTIBLE)
get_map()[i][j].setWall(false);
}
}
/**
* Call all member methods needing refreshing
*/
public void refresh() {
for (int i = 0; i < ySize; i++)
for (int j = 0; j < xSize; j++)
get_map()[i][j].refresh();
}
/**
* @return the cell at given position for any operation on certain cell !!this
* method is NOT RECOMMENDED!!
* @deprecated
*/
public Cell getCell(int xPos, int yPos) {
return get_map()[yPos][xPos];
}
public Cell[][] get_map() {
return _map;
}
public void set_map(Cell[][] _map) {
this._map = _map;
}
/**
* Judge whether a certain position is out of map
*/
public boolean isInMap(int xPos, int yPos) {
return yPos >= 0 && yPos < ySize && xPos >= 0 && xPos < xSize;
}
/**
* @return if the cell of certain yPos and xPos is available
*/
public boolean isAvailable(int xPos, int yPos) {
return isInMap(xPos, yPos) && get_map()[yPos][xPos].isAvailable();
}
/**
* Get the xSize of the map
*/
public int getSizeX() {
return this.xSize;
}
/**
* Get the ySize of the map
*/
public int getSizeY() {
return this.ySize;
}
/**
* @param xPos x position to set bomb
* @param yPos y position to set bomb
* @param bombPower power of bomb
* @param owner the player who set the bomb
* @return if the bomb is successfully set
*/
public boolean setBomb(int xPos, int yPos, int bombPower, Player owner) {
if (isInMap(xPos, yPos) && get_map()[yPos][xPos].setBomb(new Bomb(xPos, yPos, bombPower, this, owner))) {
owner.addBombPlantedNumber();
return true;
}
return false;
}
/**
* @return if an bomb is successfully removed from given position
*/
public boolean removeBomb(int xPos, int yPos) {
return (isInMap(xPos, yPos) && get_map()[yPos][xPos].removeBomb());
}
/**
* @return if a bomb is on given position
*/
public boolean isWithBomb(int xPos, int yPos) {
return (isInMap(xPos, yPos) && get_map()[yPos][xPos].isWithBomb());
}
/**
* @param xPos, yPos position to set item
* @return if the item is successfully set
*/
public boolean setItem(int xPos, int yPos) {
if (isInMap(xPos, yPos) && get_map()[yPos][xPos].setItem(new Item(xPos, yPos))) {
return true;
}
return false;
}
/**
* Create an item at given position according to given chance in GameConstants
*
* @return if the item is successfully created
*/
public boolean createItem(int xPos, int yPos) {
if ((float) Math.random() < ITEM_CHANCE) {
return (setItem(xPos, yPos));
}
return false;
}
/**
* @return if an item is successfully removed from given position
*/
public boolean removeItem(int xPos, int yPos) {
return (isInMap(xPos, yPos) && get_map()[yPos][xPos].removeItem());
}
/**
* @return if an item is on given position
*/
public boolean isWithItem(int xPos, int yPos) {
return (isInMap(xPos, yPos) && get_map()[yPos][xPos].isWithItem());
}
/**
* @return ID of Item on given position
*/
public int getItemID(int xPos, int yPos) {
if (isWithItem(xPos, yPos))
return get_map()[yPos][xPos].getItemID();
return -1;
}
/**
* @param p the player to give the Item to
* @return
*/
public boolean giveItem(int xPos, int yPos, Player p) {
if (getItemID(xPos, yPos) == -1)
return false;
get_map()[yPos][xPos].getItem().getItem(p);
get_map()[yPos][xPos].removeItem();
return true;
}
/**
* Activate a explosion effect on given position
*/
public void explosionActivate(int xPos, int yPos) {
boolean ci = false;
if (isInMap(xPos, yPos)) {
if (get_map()[yPos][xPos].isWithDestructibleWall())
ci = true;
get_map()[yPos][xPos].explosionActivate();
if (ci)
createItem(xPos, yPos);
}
}
/**
* @return given position is now at explosion
*/
public boolean isAtExplosion(int xPos, int yPos) {
return (isInMap(xPos, yPos) && get_map()[yPos][xPos].isAtExplosion());
}
/**
* set a wall at given position
*
* @param destructible whether the wall to be set is destructible
*/
public void setWall(int xPos, int yPos, boolean destructible) {
if (isInMap(xPos, yPos))
get_map()[yPos][xPos].setWall(destructible);
}
/**
* remove wall from given position
*/
public void removeWall(int xPos, int yPos) {
if (isInMap(xPos, yPos))
get_map()[yPos][xPos].removeWall();
}
/**
* @return if a wall is on given position
*/
public boolean isWithWall(int xPos, int yPos) {
return (isInMap(xPos, yPos) && get_map()[yPos][xPos].isWithWall());
}
/**
* @return if a destructible wall is on given position
*/
public boolean isWithDestructibleWall(int xPos, int yPos) {
return (isInMap(xPos, yPos) && get_map()[yPos][xPos].isWithDestructibleWall());
}
/**
* @return if an indestructible wall is on given position
*/
public boolean isWithIndestructibleWall(int xPos, int yPos) {
return (isInMap(xPos, yPos) && get_map()[yPos][xPos].isWithIndestructibleWall());
}
/**
* @param power assumed bomb power
* @return true if given position is threatened by bomb, assuming all bomb with
* a certain power
*/
public boolean isInExplosionRange(int xPos, int yPos, int power) {
if (!isInMap(xPos, yPos))
return false;
if (isWithBomb(xPos, yPos))
return true;
for (int i = xPos - 1; i >= xPos - power; i--) {
if (!isInMap(i, yPos) || isWithWall(i, yPos))
break;
if (isWithBomb(i, yPos))
return true;
}
for (int i = xPos + 1; i <= xPos + power; i++) {
if (!isInMap(i, yPos) || isWithWall(i, yPos))
break;
if (isWithBomb(i, yPos))
return true;
}
for (int i = yPos - 1; i >= yPos - power; i--) {
if (!isInMap(xPos, i) || isWithWall(xPos, i))
break;
if (isWithBomb(xPos, i))
return true;
}
for (int i = yPos + 1; i <= yPos + power; i++) {
if (!isInMap(xPos, i) || isWithWall(xPos, i))
break;
if (isWithBomb(xPos, i))
return true;
}
return false;
}
public int getXSize() {
return xSize;
}
public int getYSize() {
return ySize;
}
public int getWallID(int y, int x) {
return get_map()[x][y].getWallID();
}
}