-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMatrix.java
More file actions
311 lines (262 loc) · 10.1 KB
/
TileMatrix.java
File metadata and controls
311 lines (262 loc) · 10.1 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
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
package coloris;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javafx.animation.KeyFrame;
import javafx.animation.ParallelTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.Timeline;
import javafx.scene.Group;
import javafx.util.Duration;
public class TileMatrix implements Sprite
{
private static final double SHOW_DESTROY_SPRITE_IN_MILLIS = 200.0;
private final GameInfo gameInfo;
private final int rowCount;
private final int colCount;
private final int[] noTiles;
private final Tile[][] matrix;
boolean updateInProgress;
private final ArrayList<Tile> toDestroy;
private int previousScore;
private int score;
private final NumberDisplayer scoreDisplayer;
private int columnLastAddedTo;
public TileMatrix(GameInfo gameInfo, NumberDisplayer scoreDisplayer)
{
this.gameInfo = gameInfo;
this.rowCount = gameInfo.getRowCount();
this.colCount = gameInfo.getColCount();
noTiles = new int[colCount];
matrix = new Tile[rowCount][colCount];
updateInProgress = false;
toDestroy = new ArrayList<>();
previousScore = 0;
score = 0;
this.scoreDisplayer = scoreDisplayer;
columnLastAddedTo = -1;
}
@Override
public void update()
{
setUpdateInProgress(true);
updateMatrix();
}
private void updateMatrix()
{
boolean isToRemove = false;
HashMap<Tile, Pair> toBeRemoved = new HashMap<>();
for(int i = 0; i < getRowCount(); i++) //promenio
{
for(int j = 0; j < getColCount(); j++)
{
if (matrix[i][j] != null)
{
boolean isToRemoveV = checkForSameColorNeighbours(i, j, 'v', toBeRemoved);
boolean isToRemoveH = checkForSameColorNeighbours(i, j, 'h', toBeRemoved);
isToRemove = isToRemove || isToRemoveV || isToRemoveH;
}
}
}
if (isToRemove == true)
{
setToNull(toBeRemoved);
//najobicniji nacin da se pozove funkcija destroy sa delay-om, da bi tile-ovi zadrzali destroy sprite 0.2 sekunde,
//mozda postoji bolji nacin, razmisli o tome
Timeline timeline = new Timeline(
new KeyFrame(Duration.millis(SHOW_DESTROY_SPRITE_IN_MILLIS), e -> destroy())
);
timeline.play();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else
{
previousScore = score;
setUpdateInProgress(false);
}
}
private void setToNull(HashMap<Tile, Pair> toBeDestroyed) //setuje sprite-ove za unistenje i vrednosti tih tile-ova u matrici na null
{
for(Map.Entry<Tile, Pair> entry : toBeDestroyed.entrySet())
{
Pair indices = entry.getValue();
int row = indices.getRow();
int col = indices.getCol();
toDestroy.add(matrix[row][col]);
matrix[row][col].setDestroySprite();
matrix[row][col].setWidth(matrix[row][col].getWidth() + 1); //mnogo dobra forica
matrix[row][col].setHeight(matrix[row][col].getHeight() + 1); //mnogo dobra forica
matrix[row][col] = null;
}
}
private void destroy()
{
score += toDestroy.size();
if (score > scoreDisplayer.getMaxNumber())
score -= scoreDisplayer.getMaxNumber();
Group group = (Group)toDestroy.get(0).getParent();
group.getChildren().removeAll(toDestroy);
toDestroy.clear();
scoreDisplayer.setNumber(score);
scoreDisplayer.update();
rearrange();
}
private void rearrange()
{
ParallelTransition pt = new ParallelTransition();
pt.setOnFinished(e -> { //u slucaju da je unisteno >= 5 onda treba da se unisti poslednji red, pa tek onda da se pozove update matrix
if (score - previousScore >= 5)
destroyLastRow();
else
updateMatrix();
});
for(int i = 0; i < getColCount(); i++) //istestiraj ovaj deo algoritma
{
int j = 0;
while(j < noTiles[i] && matrix[j][i] != null)
j++;
int k = j;
while(k < noTiles[i] && matrix[k][i] == null)
k++;
while(k < noTiles[i])
{
if (matrix[k][i] != null)
{
matrix[j][i] = matrix[k][i];
matrix[k][i] = null;
pt.getChildren().add(matrix[j][i].createTranslation(k - j));
j++;
}
k++;
}
noTiles[i] = j;
}
pt.play();
}
private boolean checkForSameColorNeighbours(int row, int col, char direction, HashMap<Tile, Pair> toBeRemoved) //direction can be vertical ('v') or horizontal ('h')
{
boolean removed = false;
Tile tile = matrix[row][col];
int k = (direction == 'v' ? row + 1 : col + 1);
int limit = (direction == 'v' ? getRowCount() : getColCount());
Tile nextTile = null;
if (k < limit)
nextTile = (direction == 'v' ? matrix[k][col] : matrix[row][k]);
while(k < limit && nextTile != null && nextTile.sameColor(tile))
{
k++;
if (k < limit)
nextTile = (direction == 'v' ? matrix[k][col] : matrix[row][k]);
}
int noSameColorTiles = (direction == 'v' ? k - row : k - col);
if (noSameColorTiles >= 3) //treba najmanje tri da se skupe da bi doslo do unistenja
{
int n = (direction == 'v' ? row : col);
while(n < k)
{
Tile currentTile = (direction == 'v' ? matrix[n][col] : matrix[row][n]);
if (toBeRemoved.containsKey(currentTile) == false)
{
Pair indices = (direction == 'v' ? new Pair(n, col) : new Pair(row, n));
toBeRemoved.put(currentTile, indices);
}
n++;
}
removed = true;
}
return removed;
}
private void destroyLastRow()
{
SequentialTransition sq = new SequentialTransition();
for (int i = 0; i < getColCount(); i++)
{
boolean somethingToDestroy = false;
int j = 0;
if (matrix[j][i] != null)
{
somethingToDestroy = true;
toDestroy.add(matrix[j][i]);
}
ParallelTransition pt = new ParallelTransition();
while (j < noTiles[i] - 1)
{
pt.getChildren().add(matrix[j + 1][i].createTranslation(1));
matrix[j][i] = matrix[j + 1][i];
j++;
}
if (noTiles[i] > 0)
{
matrix[j][i] = null;
noTiles[i]--;
}
if (somethingToDestroy == true)
{
Timeline timeline = new Timeline(
new KeyFrame(Duration.millis(0.0),
(e) -> {
toDestroy.get(0).setDestroySprite();
toDestroy.get(0).setWidth(toDestroy.get(0).getWidth() + 1);
toDestroy.get(0).setHeight(toDestroy.get(0).getHeight() + 1);
}),
new KeyFrame(Duration.millis(SHOW_DESTROY_SPRITE_IN_MILLIS),
(e) -> {
Group parent = (Group)toDestroy.get(0).getParent();
parent.getChildren().remove(toDestroy.get(0));
toDestroy.remove(0);
score++;
scoreDisplayer.setNumber(score);
scoreDisplayer.update();
if (pt.getChildren().size() > 0)
pt.play();
}),
new KeyFrame(pt.getCycleDuration())
);
sq.getChildren().add(timeline);
}
}
sq.setOnFinished((e) -> {
previousScore = score;
updateMatrix();
});
sq.play();
}
public void addToColumn(int col, ArrayList<Tile> tiles)
{
columnLastAddedTo = col;
for(int row = 0; row < getRowCount(); row++)
{
if (matrix[row][col] == null)
{
for(int j = 0; j < tiles.size(); j++)
{
matrix[row + j][col] = tiles.get(tiles.size() - j - 1);
}
noTiles[col] += tiles.size();
return;
}
}
}
public void print()
{
for(int i = getRowCount() - 1; i >= 0; i--)
{
for(int j = 0; j < getColCount(); j++)
{
if (matrix[i][j] != null)
System.out.print(matrix[i][j] + " ");
else
System.out.print("O ");
}
System.out.println();
}
System.out.println();
}
public int getRowCount() {return rowCount;}
public int getColCount() {return colCount;}
public int getNoTiles(int col) {return noTiles[col];}
public int getScore() {return score;}
public boolean isUpdateInProgress() {return updateInProgress;}
public void setUpdateInProgress(boolean updateInProgress) {this.updateInProgress = updateInProgress;}
public boolean isGameover() {return noTiles[columnLastAddedTo] > gameInfo.getMaxColHeight();}
}