-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRussianGameBoard.java
More file actions
708 lines (619 loc) · 19.3 KB
/
RussianGameBoard.java
File metadata and controls
708 lines (619 loc) · 19.3 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
package game2048;
import javax.swing.JFrame;
public class RussianGameBoard implements GameBoard {
protected Block2048[][] gameArray;
int length = 0;
int centerColumn = 0;
int height = 0;
private int capacity = 0;
private int numberOfBlocks = 0;
public int gameScore = 0;
public int combineTotal = 0;
public boolean needToPopulate = false;
public int iPosition = 0;;
public int jPosition = 0;
public RussianGameBoard(JFrame frame,int passedLength, int passedHeight) {
gameArray = new Block2048[passedHeight][passedLength];
length = passedLength;
height = passedHeight;
centerColumn = (passedLength/2);
fillGameBoard(frame);
}
//This function fills the gameboard with blocks which have a value of zero.
private void fillGameBoard(JFrame frame)
{
for(int row = 0; row < height; row++)
{
for(int col = 0; col < length; col++)
{
gameArray[row][col] = new Block2048(frame, 0, true);
gameArray[row][col].setX(col*67);
gameArray[row][col].setY(row*67 + Controller2048.barHeight);
}
}
populate();
}
@Override
//This function moves the falling block one space to the right each time it is called
public void moveRight() {
for(int row = height-1; row >= 0; row--)
{
for(int col = length-1; col >=0; col--)
{
//Identifies the falling block
if(gameArray[row][col].getLockedIn() == false)
{
//prevents out of bounds exception or falling block moving into already occupied space.
if(col != length-1 &&(gameArray[row][col+1].getValue()==0))
{
gameArray[row][col+1].setBlockValue(gameArray[row][col].getValue());
gameArray[row][col+1].setLockedIn(false);
gameArray[row][col].setBlockValue(0);
}
}
}
}
}
@Override
//This moves the falling block one space to the left each time it is called
public void moveLeft() {
for(int row = height-1; row >= 0; row--)
{
for(int col = 0; col < length; col++)
{
//Identifies the falling block
if(gameArray[row][col].getLockedIn() == false)
{
//prevents out of bounds exception or falling block moving into already occupied space.
if(col != 0 &&(gameArray[row][col-1].getValue()==0))
{
gameArray[row][col-1].setBlockValue(gameArray[row][col].getValue());
gameArray[row][col-1].setLockedIn(false);
gameArray[row][col].setBlockValue(0);
}
}
}
}
}
@Override
public void moveUp() {
//empty stub function
}
//This function causes the falling block to fall one space each time it is called.
public void fall() {
boolean wasThereACombination = false;
boolean combo = false;
int loopCount = 1;
//printArray();
for(int row = height-1; row >= 0; row--)
{
for(int col = 0; col < length; col++)
{
//If falling block is not on the bottom row
if(gameArray[row][col].getLockedIn() == false && row != height-1)
{
//if the space below the falling block is open, then block falls.
if(lookDown(row,col).getValue()==0)
{
gameArray[row+1][col].setBlockValue(gameArray[row][col].getValue());
gameArray[row+1][col].setLockedIn(false);
gameArray[row][col].setBlockValue(0);
}
//if there is a block beneath falling block then lock the falling block in place and look for combinations around it.
else
{
gameArray[row][col].setLockedIn(true);
//look for combinations around the block
wasThereACombination = combineAround(row,col);
//while loop continues to make combinations until there are no more on the board.
while(wasThereACombination)
{
wasThereACombination = false;
loopCount ++;
for(int combineRow = height-1; combineRow >= 0; combineRow--)
{
for(int combineCol = 0; combineCol < length; combineCol++)
{
wasThereACombination = combineAround(combineRow,combineCol);
}
}
}
needToPopulate = true;
}
}
//looks for a block which was falling but is now on the bottom row.
if(gameArray[row][col].getLockedIn() == false && row == height-1)
{
//locks the block in place so it won't continue to fall
gameArray[row][col].setLockedIn(true);
//initiates the combination sequence
wasThereACombination = combineAround(row,col);
//continues to combine throughout the gameboard until there are no more combinations
while(wasThereACombination)
{
wasThereACombination = false;
for(int combineRow = height-1; combineRow >= 0; combineRow--)
{
for(int combineCol = 0; combineCol < length; combineCol++)
{
wasThereACombination = combineAround(combineRow,combineCol);
//if there was a combination it sets the sentinel value to true to keep the combination loop going else sentinel value = false and exit loop
}
}
}
needToPopulate = true;
}
}
}
printArray();
}
@Override
//This moves all blocks which have open space beneath them down.
public void moveDown() {
for(int col = 0; col < length; col++)
{
int moveTo = -1;
for(int row = height-1; row > 0; row--)
{
if(gameArray[row][col].getValue() == 0)
{
moveTo = row;
break;
}
}
if(moveTo != -1)
{
for(int moveFrom = moveTo - 1; moveFrom >= 0; moveFrom--)
{
if(gameArray[moveFrom][col].getValue() != 0)
{
if(gameArray[moveFrom][col].isNewCombination())
{
gameArray[moveTo][col].setNewCombination(true);
gameArray[moveFrom][col].setNewCombination(false);
}
gameArray[moveTo][col].setBlockValue(gameArray[moveFrom][col].getValue());
gameArray[moveFrom][col].setBlockValue(0);
moveTo --;
}
}
}
}
printArray();
}
//This function takes the falling block and drops it to the bottom empty space in the column.
public boolean dropBlock()
{
boolean didWeDropABlock = false;
int column = lookForColumn();
if(column != -1)
{
int bottom = lookForBottom(column);
for(int row = 0; row < height; row++)
{
for(int col = 0; col < length; col++)
{
if(gameArray[row][col].getLockedIn()==false)
{
gameArray[bottom][col].setBlockValue(gameArray[row][col].getValue());
gameArray[bottom][col].setLockedIn(false);
gameArray[row][col].setBlockValue(0);
fall();
didWeDropABlock = true;
return didWeDropABlock;
}
}
}
needToPopulate = true;
}
System.out.println("Finished Drop Block");
return didWeDropABlock;
}
//This function looks for the column which has the falling block in it
public int lookForColumn()
{
int columnValue = -1;
for(int row = height-1; row >= 0; row--)
{
for(int col = 0; col < length; col++)
{
if(gameArray[row][col].getLockedIn()==false)
{
columnValue = gameArray[row][col].getArrayX();
}
}
}
return columnValue;
}
//this function looks for the lowest open space in the column the falling block is in. This open space will be the space the falling block will fall to.
public int lookForBottom(int column)
{
int bottom = 0;
for(int row = height-1; row > 0; row--)
{
if(gameArray[row][column].getValue() == 0)
{
bottom = row;
break;
}
}
return bottom;
}
@Override
//determines if game is over by looking at the center column and determining if it is full.
public boolean isGameOver() {
boolean gameOver = false;
if(isFull())
{
gameOver = true;
}
return gameOver;
}
//returns the block above the block which is looking.
public Block2048 lookUp(int row, int col) {
return gameArray[row-1][col];
}
@Override
//returns the block to the right the block which is looking.
public Block2048 lookRight(int row, int col) {
return gameArray[row][col+1];
}
@Override
//returns the block beneath the block which is looking.
public Block2048 lookDown(int row, int col) {
return gameArray[row+1][col];
}
@Override
//returns the block to the left the block which is looking.
public Block2048 lookLeft(int row, int col) {
return gameArray[row][col-1];
}
//Populate generates the block that will fall from the top center. A random number between 0 and 1 is generated and depending on that number, a block with a certain value is populated.
public void populate() {
System.out.println("populated new block");
double whatNumber = Math.random();
if(whatNumber < 0.167)
{
gameArray[0][centerColumn].setBlockValue(2);
}
else if(whatNumber > 0.167 && whatNumber <0.333)
{
gameArray[0][centerColumn].setBlockValue(4);
}
else if(whatNumber >= 0.334 && whatNumber < 0.499)
{
gameArray[0][centerColumn].setBlockValue(8);
}
else if(whatNumber >= 0.499 && whatNumber < 0.664)
{
gameArray[0][centerColumn].setBlockValue(16);
}
else if(whatNumber >= 0.664 && whatNumber < 0.83)
{
gameArray[0][centerColumn].setBlockValue(32);
}
else
{
gameArray[0][centerColumn].setBlockValue(64);
}
gameArray[0][centerColumn].setLockedIn(false);
}
@Override
//determines if the gameArray is full
public boolean isFull() {
boolean full = false;
if(gameArray[0][centerColumn].getValue()!=0 && gameArray[0][centerColumn].getLockedIn()==true)
{
full = true;
}
return full;
}
//returns a boolean value which indicates if a block needs to be populated.
public boolean getNeedToPopulate()
{
return needToPopulate;
}
//sets the boolean value which indicates if a block needs to be populated
public void setNeedToPopulate(boolean setValue)
{
needToPopulate = setValue;
}
@Override
//tells the block at row/col to draw itself.
public void draw()
{
for(int row = 0; row < height; row++)
{
for(int col = 0; col < length; col++)
{
gameArray[row][col].drawBlock();
}
}
}
//This function looks for combinations and combines around the block which calls the function.
public boolean combineAround(int row, int col)
{
boolean didWeCombine = false;
gameArray[row][col].setNewCombination(false);
combineTotal= gameArray[row][col].getValue();
Block2048 myBlock = gameArray[row][col];
//If the block is in the top row
if(row==0)
{
//If block is in the top row, left column
if(col==0)
{
if(lookRight(row,col).getValue() == gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookRight(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookDown(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookDown(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
//If block is in the top row, right column.
else if(col== length-1)
{
if(lookLeft(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookLeft(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookDown(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookDown(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
//If block is in the top row but not in the left or right corner
else
{
if(lookRight(row,col).getValue() == gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookRight(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookLeft(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookLeft(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookDown(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookDown(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
}
// If block is in the bottom row
else if(row == height-1)
{
//If block is in the bottom left corner
if(col == 0)
{
if(lookRight(row,col).getValue() == gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookRight(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookUp(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookUp(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
//If block is in the bottom row, right column
else if(col == length -1)
{
if(lookLeft(row,col).getValue()== gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookLeft(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookUp(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookUp(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
//If block is in the bottom row, but not in the bottom corners
else
{
if(lookRight(row,col).getValue() == gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookRight(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookLeft(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookLeft(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookUp(row,col).getValue()== gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookUp(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
}
//If block is in the left column, but not in the top or bottom left corners
else if(col == 0)
{
//System.out.println("LeftColumn combine start: " + combineTotal);
if(lookRight(row,col).getValue() == gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
//System.out.println("Left Column combine: " +combineTotal);
lookRight(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookUp(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
//System.out.println("Left Column combine: " +combineTotal);
lookUp(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookDown(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
//System.out.println("Left Column combine: " +combineTotal);
lookDown(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
//If block is in the right column, but not in the top or bottom right corners
else if(col== length -1)
{
if(lookLeft(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookLeft(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookUp(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookUp(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(lookDown(row,col).getValue()==gameArray[row][col].getValue() && gameArray[row][col].getValue() != 0)
{
combineTotal = combineTotal*2;
lookDown(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
//If block is away from all edges
else
{
//System.out.println("Middle Array combine start: " + combineTotal);
if(gameArray[row][col].getValue() != 0 && (lookRight(row,col).getValue() == gameArray[row][col].getValue()))
{
combineTotal = combineTotal*2;
//System.out.println("Middle Array combine: " + combineTotal);
lookRight(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if( gameArray[row][col].getValue() != 0 && (lookLeft(row,col).getValue()==gameArray[row][col].getValue()))
{
combineTotal = combineTotal*2;
//System.out.println("Middle Array combine: " + combineTotal);
lookLeft(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(gameArray[row][col].getValue() != 0 && (lookUp(row,col).getValue()==gameArray[row][col].getValue()))
{
combineTotal = combineTotal*2;
//System.out.println("Middle Array combine: " + combineTotal);
lookUp(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
if(gameArray[row][col].getValue() != 0 && (lookDown(row,col).getValue()==gameArray[row][col].getValue()))
{
combineTotal = combineTotal*2;
//System.out.println("Middle Array combine: " + combineTotal);
lookDown(row,col).setBlockValue(0);
gameArray[row][col].setNewCombination(true);
setScore(combineTotal);
}
gameArray[row][col].setBlockValue(combineTotal);
}
didWeCombine = gameArray[row][col].isNewCombination();
moveDown();
combineTotal = 0;
return didWeCombine;
}
@Override
public void combineRight() {
}
@Override
public void combineLeft() {
}
@Override
public void combineUp() {
}
@Override
public void combineDown() {
}
public boolean combineAll() {
boolean combine = true;
for(int col = 0; col < length; col++)
{
for(int row = height-1; row > 0; row--)
{
combine = combineAround(row,col);
}
}
return combine;
}
public void printArray()
{
for(int row = 0; row < height; row++)
{
for(int col = 0; col < length; col++)
{
System.out.print(gameArray[row][col].getLockedIn() + " ");
}
System.out.println(" ");
}
System.out.println();
System.out.println();
}
public int getScore()
{
return gameScore;
}
public void setScore(int addToScore)
{
gameScore += addToScore;
}
}