-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetris.java
More file actions
601 lines (536 loc) · 16.8 KB
/
Tetris.java
File metadata and controls
601 lines (536 loc) · 16.8 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Austin Trinh
// March 23, 2022
// ITCS
// Tetris Object Classs
// Import statements
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
// Parameters of Interest:
// Bumpiness
// Holes
// Lines Cleared
public class Tetris {
// Fields
private int[][] grid;
private int scale;
private int x;
private int y;
private Tetromino tetromino;
private int rows = 24; // Default is 24
private int columns = 10; // Default is 10
private int rowsDeleted = 0;
private int score;
private int linesCleared;
private Color background = Color.black;//Color.blue.darker().darker().darker();
private ArrayList<Integer> rowDeleted = new ArrayList<Integer>();
private boolean lineCleared;
private boolean holdingbay = false;
private Color outline = Color.WHITE.darker().darker().darker().darker().darker().darker();
private boolean didTetris = false;
private boolean backToBackTetris = false;
//private Color outline = Color.WHITE;
//Tetris Default Constructor
public Tetris() {
// Instantiating fields
x = 0;
y = 0;
scale = 25;
// Creating the 2D grid and setting all integers to 0
grid = new int[rows][columns];
for(int r=0; r<rows; r++) {
for(int c=0; c<columns; c++) {
grid[r][c] = 0;
}
}
}
// Tetris 3-Argument Constructor
public Tetris(int x, int y, int scale) {
// Instantiating fields
this.x = x;
this.y = y;
this.scale = scale;
// Creating the 2D grid and setting all integers to 0
grid = new int[rows][columns];
for(int r=0; r<rows; r++) {
for(int c=0; c<columns; c++) {
grid[r][c] = 0;
}
}
}
// Constructor for holdingbay specifically
public Tetris(int x, int y, int scale, int rows, int columns) {
this.x = x;
this.y = y;
this.scale = scale;
this.rows = rows;
this.columns = columns;
holdingbay = true;
// Creating the 2D grid and setting all integers to 0
grid = new int[rows][columns];
for(int r=0; r<rows; r++) {
for(int c=0; c<columns; c++) {
grid[r][c] = 0;
}
}
}
public Tetris(Tetris tetris) {
x = tetris.getX();
y = tetris.getY();
scale = tetris.getScale();
rows = tetris.getRows();
columns = tetris.getColumns();
holdingbay = true;
grid = tetris.getGrid();
}
// DrawArray Method that draws a 2D array -> 10 across, 22 down
public void drawArray() {
// Draws the array into the console
for(int i=0; i<grid.length; i++) {
for(int x=0; x<grid[0].length; x++)
System.out.print(grid[i][x]);
System.out.println("");
}
}
// Draw Method that translates a grid into 2D graphics
public void draw(Graphics g) {
// In each row for each column...
for(int r=0; r<rows; r++) {
for(int c=0; c<columns; c++) {
// In this model of tetris, current player-controlled tetromino blocks have a unique index of 10
// and will return to a static index of 1, 2, 3, 4, 5, 6, 7 once placed
// This is to differentiate tetromino-controlled blocks from already-placed blocks
// in terms of differentiating interactions and so forth
// 0 Represents background blocks
if(r>3 && !holdingbay) {
if(grid[r][c] == 0) {
g.setColor(background);
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
}
if(holdingbay) {
if(c>2 && c<7) {
if(grid[r][c] == 0) {
g.setColor(background);
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
}
}
// 1 Represents T-Blocks
if(grid[r][c] == 1) {
g.setColor(Color.magenta.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.magenta);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 2 Represents Square Blocks
else if(grid[r][c] == 2) {
g.setColor(Color.orange.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.orange);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 3 Represents I blocks
else if(grid[r][c] == 3) {
g.setColor(Color.cyan.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.cyan);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 4 Represents L-Blocks
else if(grid[r][c] == 4) {
g.setColor(new Color(255, 120, 0).darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(new Color(255, 120, 0));
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 5 Represents Backwards L-Blocks
else if(grid[r][c] == 5) {
g.setColor(Color.blue.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.blue);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 6 Represents Z-Blocks
else if(grid[r][c] == 6) {
g.setColor(Color.red.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.red);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 7 Represents S-Blocks
else if(grid[r][c] == 7) {
g.setColor(Color.green.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.green);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 8 Represents Error Blocks
else if(grid[r][c] == 8) {
g.setColor(Color.WHITE);
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 9 Represent Block Indicators {
else if(grid[r][c] == 9) {
g.setColor(Color.WHITE.darker().darker().darker());
//g.setColor(Color.WHITE.darker().darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
}
}
if(!holdingbay) {
// Gridlines
g.setColor(outline);
for(int r=5; r<rows; r++)
g.drawLine(x, y+(scale*r), x+(scale*columns)-1, y+(scale*r));
for(int c=1; c<columns; c++)
g.drawLine(x+(scale*c), y+scale*4, x+(scale*c), y+(scale*rows)-1);
// Borders of the Tetris Grid
g.setColor(Color.WHITE);
g.drawLine(x, y+scale*4, x, y+scale*rows); // Left
g.drawLine(x+scale*columns, y+scale*4, x+scale*columns, y+scale*rows); // Right
g.drawLine(x, y+scale*rows, x+scale*columns, y+scale*rows); // Bottom
// Marks the threshold line that tetris blocks cannot exceed or game over
//g.drawLine(x, y+(4*scale), x+(scale*columns), y+(4*scale));
}
}
public void drawDynamicTetromino(Tetromino tetromino, Graphics g) {
// In each row for each column...
for(int r=0; r<rows; r++) {
for(int c=0; c<columns; c++) {
// 10 Represents tetromino-controlled blocks with the corresponding numbers above
if(grid[r][c] == 10) {
// 1 Represents T-Blocks
if(tetromino.getBlock() == 1) {
g.setColor(Color.magenta.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.magenta);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 2 Represents Square Blocks
else if(tetromino.getBlock() == 2) {
g.setColor(Color.orange.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.orange);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 3 Represents I blocks
else if(tetromino.getBlock() == 3) {
g.setColor(Color.cyan.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.cyan);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 4 Represents L-Blocks
else if(tetromino.getBlock() == 4) {
g.setColor(new Color(255, 120, 0).darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(new Color(255, 120, 0));
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 5 Represents Backwards L-Blocks
else if(tetromino.getBlock() == 5) {
g.setColor(Color.blue.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.blue);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 6 Represents Z-Blocks
else if(tetromino.getBlock() == 6) {
g.setColor(Color.red.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.red);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
// 7 Represents S-Blocks
else if(tetromino.getBlock() == 7) {
g.setColor(Color.green.darker());
g.fillRect(x+(scale*(c)), y+(scale*r), scale, scale);
g.setColor(Color.green);
g.fillRect(x+(scale*(c)+(scale/8)), y+(scale*r)+(scale/8), scale-((2*scale)/8), scale-((2*scale)/8));
g.setColor(outline);
g.drawRect(x+(scale*(c)), y+(scale*r), scale, scale);
}
}
}
}
}
// Returns a random int from 1-7 that represents a random Tetris block
public int randomBlock() {
return (int)(Math.random()*7+1);
}
// Returns a boolean that determines if the game is over or not
public boolean gameOver() {
for(int r=0; r<4; r++) {
for(int c=0; c<columns; c++) {
if(grid[r][c] != 0 && grid[r][c] != 10 && grid[r][c] != 9)
return true;
}
}
return false;
}
// Clears the whole board
public void clear() {
for(int r=0; r<rows; r++) {
for(int c=0; c<columns; c++) {
grid[r][c] = 0;
}
}
}
// Method to clear an entire row
public void clearRow(int row) {
for(int c=0; c<columns; c++) {
grid[row][c] = 0;
}
}
// Method to clear a row if it fulfills the requirement of being full
public void LineClear() {
// First resets rowsDeleted to update the counter
rowsDeleted = 0;
// Set a for-loop to check every row
for(int r=rows-1; r>=0; r--){
// States a boolean that: if the row does not have a 0 in any column, it means it is full
boolean isFull = true;
// For-loop that checks through all columns
for(int c=0; c<columns; c++) {
if(grid[r][c] == 0) {
isFull = false;
}
}
// In the event that the row is full...
if(isFull) {
rowDeleted.add(r); // Saves the index where the row was cleared from bottom to top
clearRow(r); // Clear the row
lineCleared = true; // Allows shifting to begin only once rows have been cleared.
rowsDeleted++; // Adds to the counter of how many rows you have cleared in one move. A Tetris is when you clear four lines with one move.
}
}
if(rowsDeleted == 1) {
score += 1000;
didTetris = false;
backToBackTetris = false;
}
else if(rowsDeleted == 2) {
score += 3000;
didTetris = false;
backToBackTetris = false;
}
else if(rowsDeleted == 3) {
score += 5000;
didTetris = false;
backToBackTetris = false;
}
else if(rowsDeleted == 4) {
score += 8000;
if(didTetris) {
backToBackTetris = true;
score +=4000;
}
didTetris = true;
}
// If lines are cleared... continue to the following
if(lineCleared) {
// For every row that is cleared... shift down once
for(int i=0; i<rowDeleted.size(); i++) {
linesCleared++;
shiftDownFrom(rowDeleted.get(i));
}
// Resets lineCleared boolean
lineCleared = false;
rowDeleted.clear(); // Resets rowDeleted arrayList
}
}
// Pre-req method to shift down the grid when given the row that has been cleared.
public void shiftDownFrom(int row) {
for(int r=row; r>0; r--) {
for(int c=0; c<columns; c++) {
grid[r][c] = grid[r-1][c];
}
}
for(int i=0; i<rowDeleted.size(); i++)
rowDeleted.set(i, rowDeleted.get(i)+1);
}
// Boolean for a full-clear
public boolean fullClear() {
boolean fullClear = true;
for(int r=0; r<rows; r++) {
for(int c=0; c<columns; c++) {
if(grid[r][c] != 0)
fullClear = false;
}
}
return fullClear;
}
// Print Method
public void printBoard() {
for(int r=0; r<rows; r++) {
System.out.println();
for(int c=0; c<columns; c++) {
if(grid[r][c] == 0) {
System.out.print("O ");
}
else {
System.out.print("1 ");
}
}
}
System.out.println();
}
// Methods for Genetic Algorithm
// getHoles
public int getHoles() {
int holes = 0;
for(int c=0; c<columns; c++) {
boolean firstBlock = false;
for(int r=0; r<rows; r++) {
if(grid[r][c] != 0 && grid[r][c] != 9)
firstBlock = true;
if((grid[r][c] == 0 || grid[r][c] == 9) && firstBlock)
holes++;
}
}
return holes;
}
// Total Score Cleared
public int getScore() {
return score;
}
// Bumpiness
public int getBumpiness() {
int bumpScore = 0;
int[] bumps = new int[10];
for(int c=0; c<columns; c++) {
int row = rows;
for(int r=0; r<rows; r++) {
if(grid[r][c] != 0 && grid[r][c] != 9) {
row = r;
break;
}
}
bumps[c] = rows-row;
}
for(int i=0; i<bumps.length-1; i++) {
bumpScore += Math.abs(bumps[i]-bumps[i+1]);
}
return bumpScore;
}
// Height of stack
public int getAggregateHeight() {
// int tallest = getHeightHelper(0);
// for(int c=1; c<columns; c++) {
// if(tallest < getHeightHelper(c)) {
// tallest = getHeightHelper(c);
// }
// }
// return tallest;
int aggregateHeight = 0;
for(int c=0; c<columns; c++) {
aggregateHeight += getHeightHelper(c);
}
return aggregateHeight;
}
// getHeight helper
public int getHeightHelper(int col) {
for(int r=0; r<rows; r++) {
if(grid[r][col] != 0 && grid[r][col] != 9)
return rows-r;
}
return 0;
}
public int didATetris() {
if(didTetris)
return 8000;
else
return 0;
}
public int didBackToBackTetris() {
if(didTetris && backToBackTetris)
return 4000;
else
return 0;
}
// Copy of Tetris
public int[][] getGridCopy() {
int[][] gridCopy = new int[rows][columns];
for(int r=0; r<rows; r++) {
for(int c=0; c<columns; c++) {
if(grid[r][c] == 10)
gridCopy[r][c] = 0;
else
gridCopy[r][c] = grid[r][c];
}
}
return gridCopy;
}
// GETTER METHODS
public int[][] getGrid() { // GetGrid method that returns the current grid of Tetris
return grid;
}
public void setGrid(int[][] grid) {
this.grid = grid;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getScale() {
return scale;
}
public int getRows() { // Returns # of rows
return rows;
}
public int getColumns() { // Returns # of rows
return columns;
}
public Color getBackground() { // Returns the color of the background
return background;
}
public int getRowsDeleted() {
return rowsDeleted;
}
public boolean didLineClear() {
return lineCleared;
}
public void lineClear(boolean bool) {
this.lineCleared = bool;
}
public void setScore(int score) {
this.score = score;
}
public int getColor(Tetromino tetromino) {
return tetromino.getBlock();
}
public boolean getDidBackToBackTetris() {
return backToBackTetris;
}
}