-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleTetris.java
811 lines (711 loc) · 25.8 KB
/
ConsoleTetris.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
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
/**
* @author Crckr Hckr
*
* The main .java file for the Console Tetris project that simulates retro graphics
* through the use of a console video for graphical output.
*
*/
import java.util.Random; //allows for use of random numbers
import java.util.logging.*; //to disable debug input from jnativehook - see constructor
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class ConsoleTetris implements NativeKeyListener{
static boolean leftKeyPressed = false;
static boolean rightKeyPressed = false;
static boolean downKeyPressed = false;
static boolean spaceKeyPressed = false;
public ConsoleTetris() {
// Clear previous logging configurations.
LogManager.getLogManager().reset();
// Get the logger for "org.jnativehook" and set the level to off.
Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
// Disable the parent handlers.
logger.setUseParentHandlers(false);
}
public static void main(String[] args) {
try {
GlobalScreen.registerNativeHook();
}
catch (NativeHookException ex){
System.err.println("There was a problem registering the native hook. ");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.addNativeKeyListener(new ConsoleTetris());
Random rand = new Random(); //used to generate random tetrominos
char[][] gameBoard = new char[22][10]; //standard tetris grid is 22 rows by 10 columns
boolean gameOver = false; //condition for game loop to continue running
boolean spawnTetromino = true; //if true, we need to spawn a random tetromino on to gameBoard
initializegameBoard(gameBoard); //sets multi-dimension array making up gameBoard to all zeroes
printTitleScreen();
//Thread.sleep allows intro screen to stick around for a while
//requires try/catch block
try {
Thread.sleep(2500);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
//main game loop
while(!gameOver) {
if(spawnTetromino) {
clearLines(gameBoard); //clear full lines if any
spawnRandomTetromino(gameBoard, rand); //spawns a random tetromino on to gameBoard
spawnTetromino = false; //tetromino has been spawned at this point
}
if(leftKeyPressed) {
moveTetrominoLeft(gameBoard);
leftKeyPressed = false;
}
else if(rightKeyPressed) {
moveTetrominoRight(gameBoard);
rightKeyPressed = false;
}
else if(spaceKeyPressed) {
rotateTetromino(gameBoard);
spaceKeyPressed = false;
}
else if(downKeyPressed) {
downKeyPressed = false;
}
else
spawnTetromino = dropTetromino(gameBoard); //drops the tetromino down one row at a time
//if it can't be dropped any further, true is returned
//in which case another tetromino has to be spawned
//gameBoard is printed every 100ms
try {
Thread.sleep(100); //200
printGameBoard(gameBoard);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
public static void printTitleScreen() {
System.out.println(" _____ _____ _ _ _____ _____ _ _____");
System.out.println("/ __ \\ _ | \\ | |/ ___|| _ | | | ___|");
System.out.println("| / \\/ | | | \\| |\\ `--. | | | | | | |__ ");
System.out.println("| | | | | | . ` | `--. \\| | | | | | __|");
System.out.println("| \\__/\\ \\_/ / |\\ |/\\__/ /\\ \\_/ / |____| |___");
System.out.println(" \\____/\\___/\\_| \\_/\\____/ \\___/\\_____/\\____/");
System.out.println("\n\n");
System.out.println(" _____ _____ ___________ _____ _____");
System.out.println("|_ _| ___|_ _| ___ \\_ _/ ___|");
System.out.println(" | | | |__ | | | |_/ / | | \\ `--.");
System.out.println(" | | | __| | | | / | | `--. \\");
System.out.println(" | | | |___ | | | |\\ \\ _| |_/\\__/ /");
System.out.println(" \\_/ \\____/ \\_/ \\_| \\_|\\___/\\____/");
System.out.println("");
}
public static void initializegameBoard(char[][] gameBoard) {
for(int i = 0; i < gameBoard.length; i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
gameBoard[i][j] = 0;
}
}
}
public static void printGameBoard(char[][] gameBoard) throws Exception {
for(int i = 0; i < 20; i++)
System.out.println();
for(int i = 2; i < gameBoard.length; i++) {
System.out.print("#");
for(int j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 0)
System.out.print(" ");
else if(gameBoard[i][j] == 3)
System.out.print("-");
else
System.out.print("*");
}
System.out.print("#\n");
}
for(int i = 0; i < gameBoard[0].length+2; i++) //2 accounts for first and last "#" in gird
System.out.print("#");
System.out.println("\n");
}
public static void spawnRandomTetromino(char[][] gameBoard, Random rand) {
int tetromino = rand.nextInt(8) + 1; //generate random no. between 1 and 7
switch (tetromino) {
case 1: spawnTetrominoI(gameBoard);
break;
case 2: spawnTetrominoO(gameBoard);
break;
case 3: spawnTetrominoT(gameBoard);
break;
case 4: spawnTetrominoS(gameBoard);
break;
case 5: spawnTetrominoZ(gameBoard);
break;
case 6: spawnTetrominoJ(gameBoard);
break;
case 7: spawnTetrominoL(gameBoard);
break;
case 8: spawnVerticalI(gameBoard);
}
}
public static void spawnVerticalI(char[][] gameBoard) {
gameBoard[0][4] = 1;
gameBoard[1][4] = 1;
gameBoard[2][4] = 1;
gameBoard[3][4] = 1;
}
public static void spawnTetrominoI(char[][] gameBoard){
for (int j = 0 + 3; j < gameBoard[0].length - 3; j++)
gameBoard[0][j] = 1; //****
}
public static void spawnTetrominoO(char[][] gameBoard){
gameBoard[0][4] = 1; gameBoard[0][5] = 1; //**
gameBoard[1][4] = 1; gameBoard[1][5] = 1; //**
}
public static void spawnTetrominoT(char[][] gameBoard){
gameBoard[0][3] = 1; gameBoard[0][4] = 1; gameBoard[0][5] = 1; //***
gameBoard[1][4] = 1; // *
}
public static void spawnTetrominoS(char[][] gameBoard){
gameBoard[0][4] = 1; gameBoard[0][5] = 1; //**
gameBoard[1][3] = 1; gameBoard[1][4] = 1; //**
}
public static void spawnTetrominoZ(char[][] gameBoard){
gameBoard[0][3] = 1; gameBoard[0][4] = 1; //**
gameBoard[1][4] = 1; gameBoard[1][5] = 1; //**
}
public static void spawnTetrominoJ(char[][] gameBoard){
gameBoard[0][5] = 1; // *
gameBoard[1][3] = 1; gameBoard[1][4] = 1; gameBoard[1][5] = 1; //***
}
public static void spawnTetrominoL(char[][] gameBoard){
gameBoard[0][3] = 1; //*
gameBoard[1][3] = 1; gameBoard[1][4] = 1; gameBoard[1][5] = 1; //***
}
public static boolean dropTetromino(char[][] gameBoard){ //returns boolean value indicating if when we have to spawn next tetromino
int tetrominoRow = -1; //holds row containing current tetromino
boolean tetrominoFound = false; //used to find which row contains current tetromino
boolean collision = false; //used to determine if drop possible
boolean specialCaseOne = true; //it's a special case if tetromino is a horizontal 'I' shaped one
boolean specialCaseTwo = false; //it's also a special case if tetromino is a vertical 'I' shaped one
//find tetromino location in board
for(int i = 0; i < gameBoard.length && !tetrominoFound; i++) { //sweep through all rows until tetromino found
for(int j = 0; j < gameBoard[i].length; j++) { //sweep through column to determine if row contains tetromino
//we found our row
if(gameBoard[i][j] == 1) {
tetrominoFound = true;
tetrominoRow = i;
}
}
}
//we assume that specialCaseOne applies and so we must test to determine if it 'doesn't' apply
if(tetrominoFound && tetrominoRow+1 != gameBoard.length) {
//the special case is basically when we just need to check the next row for whether the tetromino can be dropped
for(int j = 0; j < gameBoard[tetrominoRow].length; j++) {
if(gameBoard[tetrominoRow+1][j] == 1) {
specialCaseOne = false; //tetromino spans two rows and therefore specialCaseOne doesn't apply
}
}
}
//on the contrary, we need to determine if specialCaseTwo applies since we assume it doesn't apply initially
if(tetrominoFound && tetrominoRow+3 < gameBoard.length) {
for(int j = 0; j < gameBoard[tetrominoRow].length; j++) {
if(gameBoard[tetrominoRow][j] == 1 && gameBoard[tetrominoRow+1][j] == 1 && gameBoard[tetrominoRow+2][j] == 1 && gameBoard[tetrominoRow+3][j] == 1) {
specialCaseTwo = true; //tetromino spans four rows and therefore specialCaseTwo applies
}
}
}
//check if tetromino can be dropped
if(specialCaseOne) { //horizontal 'I'
if(tetrominoRow + 1 == gameBoard.length)
collision = true;
else {
for(int i = tetrominoRow; i < Math.min(tetrominoRow+1, gameBoard.length-1); i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 1 && gameBoard[i+1][j] == 2)
collision = true;
}
}
}
}
else if(specialCaseTwo) { //vertical 'I'
if(tetrominoRow + 4 >= gameBoard.length)
collision = true;
else {
for(int i = tetrominoRow; i < Math.min(tetrominoRow+1, gameBoard.length-1); i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 1 && (gameBoard[i+1][j] == 2 || gameBoard[i+2][j] == 2 || gameBoard[i+3][j] == 2 || gameBoard[i+4][j] == 2))
collision = true;
}
}
}
}
else { //every other tetromino
if(tetrominoRow + 1 >= gameBoard.length || tetrominoRow + 2 >= gameBoard.length) //all other tetrominoes occupy at least two rows
collision = true;
else {
for(int i = tetrominoRow; i < Math.min(tetrominoRow+1, gameBoard.length-1); i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
if( gameBoard[i+1][j] == 1 && gameBoard[i+2][j] == 2)
collision = true;
}
}
}
}
//drop the tetromino if feasible
if(!collision) {
if(specialCaseOne) {
for(int i = tetrominoRow, j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 1) {
gameBoard[i+1][j] = 1; //move part of tetromino downwards
gameBoard[i][j] = 0; //clear part from old position
}
}
}
else if(specialCaseTwo) {
for(int i = tetrominoRow, j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 1) {
gameBoard[i+4][j] = 1; //move part of tetromino downwards
gameBoard[i][j] = 0; //clear part from old position
}
}
}
else {
for(int i = tetrominoRow, j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i+1][j] == 1) {
gameBoard[i+2][j] = 1; //move part of tetromino downwards
gameBoard[i+1][j] = 0; //clear part from old position
}
}
for(int i = tetrominoRow, j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 1) {
gameBoard[i+1][j] = 1; //move part of tetromino downwards
gameBoard[i][j] = 0; //clear part from old position
}
}
}
}
//in the event a collision occurs
if(collision) {
for(int i = tetrominoRow; i < gameBoard.length; i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 1)
gameBoard[i][j] = 2;
}
}
}
return collision; //true means new tetromino needs to be spawned, not the case otherwise
}
public static void moveTetrominoLeft(char[][] gameBoard) {
int tetrominoRow = -1; //holds the row of the tetromino
boolean tetrominoFound = false; //used to determine if row contains a tetromino
boolean collision = false; //used to determine if shift to left possible
boolean specialCaseOne = true; //it's a special case if tetromino is a horizontal 'I' shaped one
boolean specialCaseTwo = false; //it's also a special case if tetromino is a vertical 'I' shaped one
//find tetromino location in board
for(int i = 0; i < gameBoard.length && !tetrominoFound; i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 1) {
tetrominoFound = true;
tetrominoRow = i;
}
}
}
//we assume that specialCaseOne applies and so we must test to determine if it 'doesn't' apply
if(tetrominoFound && tetrominoRow+1 != gameBoard.length) {
//the special case is basically when we just need to check the next row for whether the tetromino can be dropped
for(int j = 0; j < gameBoard[tetrominoRow].length; j++) {
if(gameBoard[tetrominoRow+1][j] == 1) {
specialCaseOne = false; //tetromino spans two rows and therefore specialCaseOne doesn't apply
}
}
}
//on the contrary, we need to determine if specialCaseTwo applies since we assume it doesn't apply initially
if(tetrominoFound && tetrominoRow+3 < gameBoard.length) {
for(int j = 0; j < gameBoard[tetrominoRow].length; j++) {
if(gameBoard[tetrominoRow][j] == 1 && gameBoard[tetrominoRow+1][j] == 1 && gameBoard[tetrominoRow+2][j] == 1 && gameBoard[tetrominoRow+3][j] == 1) {
specialCaseTwo = true; //tetromino spans four rows and therefore specialCaseTwo applies
}
}
}
//check if tetromino can be moved to the left
if(specialCaseOne) { //horizontal 'I'
for(int i = 0; i < gameBoard[tetrominoRow].length && !collision; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
if( i == 0 || gameBoard[tetrominoRow][i-1] == 2 ) {
collision = true;
}
i = gameBoard[tetrominoRow].length;
}
}
}
else if(specialCaseTwo) { //vertical 'I'
for(int i = 0; i < gameBoard[tetrominoRow].length && !collision; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
if( i == 0 ||
gameBoard[tetrominoRow][i-1] != 0 ||
gameBoard[tetrominoRow+1][i-1] != 0 ||
gameBoard[tetrominoRow+2][i-1] != 0 ||
gameBoard[tetrominoRow+3][i-1] != 0) {
collision = true;
}
i = gameBoard[tetrominoRow].length;
}
}
}
else { //every other tetromino (spans over 2(min) to 3(max) rows)
for(int i = 0; i < gameBoard[tetrominoRow].length; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
if( i == 0 || gameBoard[tetrominoRow][i-1] == 2) {
collision = true;
}
}
if(gameBoard[tetrominoRow+1][i] == 1) {
if( i == 0 || gameBoard[tetrominoRow+1][i-1] == 2) {
collision = true;
}
}
if(tetrominoRow + 2 < gameBoard.length && gameBoard[tetrominoRow+2][i] == 3) {
if( i == 0 || gameBoard[tetrominoRow+1][i-1] == 2) {
collision = true;
}
}
}
}
//move tetromino to the left if feasible
if(!collision) {
if(specialCaseOne) { //horizontal 'I'
for(int i = 0; i < gameBoard[tetrominoRow].length; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
gameBoard[tetrominoRow][i-1] = 1;
gameBoard[tetrominoRow][i+3] = 0;
i = gameBoard[tetrominoRow].length;
}
}
}
else if(specialCaseTwo) { //vertical 'I'
for(int i = 0; i < gameBoard[tetrominoRow].length; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
gameBoard[tetrominoRow][i-1] = 1; gameBoard[tetrominoRow][i] = 0;
gameBoard[tetrominoRow+1][i-1] = 1; gameBoard[tetrominoRow+1][i] = 0;
gameBoard[tetrominoRow+2][i-1] = 1; gameBoard[tetrominoRow+2][i] = 0;
gameBoard[tetrominoRow+3][i-1] = 1; gameBoard[tetrominoRow+3][i] = 0;
i = gameBoard[tetrominoRow].length;
}
}
}
else { //every other tetromino (spans over 2(min) to 3(max) rows)
//1st row
for(int i = 0; i < gameBoard[tetrominoRow].length; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
gameBoard[tetrominoRow][i-1] = 1;
if(i+1 < gameBoard[tetrominoRow].length && gameBoard[tetrominoRow][i+1] == 1) {
gameBoard[tetrominoRow][i] = 1;
if(i+2 < gameBoard[tetrominoRow].length && gameBoard[tetrominoRow][i+2] == 1) {
gameBoard[tetrominoRow][i+1] = 1;
gameBoard[tetrominoRow][i+2] = 0;
}
else{
gameBoard[tetrominoRow][i+1] = 0;
}
}
else {
gameBoard[tetrominoRow][i] = 0;
}
i = gameBoard[tetrominoRow].length;
}
}
//2nd row
for(int i = 0; i < gameBoard[tetrominoRow+1].length; i++) {
if(gameBoard[tetrominoRow+1][i] == 1) {
gameBoard[tetrominoRow+1][i-1] = 1;
if(i+1 < gameBoard[tetrominoRow+1].length && gameBoard[tetrominoRow+1][i+1] == 1) {
gameBoard[tetrominoRow+1][i] = 1;
if(i+2 < gameBoard[tetrominoRow+1].length && gameBoard[tetrominoRow+1][i+2] == 1) {
gameBoard[tetrominoRow+1][i+1] = 1;
gameBoard[tetrominoRow+1][i+2] = 0;
}
else{
gameBoard[tetrominoRow+1][i+1] = 0;
}
}
else {
gameBoard[tetrominoRow+1][i] = 0;
}
i = gameBoard[tetrominoRow].length;
}
}
//3rd row - if it's plausible
if( (tetrominoRow + 2) < gameBoard.length) {
for(int i = 0; i < gameBoard[tetrominoRow+2].length; i++) {
if(gameBoard[tetrominoRow+2][i] == 1) {
gameBoard[tetrominoRow+2][i-1] = 1;
if(i+1 < gameBoard[tetrominoRow+2].length && gameBoard[tetrominoRow+2][i+1] == 1) {
gameBoard[tetrominoRow+2][i] = 1;
if(i+2 < gameBoard[tetrominoRow+2].length && gameBoard[tetrominoRow+2][i+2] == 1) {
gameBoard[tetrominoRow+2][i+1] = 1;
gameBoard[tetrominoRow+2][i+2] = 0;
}
else{
gameBoard[tetrominoRow+2][i+1] = 0;
}
}
else {
gameBoard[tetrominoRow+2][i] = 0;
}
i = gameBoard[tetrominoRow].length;
}
}
}
}
}
}
public static void moveTetrominoRight(char[][] gameBoard) {
int tetrominoRow = -1; //holds the row of the tetromino
boolean tetrominoFound = false; //used to determine if row contains a tetromino
boolean collision = false; //used to determine if shift to right possible
boolean specialCaseOne = true; //it's a special case if tetromino is a horizontal 'I' shaped one
boolean specialCaseTwo = false; //it's also a special case if tetromino is a vertical 'I' shaped one
//find tetromino location in board
for(int i = 0; i < gameBoard.length && !tetrominoFound; i++) {
for(int j = 0; j < gameBoard[i].length; j++) {
if(gameBoard[i][j] == 1) {
tetrominoFound = true;
tetrominoRow = i;
}
}
}
//we assume that specialCaseOne applies and so we must test to determine if it 'doesn't' apply
if(tetrominoFound && tetrominoRow+1 != gameBoard.length) {
//the special case is basically when we just need to check the next row for whether the tetromino can be dropped
for(int j = 0; j < gameBoard[tetrominoRow].length; j++) {
if(gameBoard[tetrominoRow+1][j] == 1) {
specialCaseOne = false; //tetromino spans two rows and therefore specialCaseOne doesn't apply
}
}
}
//on the contrary, we need to determine if specialCaseTwo applies since we assume it doesn't apply initially
if(tetrominoFound && tetrominoRow+3 < gameBoard.length) {
for(int j = 0; j < gameBoard[tetrominoRow].length; j++) {
if(gameBoard[tetrominoRow][j] == 1 && gameBoard[tetrominoRow+1][j] == 1 && gameBoard[tetrominoRow+2][j] == 1 && gameBoard[tetrominoRow+3][j] == 1) {
specialCaseTwo = true; //tetromino spans four rows and therefore specialCaseTwo applies
}
}
}
//check if tetromino can be moved to the right
if(specialCaseOne) { //horizontal 'I'
for(int i = 0; i < gameBoard[tetrominoRow].length && !collision; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
if( i+3 == gameBoard[tetrominoRow].length-1 ||
gameBoard[tetrominoRow][i+4] == 2 ) {
collision = true;
}
i = gameBoard[tetrominoRow].length;
}
}
}
else if(specialCaseTwo) { //vertical 'I'
for(int i = 0; i < gameBoard[tetrominoRow].length && !collision; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
if( i == gameBoard[tetrominoRow].length-1 ||
gameBoard[tetrominoRow][i+1] != 0 ||
gameBoard[tetrominoRow+1][i+1] != 0 ||
gameBoard[tetrominoRow+2][i+1] != 0 ||
gameBoard[tetrominoRow+3][i+1] != 0) {
collision = true;
}
i = gameBoard[tetrominoRow].length;
}
}
}
else { //every other tetromino (spans over 2(min) to 3(max) rows)
for(int i = 0; i < gameBoard[tetrominoRow].length; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
if( i == gameBoard[tetrominoRow].length-1 ||
gameBoard[tetrominoRow][i+1] == 2 ) {
collision = true;
}
}
if(gameBoard[tetrominoRow+1][i] == 1) {
if( i == gameBoard[tetrominoRow+1].length-1 ||
gameBoard[tetrominoRow+1][i+1] == 2 ) {
collision = true;
}
}
if(tetrominoRow+2 < gameBoard.length && gameBoard[tetrominoRow+2][i] == 1) {
if( i == gameBoard[tetrominoRow+2].length-1 ||
gameBoard[tetrominoRow+2][i+1] == 2 ) {
collision = true;
}
}
}
}
//move tetromino to the right if feasible
if(!collision) {
if(specialCaseOne) { //horizontal 'I'
for(int i = 0; i < gameBoard[tetrominoRow].length; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
gameBoard[tetrominoRow][i+4] = 1;
gameBoard[tetrominoRow][i] = 0;
i = gameBoard[tetrominoRow].length;
}
}
}
else if(specialCaseTwo) { //vertical 'I'
for(int i = 0; i < gameBoard[tetrominoRow].length; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
gameBoard[tetrominoRow][i+1] = 1; gameBoard[tetrominoRow][i] = 0;
gameBoard[tetrominoRow+1][i+1] = 1; gameBoard[tetrominoRow+1][i] = 0;
gameBoard[tetrominoRow+2][i+1] = 1; gameBoard[tetrominoRow+2][i] = 0;
gameBoard[tetrominoRow+3][i+1] = 1; gameBoard[tetrominoRow+3][i] = 0;
i = gameBoard[tetrominoRow].length;
}
}
}
else { //every other tetromino (spans over 2(min) to 3(max) rows)
//1st row
for(int i = 0; i < gameBoard[tetrominoRow].length; i++) {
if(gameBoard[tetrominoRow][i] == 1) {
if(i+1 < gameBoard[tetrominoRow].length && gameBoard[tetrominoRow][i+1] == 1) {
if(i+2 < gameBoard[tetrominoRow].length && gameBoard[tetrominoRow][i+2] == 1) {
gameBoard[tetrominoRow][i+3] = 1;
}
gameBoard[tetrominoRow][i+2] = 1;
}
gameBoard[tetrominoRow][i+1] = 1;
gameBoard[tetrominoRow][i] = 0;
i = gameBoard[tetrominoRow].length;
}
}
//2nd row
for(int i = 0; i < gameBoard[tetrominoRow+1].length; i++) {
if(gameBoard[tetrominoRow+1][i] == 1) {
if(i+1 < gameBoard[tetrominoRow].length && gameBoard[tetrominoRow+1][i+1] == 1) {
if(i+2 < gameBoard[tetrominoRow].length && gameBoard[tetrominoRow+1][i+2] == 1) {
gameBoard[tetrominoRow+1][i+3] = 1;
}
gameBoard[tetrominoRow+1][i+2] = 1;
}
gameBoard[tetrominoRow+1][i+1] = 1;
gameBoard[tetrominoRow+1][i] = 0;
i = gameBoard[tetrominoRow+1].length;
}
}
//3rd row - if it's plausible
if( (tetrominoRow + 2) < gameBoard.length) {
for(int i = 0; i < gameBoard[tetrominoRow+2].length; i++) {
if(i+1 < gameBoard[tetrominoRow].length && gameBoard[tetrominoRow+2][i] == 1) {
if(i+2 < gameBoard[tetrominoRow].length && gameBoard[tetrominoRow+2][i+1] == 1) {
if(gameBoard[tetrominoRow+2][i+2] == 1) {
gameBoard[tetrominoRow+2][i+3] = 1;
}
gameBoard[tetrominoRow+2][i+2] = 1;
}
gameBoard[tetrominoRow+2][i+1] = 1;
gameBoard[tetrominoRow+2][i] = 0;
i = gameBoard[tetrominoRow+2].length;
}
}
}
}
}
}
public static void moveTetrominoDown(char[][] gameBoard) {
}
public static void rotateTetromino(char[][] gameBoard) {
}
public static void clearLines(char[][] gameBoard) {
int initLines = 0;
int linesCleared = 0;
boolean fullLine = true;
for(int i = gameBoard.length-1; i >= 0; i--) {
fullLine = true;
for(int j = 0; j < gameBoard[i].length; j++) {
if( gameBoard[i][j] == 0 ) {
fullLine = false;
}
}
if(fullLine) {
linesCleared++;
}
}
initLines = linesCleared; //need to preserve value as it is passed to clearGaps() function
for(int i = gameBoard.length-1; linesCleared > 0 && i >= 0; i --) {
for(int j = 0; j < gameBoard[i].length; j++) {
gameBoard[i][j] = 3;
}
linesCleared--;
try {
Thread.sleep(200);
printGameBoard(gameBoard);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
if(initLines > 0)
clearGaps(gameBoard, initLines);
}
public static void clearGaps(char[][] gameBoard, int linesCleared) {
int initLines = linesCleared;
for(int i = gameBoard.length-1; i >= initLines; i--) {
for(int j = 0; j < gameBoard[i].length; j++) {
gameBoard[i][j] = gameBoard[i-initLines][j];
}
linesCleared--;
}
for(int i = initLines; i >= 0; i--) {
for(int j = 0; j < gameBoard[i].length; j++) {
gameBoard[i][j] = 0;
}
}
try {
Thread.sleep(200);
printGameBoard(gameBoard);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
if(e.getKeyCode() == NativeKeyEvent.VC_A) {
leftKeyPressed = true;
rightKeyPressed = false;
downKeyPressed = false;
spaceKeyPressed = false;
}
else if(e.getKeyCode() == NativeKeyEvent.VC_D) {
leftKeyPressed = false;
rightKeyPressed = true;
downKeyPressed = false;
spaceKeyPressed = false;
}
else if(e.getKeyCode() == NativeKeyEvent.VC_X) {
leftKeyPressed = false;
rightKeyPressed = false;
downKeyPressed = true;
spaceKeyPressed = false;
}
else if(e.getKeyCode() == NativeKeyEvent.VC_SPACE) {
leftKeyPressed = false;
rightKeyPressed = false;
downKeyPressed = false;
spaceKeyPressed = true;
}
else if(e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
try {
GlobalScreen.unregisterNativeHook();
System.exit(0);
}
catch (NativeHookException ex){
ex.printStackTrace();
}
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
}
}