-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTetrisGame.java
More file actions
914 lines (765 loc) · 30.2 KB
/
TetrisGame.java
File metadata and controls
914 lines (765 loc) · 30.2 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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.JFrame;
/**
* @author Adarsh Varshney
* Reference: Brendan Jones
*/
public class TetrisGame extends JFrame {
private static final long serialVersionUID = 12345L;
private static final long FRAME_TIME = 1000L / 50L;
private static final int TYPE_COUNT = TileType.values().length;
private BoardPanel board;
private SidePanel side;
private boolean isPaused;
private boolean isNewGame;
private boolean isGameOver;
private int level;
private int score;
private Random random;
private Clock logicTimer;
private TileType currentType;
private TileType nextType;
private int currentCol;
private int currentRow;
private int currentRotation;
private int dropCooldown;
private float gameSpeed;
private TetrisGame() {
super("Adarsh's Tetris");
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
this.board = new BoardPanel(this);
this.side = new SidePanel(this);
add(board, BorderLayout.CENTER);
add(side, BorderLayout.EAST);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_S:
if(!isPaused && dropCooldown == 0) {
logicTimer.setCyclesPerSecond(25.0f);
}
break;
case KeyEvent.VK_A:
if(!isPaused && board.isValidAndEmpty(currentType, currentCol - 1, currentRow, currentRotation)) {
currentCol--;
}
break;
case KeyEvent.VK_D:
if(!isPaused && board.isValidAndEmpty(currentType, currentCol + 1, currentRow, currentRotation)) {
currentCol++;
}
break;
case KeyEvent.VK_Q:
if(!isPaused) {
rotatePiece((currentRotation == 0) ? 3 : currentRotation - 1);
}
break;
case KeyEvent.VK_E:
if(!isPaused) {
rotatePiece((currentRotation == 3) ? 0 : currentRotation + 1);
}
break;
case KeyEvent.VK_P:
if(!isGameOver && !isNewGame) {
isPaused = !isPaused;
logicTimer.setPaused(isPaused);
}
break;
case KeyEvent.VK_ENTER:
if(isGameOver || isNewGame) {
resetGame();
}
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_S:
logicTimer.setCyclesPerSecond(gameSpeed);
logicTimer.reset();
break;
}
}
});
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void startGame() {
this.random = new Random();
this.isNewGame = true;
this.gameSpeed = 1.0f;
this.logicTimer = new Clock(gameSpeed);
logicTimer.setPaused(true);
while(true) {
long start = System.nanoTime();
logicTimer.update();
if(logicTimer.hasElapsedCycle()) {
updateGame();
}
if(dropCooldown > 0) {
dropCooldown--;
}
renderGame();
long delta = (System.nanoTime() - start) / 1000000L;
if(delta < FRAME_TIME) {
try {
Thread.sleep(FRAME_TIME - delta);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
private void updateGame() {
if(board.isValidAndEmpty(currentType, currentCol, currentRow + 1, currentRotation)) {
currentRow++;
} else {
board.addPiece(currentType, currentCol, currentRow, currentRotation);
int cleared = board.checkLines();
if(cleared > 0) {
score += 50 << cleared;
}
gameSpeed += 0.035f;
logicTimer.setCyclesPerSecond(gameSpeed);
logicTimer.reset();
dropCooldown = 25;
level = (int)(gameSpeed * 1.70f);
spawnPiece();
}
}
private void renderGame() {
board.repaint();
side.repaint();
}
private void resetGame() {
this.level = 1;
this.score = 0;
this.gameSpeed = 1.0f;
this.nextType = TileType.values()[random.nextInt(TYPE_COUNT)];
this.isNewGame = false;
this.isGameOver = false;
board.clear();
logicTimer.reset();
logicTimer.setCyclesPerSecond(gameSpeed);
spawnPiece();
}
private void spawnPiece() {
this.currentType = nextType;
this.currentCol = currentType.getSpawnColumn();
this.currentRow = currentType.getSpawnRow();
this.currentRotation = 0;
this.nextType = TileType.values()[random.nextInt(TYPE_COUNT)];
if(!board.isValidAndEmpty(currentType, currentCol, currentRow, currentRotation)) {
this.isGameOver = true;
logicTimer.setPaused(true);
}
}
private void rotatePiece(int newRotation) {
int newColumn = currentCol;
int newRow = currentRow;
int left = currentType.getLeftInset(newRotation);
int right = currentType.getRightInset(newRotation);
int top = currentType.getTopInset(newRotation);
int bottom = currentType.getBottomInset(newRotation);
if(currentCol < -left) {
newColumn -= currentCol - left;
} else if(currentCol + currentType.getDimension() - right >= BoardPanel.COL_COUNT) {
newColumn -= (currentCol + currentType.getDimension() - right) - BoardPanel.COL_COUNT + 1;
}
if(currentRow < -top) {
newRow -= currentRow - top;
} else if(currentRow + currentType.getDimension() - bottom >= BoardPanel.ROW_COUNT) {
newRow -= (currentRow + currentType.getDimension() - bottom) - BoardPanel.ROW_COUNT + 1;
}
if(board.isValidAndEmpty(currentType, newColumn, newRow, newRotation)) {
currentRotation = newRotation;
currentRow = newRow;
currentCol = newColumn;
}
}
public boolean isPaused() {
return isPaused;
}
public boolean isGameOver() {
return isGameOver;
}
public boolean isNewGame() {
return isNewGame;
}
public int getScore() {
return score;
}
public int getLevel() {
return level;
}
public TileType getPieceType() {
return currentType;
}
public TileType getNextPieceType() {
return nextType;
}
public int getPieceCol() {
return currentCol;
}
public int getPieceRow() {
return currentRow;
}
public int getPieceRotation() {
return currentRotation;
}
public static void main(String[] args) {
TetrisGame TetrisGame = new TetrisGame();
TetrisGame.startGame();
}
public class BoardPanel extends JPanel {
private static final long serialVersionUID = 12345L;
public static final int COLOR_MIN = 35;
public static final int COLOR_MAX = 255 - COLOR_MIN;
private static final int BORDER_WIDTH = 5;
public static final int COL_COUNT = 10;
private static final int VISIBLE_ROW_COUNT = 20;
private static final int HIDDEN_ROW_COUNT = 2;
public static final int ROW_COUNT = VISIBLE_ROW_COUNT + HIDDEN_ROW_COUNT;
public static final int TILE_SIZE = 24;
public static final int SHADE_WIDTH = 4;
private static final int CENTER_X = COL_COUNT * TILE_SIZE / 2;
private static final int CENTER_Y = VISIBLE_ROW_COUNT * TILE_SIZE / 2;
public static final int PANEL_WIDTH = COL_COUNT * TILE_SIZE + BORDER_WIDTH * 2;
public static final int PANEL_HEIGHT = VISIBLE_ROW_COUNT * TILE_SIZE + BORDER_WIDTH * 2;
private static final Font LARGE_FONT = new Font("Tahoma", Font.BOLD, 16);
private static final Font SMALL_FONT = new Font("Tahoma", Font.BOLD, 12);
private TetrisGame TetrisGame;
private TileType[][] tiles;
public BoardPanel(TetrisGame TetrisGame) {
this.TetrisGame = TetrisGame;
this.tiles = new TileType[ROW_COUNT][COL_COUNT];
setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
setBackground(Color.BLACK);
}
public void clear() {
for(int i = 0; i < ROW_COUNT; i++) {
for(int j = 0; j < COL_COUNT; j++) {
tiles[i][j] = null;
}
}
}
public boolean isValidAndEmpty(TileType type, int x, int y, int rotation) {
if(x < -type.getLeftInset(rotation) || x + type.getDimension() - type.getRightInset(rotation) >= COL_COUNT) {
return false;
}
if(y < -type.getTopInset(rotation) || y + type.getDimension() - type.getBottomInset(rotation) >= ROW_COUNT) {
return false;
}
for(int col = 0; col < type.getDimension(); col++) {
for(int row = 0; row < type.getDimension(); row++) {
if(type.isTile(col, row, rotation) && isOccupied(x + col, y + row)) {
return false;
}
}
}
return true;
}
public void addPiece(TileType type, int x, int y, int rotation) {
for(int col = 0; col < type.getDimension(); col++) {
for(int row = 0; row < type.getDimension(); row++) {
if(type.isTile(col, row, rotation)) {
setTile(col + x, row + y, type);
}
}
}
}
public int checkLines() {
int completedLines = 0;
for(int row = 0; row < ROW_COUNT; row++) {
if(checkLine(row)) {
completedLines++;
}
}
return completedLines;
}
private boolean checkLine(int line) {
for(int col = 0; col < COL_COUNT; col++) {
if(!isOccupied(col, line)) {
return false;
}
}
for(int row = line - 1; row >= 0; row--) {
for(int col = 0; col < COL_COUNT; col++) {
setTile(col, row + 1, getTile(col, row));
}
}
return true;
}
private boolean isOccupied(int x, int y) {
return tiles[y][x] != null;
}
private void setTile(int x, int y, TileType type) {
tiles[y][x] = type;
}
private TileType getTile(int x, int y) {
return tiles[y][x];
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.translate(BORDER_WIDTH, BORDER_WIDTH);
if(TetrisGame.isPaused()) {
g.setFont(LARGE_FONT);
g.setColor(Color.WHITE);
String msg = "PAUSED";
g.drawString(msg, CENTER_X - g.getFontMetrics().stringWidth(msg) / 2, CENTER_Y);
} else if(TetrisGame.isNewGame() || TetrisGame.isGameOver()) {
g.setFont(LARGE_FONT);
g.setColor(Color.WHITE);
String msg = TetrisGame.isNewGame() ? "Tetris" : "GAME OVER";
g.drawString(msg, CENTER_X - g.getFontMetrics().stringWidth(msg) / 2, 150);
g.setFont(SMALL_FONT);
msg = "Press Enter to Play" + (TetrisGame.isNewGame() ? "" : " Again");
g.drawString(msg, CENTER_X - g.getFontMetrics().stringWidth(msg) / 2, 300);
} else {
for(int x = 0; x < COL_COUNT; x++) {
for(int y = HIDDEN_ROW_COUNT; y < ROW_COUNT; y++) {
TileType tile = getTile(x, y);
if(tile != null) {
drawTile(tile, x * TILE_SIZE, (y - HIDDEN_ROW_COUNT) * TILE_SIZE, g);
}
}
}
TileType type = TetrisGame.getPieceType();
int pieceCol = TetrisGame.getPieceCol();
int pieceRow = TetrisGame.getPieceRow();
int rotation = TetrisGame.getPieceRotation();
for(int col = 0; col < type.getDimension(); col++) {
for(int row = 0; row < type.getDimension(); row++) {
if(pieceRow + row >= 2 && type.isTile(col, row, rotation)) {
drawTile(type, (pieceCol + col) * TILE_SIZE, (pieceRow + row - HIDDEN_ROW_COUNT) * TILE_SIZE, g);
}
}
}
Color base = type.getBaseColor();
base = new Color(base.getRed(), base.getGreen(), base.getBlue(), 20);
for(int lowest = pieceRow; lowest < ROW_COUNT; lowest++) {
if(isValidAndEmpty(type, pieceCol, lowest, rotation)) {
continue;
}
lowest--;
for(int col = 0; col < type.getDimension(); col++) {
for(int row = 0; row < type.getDimension(); row++) {
if(lowest + row >= 2 && type.isTile(col, row, rotation)) {
drawTile(base, base.brighter(), base.darker(), (pieceCol + col) * TILE_SIZE, (lowest + row - HIDDEN_ROW_COUNT) * TILE_SIZE, g);
}
}
}
break;
}
g.setColor(Color.DARK_GRAY);
for(int x = 0; x < COL_COUNT; x++) {
for(int y = 0; y < VISIBLE_ROW_COUNT; y++) {
g.drawLine(0, y * TILE_SIZE, COL_COUNT * TILE_SIZE, y * TILE_SIZE);
g.drawLine(x * TILE_SIZE, 0, x * TILE_SIZE, VISIBLE_ROW_COUNT * TILE_SIZE);
}
}
}
g.setColor(Color.WHITE);
g.drawRect(0, 0, TILE_SIZE * COL_COUNT, TILE_SIZE * VISIBLE_ROW_COUNT);
}
private void drawTile(TileType type, int x, int y, Graphics g) {
drawTile(type.getBaseColor(), type.getLightColor(), type.getDarkColor(), x, y, g);
}
private void drawTile(Color base, Color light, Color dark, int x, int y, Graphics g) {
g.setColor(base);
g.fillRect(x, y, TILE_SIZE, TILE_SIZE);
g.setColor(dark);
g.fillRect(x, y + TILE_SIZE - SHADE_WIDTH, TILE_SIZE, SHADE_WIDTH);
g.fillRect(x + TILE_SIZE - SHADE_WIDTH, y, SHADE_WIDTH, TILE_SIZE);
g.setColor(light);
for(int i = 0; i < SHADE_WIDTH; i++) {
g.drawLine(x, y + i, x + TILE_SIZE - i - 1, y + i);
g.drawLine(x + i, y, x + i, y + TILE_SIZE - i - 1);
}
}
}
public class Clock {
private float millisPerCycle;
private long lastUpdate;
private int elapsedCycles;
private float excessCycles;
private boolean isPaused;
public Clock(float cyclesPerSecond) {
setCyclesPerSecond(cyclesPerSecond);
reset();
}
public void setCyclesPerSecond(float cyclesPerSecond) {
this.millisPerCycle = (1.0f / cyclesPerSecond) * 1000;
}
public void reset() {
this.elapsedCycles = 0;
this.excessCycles = 0.0f;
this.lastUpdate = getCurrentTime();
this.isPaused = false;
}
public void update() {
long currUpdate = getCurrentTime();
float delta = (float)(currUpdate - lastUpdate) + excessCycles;
if(!isPaused) {
this.elapsedCycles += (int)Math.floor(delta / millisPerCycle);
this.excessCycles = delta % millisPerCycle;
}
this.lastUpdate = currUpdate;
}
public void setPaused(boolean paused) {
this.isPaused = paused;
}
public boolean isPaused() {
return isPaused;
}
public boolean hasElapsedCycle() {
if(elapsedCycles > 0) {
this.elapsedCycles--;
return true;
}
return false;
}
public boolean peekElapsedCycle() {
return (elapsedCycles > 0);
}
private static final long getCurrentTime() {
return (System.nanoTime() / 1000000L);
}
}
public class SidePanel extends JPanel {
private static final long serialVersionUID = 2181495598854992747L;
private static final int TILE_SIZE = BoardPanel.TILE_SIZE >> 1;
private static final int SHADE_WIDTH = BoardPanel.SHADE_WIDTH >> 1;
private static final int TILE_COUNT = 5;
private static final int SQUARE_CENTER_X = 130;
private static final int SQUARE_CENTER_Y = 65;
private static final int SQUARE_SIZE = (TILE_SIZE * TILE_COUNT >> 1);
private static final int SMALL_INSET = 20;
private static final int LARGE_INSET = 40;
private static final int STATS_INSET = 125;
private static final int CONTROLS_INSET = 210;
private static final int TEXT_STRIDE = 25;
private static final Font SMALL_FONT = new Font("Tahoma", Font.BOLD, 11);
private static final Font LARGE_FONT = new Font("Tahoma", Font.BOLD, 13);
private static final Color DRAW_COLOR = new Color(128, 192, 128);
private TetrisGame TetrisGame;
public SidePanel(TetrisGame TetrisGame) {
this.TetrisGame = TetrisGame;
setPreferredSize(new Dimension(200, BoardPanel.PANEL_HEIGHT));
setBackground(Color.BLACK);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(DRAW_COLOR);
int offset;
g.setFont(LARGE_FONT);
g.drawString("Stats", SMALL_INSET, offset = STATS_INSET);
g.setFont(SMALL_FONT);
g.drawString("Level: " + TetrisGame.getLevel(), LARGE_INSET, offset += TEXT_STRIDE);
g.drawString("Score: " + TetrisGame.getScore(), LARGE_INSET, offset += TEXT_STRIDE);
g.setFont(LARGE_FONT);
g.drawString("Controls", SMALL_INSET, offset = CONTROLS_INSET);
g.setFont(SMALL_FONT);
g.drawString("A - Move Left", LARGE_INSET, offset += TEXT_STRIDE);
g.drawString("D - Move Right", LARGE_INSET, offset += TEXT_STRIDE);
g.drawString("Q - Rotate Anticlockwise", LARGE_INSET, offset += TEXT_STRIDE);
g.drawString("E - Rotate Clockwise", LARGE_INSET, offset += TEXT_STRIDE);
g.drawString("S - Drop", LARGE_INSET, offset += TEXT_STRIDE);
g.drawString("P - Pause Game", LARGE_INSET, offset += TEXT_STRIDE);
g.drawString("Designed By", getWidth() / 2 - g.getFontMetrics().stringWidth("Designed By") / 2,
offset += 2*TEXT_STRIDE);
g.setFont(LARGE_FONT);
g.drawString("Adarsh Varshney", getWidth() / 2 - g.getFontMetrics().stringWidth("Adarsh Varshney") / 2, offset += TEXT_STRIDE);
g.setFont(LARGE_FONT);
g.drawString("Next Piece:", SMALL_INSET, 70);
g.drawRect(SQUARE_CENTER_X - SQUARE_SIZE, SQUARE_CENTER_Y - SQUARE_SIZE, SQUARE_SIZE * 2, SQUARE_SIZE * 2);
TileType type = TetrisGame.getNextPieceType();
if(!TetrisGame.isGameOver() && type != null) {
int cols = type.getCols();
int rows = type.getRows();
int dimension = type.getDimension();
int startX = (SQUARE_CENTER_X - (cols * TILE_SIZE / 2));
int startY = (SQUARE_CENTER_Y - (rows * TILE_SIZE / 2));
int top = type.getTopInset(0);
int left = type.getLeftInset(0);
for(int row = 0; row < dimension; row++) {
for(int col = 0; col < dimension; col++) {
if(type.isTile(col, row, 0)) {
drawTile(type, startX + ((col - left) * TILE_SIZE), startY + ((row - top) * TILE_SIZE), g);
}
}
}
}
}
private void drawTile(TileType type, int x, int y, Graphics g) {
g.setColor(type.getBaseColor());
g.fillRect(x, y, TILE_SIZE, TILE_SIZE);
g.setColor(type.getDarkColor());
g.fillRect(x, y + TILE_SIZE - SHADE_WIDTH, TILE_SIZE, SHADE_WIDTH);
g.fillRect(x + TILE_SIZE - SHADE_WIDTH, y, SHADE_WIDTH, TILE_SIZE);
g.setColor(type.getLightColor());
for(int i = 0; i < SHADE_WIDTH; i++) {
g.drawLine(x, y + i, x + TILE_SIZE - i - 1, y + i);
g.drawLine(x + i, y, x + i, y + TILE_SIZE - i - 1);
}
}
}
public enum TileType {
TypeI(new Color(BoardPanel.COLOR_MIN, BoardPanel.COLOR_MAX, BoardPanel.COLOR_MAX), 4, 4, 1, new boolean[][] {
{
false, false, false, false,
true, true, true, true,
false, false, false, false,
false, false, false, false,
},
{
false, false, true, false,
false, false, true, false,
false, false, true, false,
false, false, true, false,
},
{
false, false, false, false,
false, false, false, false,
true, true, true, true,
false, false, false, false,
},
{
false, true, false, false,
false, true, false, false,
false, true, false, false,
false, true, false, false,
}
}),
TypeJ(new Color(BoardPanel.COLOR_MIN, BoardPanel.COLOR_MIN, BoardPanel.COLOR_MAX), 3, 3, 2, new boolean[][] {
{
true, false, false,
true, true, true,
false, false, false,
},
{
false, true, true,
false, true, false,
false, true, false,
},
{
false, false, false,
true, true, true,
false, false, true,
},
{
false, true, false,
false, true, false,
true, true, false,
}
}),
TypeL(new Color(BoardPanel.COLOR_MAX, 127, BoardPanel.COLOR_MIN), 3, 3, 2, new boolean[][] {
{
false, false, true,
true, true, true,
false, false, false,
},
{
false, true, false,
false, true, false,
false, true, true,
},
{
false, false, false,
true, true, true,
true, false, false,
},
{
true, true, false,
false, true, false,
false, true, false,
}
}),
TypeO(new Color(BoardPanel.COLOR_MAX, BoardPanel.COLOR_MAX, BoardPanel.COLOR_MIN), 2, 2, 2, new boolean[][] {
{
true, true,
true, true,
},
{
true, true,
true, true,
},
{
true, true,
true, true,
},
{
true, true,
true, true,
}
}),
TypeS(new Color(BoardPanel.COLOR_MIN, BoardPanel.COLOR_MAX, BoardPanel.COLOR_MIN), 3, 3, 2, new boolean[][] {
{
false, true, true,
true, true, false,
false, false, false,
},
{
false, true, false,
false, true, true,
false, false, true,
},
{
false, false, false,
false, true, true,
true, true, false,
},
{
true, false, false,
true, true, false,
false, true, false,
}
}),
TypeT(new Color(128, BoardPanel.COLOR_MIN, 128), 3, 3, 2, new boolean[][] {
{
false, true, false,
true, true, true,
false, false, false,
},
{
false, true, false,
false, true, true,
false, true, false,
},
{
false, false, false,
true, true, true,
false, true, false,
},
{
false, true, false,
true, true, false,
false, true, false,
}
}),
TypeZ(new Color(BoardPanel.COLOR_MAX, BoardPanel.COLOR_MIN, BoardPanel.COLOR_MIN), 3, 3, 2, new boolean[][] {
{
true, true, false,
false, true, true,
false, false, false,
},
{
false, false, true,
false, true, true,
false, true, false,
},
{
false, false, false,
true, true, false,
false, true, true,
},
{
false, true, false,
true, true, false,
true, false, false,
}
});
private Color baseColor;
private Color lightColor;
private Color darkColor;
private int spawnCol;
private int spawnRow;
private int dimension;
private int rows;
private int cols;
private boolean[][] tiles;
private TileType(Color color, int dimension, int cols, int rows, boolean[][] tiles) {
this.baseColor = color;
this.lightColor = color.brighter();
this.darkColor = color.darker();
this.dimension = dimension;
this.tiles = tiles;
this.cols = cols;
this.rows = rows;
this.spawnCol = 5 - (dimension >> 1);
this.spawnRow = getTopInset(0);
}
public Color getBaseColor() {
return baseColor;
}
public Color getLightColor() {
return lightColor;
}
public Color getDarkColor() {
return darkColor;
}
public int getDimension() {
return dimension;
}
public int getSpawnColumn() {
return spawnCol;
}
public int getSpawnRow() {
return spawnRow;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public boolean isTile(int x, int y, int rotation) {
return tiles[rotation][y * dimension + x];
}
public int getLeftInset(int rotation) {
for(int x = 0; x < dimension; x++) {
for(int y = 0; y < dimension; y++) {
if(isTile(x, y, rotation)) {
return x;
}
}
}
return -1;
}
public int getRightInset(int rotation) {
for(int x = dimension - 1; x >= 0; x--) {
for(int y = 0; y < dimension; y++) {
if(isTile(x, y, rotation)) {
return dimension - x;
}
}
}
return -1;
}
public int getTopInset(int rotation) {
for(int y = 0; y < dimension; y++) {
for(int x = 0; x < dimension; x++) {
if(isTile(x, y, rotation)) {
return y;
}
}
}
return -1;
}
public int getBottomInset(int rotation) {
for(int y = dimension - 1; y >= 0; y--) {
for(int x = 0; x < dimension; x++) {
if(isTile(x, y, rotation)) {
return dimension - y;
}
}
}
return -1;
}
}
}