-
Notifications
You must be signed in to change notification settings - Fork 0
/
TetrisPiece.java
406 lines (290 loc) · 12.1 KB
/
TetrisPiece.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package Tetris;
import javafx.animation.Timeline;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
/**
*This class models the Tetris pieces that are used in the game, which are made up of squares.
*/
public class TetrisPiece{
private TetrisSquare[]_tetrispiece;
private int _ycoord;
private int _xcoord;
/*
* This method constructs a Tetris piece, which is an array of four squares.
*/
public TetrisPiece() {
_tetrispiece= new TetrisSquare[4];
}
/*
* This method uses a switch statement to randomly create one of seven types of tetris pieces.
*/
public void factoryPattern(Pane _gamepane, TetrisSquare[][] _board, Timeline _timeline) {
int rand = (int)(Math.random()*7);
switch(rand) {
case 0:
for (int i = 0; i< _tetrispiece.length; i++) {
_tetrispiece[i]= new TetrisSquare();
_tetrispiece[i].getTetrisSquare().setFill(Color.PINK);
_tetrispiece[i].getTetrisSquare().setStroke(Color.WHITE);
_tetrispiece[i].getTetrisSquare().setX(Constants.PIECE1[i][0]+Constants.SQUARE_XOFFSET);
_tetrispiece[i].getTetrisSquare().setY(Constants.PIECE1[i][1]+Constants.PIECE_YOFFSET);
}
break;
case 1:
for (int i = 0; i< _tetrispiece.length; i++) {
_tetrispiece[i]= new TetrisSquare();
_tetrispiece[i].getTetrisSquare().setFill(Color.RED);
_tetrispiece[i].getTetrisSquare().setStroke(Color.WHITE);
_tetrispiece[i].getTetrisSquare().setX(Constants.PIECE2[i][0]+Constants.PIECE_XOFFSET);
_tetrispiece[i].getTetrisSquare().setY(Constants.PIECE2[i][1]+Constants.PIECE_YOFFSET);
}
break;
case 2:
for (int i = 0; i< _tetrispiece.length; i++) {
_tetrispiece[i]= new TetrisSquare();
_tetrispiece[i].getTetrisSquare().setFill(Color.ORANGE);
_tetrispiece[i].getTetrisSquare().setStroke(Color.WHITE);
_tetrispiece[i].getTetrisSquare().setX(Constants.PIECE3[i][0]+Constants.PIECE_XOFFSET);
_tetrispiece[i].getTetrisSquare().setY(Constants.PIECE3[i][1]+Constants.PIECE_YOFFSET);
}
break;
case 3:
for (int i = 0; i< _tetrispiece.length; i++) {
_tetrispiece[i]= new TetrisSquare();
_tetrispiece[i].getTetrisSquare().setFill(Color.YELLOW);
_tetrispiece[i].getTetrisSquare().setStroke(Color.WHITE);
_tetrispiece[i].getTetrisSquare().setX(Constants.PIECE4[i][0]+Constants.PIECE_XOFFSET);
_tetrispiece[i].getTetrisSquare().setY(Constants.PIECE4[i][1]+Constants.PIECE_YOFFSET);
}
break;
case 4:
for (int i = 0; i< _tetrispiece.length; i++) {
_tetrispiece[i]= new TetrisSquare();
_tetrispiece[i].getTetrisSquare().setFill(Color.GREEN);
_tetrispiece[i].getTetrisSquare().setStroke(Color.WHITE);
_tetrispiece[i].getTetrisSquare().setX(Constants.PIECE5[i][0]+Constants.PIECE_XOFFSET);
_tetrispiece[i].getTetrisSquare().setY(Constants.PIECE5[i][1]+Constants.PIECE_YOFFSET);
}
break;
case 5:
for (int i = 0; i< _tetrispiece.length; i++) {
_tetrispiece[i]= new TetrisSquare();
_tetrispiece[i].getTetrisSquare().setFill(Color.BLUE);
_tetrispiece[i].getTetrisSquare().setStroke(Color.WHITE);
_tetrispiece[i].getTetrisSquare().setX(Constants.PIECE6[i][0]+Constants.PIECE_XOFFSET);
_tetrispiece[i].getTetrisSquare().setY(Constants.PIECE6[i][1]+Constants.PIECE_YOFFSET);
}
break;
case 6:
for (int i = 0; i< _tetrispiece.length; i++) {
_tetrispiece[i]= new TetrisSquare();
_tetrispiece[i].getTetrisSquare().setFill(Color.PURPLE);
_tetrispiece[i].getTetrisSquare().setStroke(Color.WHITE);
_tetrispiece[i].getTetrisSquare().setX(Constants.PIECE7[i][0]+Constants.PIECE_XOFFSET);
_tetrispiece[i].getTetrisSquare().setY(Constants.PIECE7[i][1]+Constants.PIECE_YOFFSET);
}
break;
}
/*
* this if statement checks if the piece can be added to the board, and if so, adds it graphically. Otherwise, it starts the gameOver
* method which ends the game. It is placed here to check before each new piece is added graphically so that pieces at
* the top of the board do not overlap just before the game ends.
*/
if (moveValidity(_board)==true) {
for (int i=0; i<_tetrispiece.length; i++) {
_gamepane.getChildren().add(_tetrispiece[i].getTetrisSquare());
}
}
else {
TetrisPiece.this.checkgameOver(_board, _timeline, _gamepane);
}
}
/*
* This method checks if the game is over by checking if there are any squares in the top row. When the game is over, it stops the timeline and creates a label
* with text that visually tells the user that the game is over, and makes the game unresponsive to key presses.
*/
public void checkgameOver(TetrisSquare[][]_board, Timeline _timeline, Pane _gamepane) {
for (int i= 2; i<12; i++) {
if( _board[i][2]!=null){
_timeline.stop();
Label gameover = new Label("GAME OVER!! >:0");
gameover.setTextFill(Color.WHITE);
gameover.setLayoutX(Constants.LABEL_WIDTH);
gameover.setLayoutY(Constants.BOARD_HEIGHT/2);
_gamepane.getChildren().add(gameover);
_gamepane.setFocusTraversable(false);
}
}
}
/*
* This method moves the tetris piece down if it is able to move down (move validity is true), and saves the squares of the piece to the board + creates a
* new piece if it is not able to move down.
*/
public void moveDown(TetrisSquare[][] _board, Pane _gamepane, Timeline _timeline) {
if (downmoveValidity(_board)==true) {
for (int i=0; i<_tetrispiece.length; i++) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY()+Constants.SQUARE_SIZE)/Constants.SQUARE_SIZE;
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX())/Constants.SQUARE_SIZE;
_tetrispiece[i].getTetrisSquare().setY((_ycoord)*Constants.SQUARE_SIZE);
}
}
else {
for (int i=0; i<_tetrispiece.length; i++) {
if (_tetrispiece[i]!=null) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY()+Constants.SQUARE_SIZE)/Constants.SQUARE_SIZE;
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX())/Constants.SQUARE_SIZE;
System.out.println(_ycoord);
System.out.println(_xcoord);
_board[_xcoord][_ycoord-1]= _tetrispiece[i];
}
}
_gamepane.setFocusTraversable(false);
this.factoryPattern(_gamepane, _board, _timeline);
}
}
/*
* This method moves the currently active piece to the left is the move validity is true.
*/
public void moveLeft(TetrisSquare[][] _board, Pane _gamepane) {
if (leftmoveValidity(_board)==true) {
for (int i=0; i<_tetrispiece.length; i++) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY()/Constants.SQUARE_SIZE);
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX()-Constants.SQUARE_SIZE)/Constants.SQUARE_SIZE;
_tetrispiece[i].getTetrisSquare().setX((_xcoord)*Constants.SQUARE_SIZE);
}
}
else {
_gamepane.setFocusTraversable(false);
}
}
/*
* This method moves the currently active piece to the right is the move validity is true.
*/
public void moveRight(TetrisSquare[][] _board, Pane _gamepane) {
if (rightmoveValidity(_board)==true) {
for (int i=0; i<_tetrispiece.length; i++) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY()/Constants.SQUARE_SIZE);
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX()+Constants.SQUARE_SIZE)/Constants.SQUARE_SIZE;
_tetrispiece[i].getTetrisSquare().setX((_xcoord)*Constants.SQUARE_SIZE);
}
}
else {
_gamepane.setFocusTraversable(false);
}
}
/*
* This method rotates the piece 90 degrees counterclockwise if the move validity is true.
*/
public void rotate(TetrisSquare[][] _board, Pane _gamepane) {
if(rotateValidity(_board)==true) {
for (int i= 0; i<_tetrispiece.length;i++) {
double xcenter = _tetrispiece[2].getTetrisSquare().getX();
double ycenter = _tetrispiece[2].getTetrisSquare().getY();
if (_tetrispiece[i]!=null && _tetrispiece[i].getTetrisSquare().getFill()!=Color.PINK) {
double xloc =_tetrispiece[i].getTetrisSquare().getX();
double yloc = _tetrispiece[i].getTetrisSquare().getY();
_xcoord = (int) ( xcenter - ycenter+ yloc);
_ycoord= (int) (ycenter +xcenter - xloc);
_tetrispiece[i].getTetrisSquare().setX(_xcoord);
_tetrispiece[i].getTetrisSquare().setY(_ycoord);
}
}
}
else {
_gamepane.setFocusTraversable(false);
}
}
/*
* This method drops the piece down to the lowest possible position it can go without overlapping existing pieces; it uses a while statement so that it keeps
* moving the piece down while move validity is true until it returns false.
*/
public void drop(TetrisSquare[][]_board, Pane _gamepane) {
while (downmoveValidity(_board)==true) {
for (int i=0; i<_tetrispiece.length; i++) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY()+Constants.SQUARE_SIZE)/Constants.SQUARE_SIZE;
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX())/Constants.SQUARE_SIZE;
_tetrispiece[i].getTetrisSquare().setY((_ycoord)*Constants.SQUARE_SIZE);
}
}
}
/*
* This method checks if the piece can rotate in its current place on the board.
*/
private boolean rotateValidity(TetrisSquare[][]_board) {
for (int i=0; i<_tetrispiece.length; i++) {
double xcenter = _tetrispiece[2].getTetrisSquare().getX();
double ycenter = _tetrispiece[2].getTetrisSquare().getY();
if (_tetrispiece[i]!=null) {
double xloc =_tetrispiece[i].getTetrisSquare().getX();
double yloc = _tetrispiece[i].getTetrisSquare().getY();
_xcoord = (int) ( xcenter - ycenter+ yloc)/Constants.SQUARE_SIZE;
_ycoord= (int) (ycenter +xcenter - xloc)/Constants.SQUARE_SIZE;
if (_board[_xcoord][_ycoord]!=null){
return false;
}
}
}
return true;
}
/*
* This method checks if the piece can move down in its current place on the board.
*/
private boolean downmoveValidity(TetrisSquare[][]_board) {
for (int i=0; i<_tetrispiece.length; i++) {
if (_tetrispiece[i]!=null) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY()+Constants.SQUARE_SIZE)/Constants.SQUARE_SIZE;
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX())/Constants.SQUARE_SIZE;
if (_board[_xcoord][_ycoord]!=null){
return false;
}
}
}
return true;
}
/*
* This method checks if the piece can be created in its place on the board (it is called before each piece is graphically added to the board)
*/
private boolean moveValidity(TetrisSquare[][]_board) {
for (int i=0; i<_tetrispiece.length; i++) {
if (_tetrispiece[i]!=null) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY())/Constants.SQUARE_SIZE;
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX())/Constants.SQUARE_SIZE;
if (_board[_xcoord][_ycoord]!=null){
return false;
}
}
}
return true;
}
/*
* This method checks if the piece can move left in its current place on the board.
*/
private boolean leftmoveValidity(TetrisSquare[][]_board) {
for (int i=0; i<_tetrispiece.length; i++) {
if (_tetrispiece[i]!=null) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY())/Constants.SQUARE_SIZE;
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX()-Constants.SQUARE_SIZE)/Constants.SQUARE_SIZE;
if (_board[_xcoord][_ycoord]!=null){
return false;
}
}
}
return true;
}
/*
* This method checks if the piece can move right in its current place on the board.
*/
private boolean rightmoveValidity(TetrisSquare[][]_board) {
for (int i=0; i<_tetrispiece.length; i++) {
if (_tetrispiece[i]!=null) {
_ycoord= (int)(_tetrispiece[i].getTetrisSquare().getY())/Constants.SQUARE_SIZE;
_xcoord = (int)( _tetrispiece[i].getTetrisSquare().getX()+Constants.SQUARE_SIZE)/Constants.SQUARE_SIZE;
if (_board[_xcoord][_ycoord]!=null){
return false;
}
}
}
return true;
}
}