-
Notifications
You must be signed in to change notification settings - Fork 1
/
BlockGame_WS2801.ino
1198 lines (1050 loc) · 30.5 KB
/
BlockGame_WS2801.ino
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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
\file BlockGame_WS2801.ino
\brief Arduino sketch to run a Block Playing Game using a string of WS2801 RGB leds.
\remarks comments are implemented with Doxygen Markdown format
\section Contributors Contributors
\author Michael Flaga, www.flaga.net
\author Brian Adams, www.lansingmakersnetwork.org
\author Bill Porter, www.billporter.info
\author Mofidul Jamal
\licence This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
\section References References
\see
- Software
- <A HREF = "https://github.com/adafruit/Adafruit-WS2801-Library"> WS2801 Library</A>.
- <A HREF = "https://github.com/adafruit/Adafruit_NeoPixel"> Adafruit_NeoPixel Library</A>.
- <A HREF = "http://playground.arduino.cc//Main/WiiChuckClass?action=sourceblock&num=3"> Wii Nunchuck Library</A>.
- Hardware
- a Arduino
- a lot of either WS2801 or WS2811/WS2812s
- Wii Chuck
- <A HREF = https://www.sparkfun.com/products/9281"> WiiChuck Adapter - DEV-09281</A>.
\Note
- due to the larger number of the strings length the WS2801 is preferable over that of the WS2811, in that the WS2801 has the clock.
and WS2801 can be purchased to run off of 12 Volts, lowering the supply amperage.
- Note at this time the WiiChuckClass will only compile on an Arduino UNO or Mega, but not Micro/Leonardo.
*/
// Hardware Specific Constants
// Type of RGB:
#define NEOPIXEL // comment out if WS2801
//#define LEDCLKPIN 13 // if WS2801 (not NeoPixel) and commented out then Hardware SPI will be used.
#define LEDDATAPIN 11 // Pin of RGB data if not SPI, otherwise dont care.
// Panel Description:
//#define BOTTOM_LEFT // comment if first pixel is bottom left, otherwise it is bottom right.
//#define VERT_STRIPS // comment out if Horiztonal Rows otherwise Veritical Colomuns.
#define PROGRESSIVE // Comment out if stripes are zigzag .
#define FIELD_WIDTH 8 // Horizontal Width.
#define FIELD_HEIGHT 24 // Veritical Heigth.
#define LEDS FIELD_HEIGHT * FIELD_WIDTH
// Operational Constants:
#define FULL_INTENSITY 16 // 0 to 255 where lower values are dimmed to run off of smaller power supplies, good for testing.
#define MAX_IDLEBRICKS 4 //Number of Moves without user input, before Idle
#define TICK_DELAY 400 //game speed, millseconds between screen updates.
#define BOUNCE_DELAY 50 //weight given to the highest column for ai
#define HIGH_COLUMN_WEIGHT 5 //weight given to the number of holes for ai
#define HOLE_WEIGHT 3
#define ADD_EXTRA_BRICK_COUNT 20 // limit before adding last mystry brick
#ifdef NEOPIXEL
#include <Adafruit_NeoPixel.h>
#else // NEOPIXEL
#include <SPI.h>
#include <Adafruit_WS2801.h>
#endif // NEOPIXEL
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <Wire.h>
#include <WiiChuck.h>
//Interpreted WII Chuck response values
#define COUNTERCLOCKWISE 0
#define DOWN 1
#define RIGHT 2
#define LEFT 3
#define CLOCKWISE 4
#define NUMBEROFMOVES 5 // Idle/No input from WII Chuck
unsigned long next_tick = 0;
unsigned long bounce_tick = 0;
// Block Descriptions, Each brick consists of 4 patterns one for of each rotation,
// where each uint16_t pattern is 4 nibbles that stack to create the 4x4 block.
const PROGMEM uint16_t bricks[][4] = {
{
0b0100010001000100, //1x4 cyan
0b0000000011110000,
0b0100010001000100,
0b0000000011110000
},
{
0b0000010011100000, //T purple
0b0000010001100100,
0b0000000011100100,
0b0000010011000100
},
{
0b0000011001100000, //2x2 yellow
0b0000011001100000,
0b0000011001100000,
0b0000011001100000
},
{
0b0000000011100010, //L orange
0b0000010001001100,
0b0000100011100000,
0b0000011001000100
},
{
0b0000000011101000, //inverse L blue
0b0000110001000100,
0b0000001011100000,
0b0000010001000110
},
{
0b0000100011000100, //S green
0b0000011011000000,
0b0000100011000100,
0b0000011011000000
},
{
0b0000010011001000, //Z red
0b0000110001100000,
0b0000010011001000,
0b0000110001100000
},
#define EXTRABRICKS 2 // The Two Non-Cannon Bricks are easter egg or deterents.
{
0b0000111010101110, // doughnut
0b0000111010101110,
0b0000111010101110,
0b0000111010101110
},
{
0b0000101001001010, // X
0b0000101001001010,
0b0000101001001010,
0b0000101001001010,
}
};
uint8_t brick_count = sizeof(bricks)/sizeof(bricks[0]);
//8 bit RGB colors of blocks
//RRRBBBGG
const PROGMEM uint8_t brick_colors[]={
0b00011111, //cyan
0b10010000, //purple
0b11100011, //yellow
0b11100001, //orange?
0b00011100, //blue
0b00000011, //green
0b11100000, //red
0b11100111, //pale yellow
0b01011001, //pale purple
};
//You will need to modify this to translate fro x,y to a pixel number.
uint16_t computeAddress(int row, int col){
uint16_t reversed = 0;
#ifdef VERT_STRIPS
#ifndef PROGRESSIVE
if (col%2 == 0) {
reversed = 1;
}
#endif // PROGRESSIVE
uint16_t base = (col)*FIELD_HEIGHT;
if (reversed) {
base += FIELD_HEIGHT - 1;
}
uint16_t final = reverse == 1? base - row: base + row;
}
#else // VERT_STRIPS
#ifndef PROGRESSIVE
if (row%2 == 0){
reversed = 1;
}
#endif // PROGRESSIVE
uint16_t base = (row)*FIELD_WIDTH;
if (reversed) {
base += FIELD_WIDTH -1;
}
uint16_t final = reversed == 1 ? base - col: base + col;
#endif // VERT_STRIPS
return final;
}
byte wall[FIELD_WIDTH][FIELD_HEIGHT];
//The 'wall' is the 2D array that holds all bricks that have already 'fallen' into place
bool aiCalculatedAlready = false;
bool useAi = true;
struct TAiMoveInfo{
byte rotation;
int positionX, positionY;
int weight;
} aiCurrentMove;
struct TBrick{
byte type; //This is the current brick shape.
byte rotation; //active brick rotation
byte color; //active brick color
int positionX, positionY; //active brick position
byte pattern[4][4]; //2D array of active brick shape, used for drawing and collosion detection
} currentBrick;
//unsigned short level = 0;
//unsigned long score = 0;
//unsigned long score_lines = 0;
//WiiChuck
WiiChuck chuck = WiiChuck();
// Define the RGB pixel array and controller functions,
#ifdef NEOPIXEL
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, LEDDATAPIN, NEO_GRB + NEO_KHZ800);
#else // NEOPIXEL
#ifdef LEDCLKPIN
Adafruit_WS2801 strip = Adafruit_WS2801(LEDS, LEDDATAPIN, LEDCLKPIN);
#else // LEDCLKPIN
Adafruit_WS2801 strip = Adafruit_WS2801(LEDS);
#endif // LEDCLKPIN
#endif // NEOPIXEL
int idleBricks = 0;
int numberOfBricksSinceAI = 0;
int numberOfBricksInGame = 0;
void setup(){
Serial.begin(115200);
Serial.println(F("Starting Arduino BlockGame"));
Serial.print(F("Numbers of Possible Bricks = "));
Serial.println(sizeof(bricks)/sizeof(bricks[0]));
brick_count = (sizeof(bricks)/sizeof(bricks[0])) - EXTRABRICKS;
Serial.print(F("Numbers of Bricks in Play = "));
Serial.println(brick_count);
strip.begin();
//Pre-Operating Self Test of LED grid.
Serial.println(F("Starting Pre Operating Self Test"));
Serial.println(F("fade to Red"));
fadeGrid(Color(0,0,0), Color(FULL_INTENSITY,0,0), 8, 50); // fade from off to Red
Serial.println(F("fade to Green"));
fadeGrid(Color(FULL_INTENSITY,0,0), Color(0,FULL_INTENSITY,0), 8, 50); // fade from Red to Green
Serial.println(F("fade to Blue"));
fadeGrid(Color(0,FULL_INTENSITY,0), Color(0,0,FULL_INTENSITY), 8, 50); // fade from Green to Blue
Serial.println(F("fade to off"));
fadeGrid(Color(0,0,FULL_INTENSITY), Color(0,0,0), 8, 50); // fade from Blue to Off
Serial.println(F("Pre Operating Self Test Finished"));
Serial.print(F("useAI mode = "));
Serial.println(useAi);
chuck.begin();
chuck.update();
newGame();
}
void loop(){
//screenTest();
play();
}
//tests pixels
void screenTest(){
for( int i = 0; i < FIELD_WIDTH; i++ )
{
for( int k = 0; k < FIELD_HEIGHT; k++ )
{
wall[i][k] = 7;
drawGame();
delay(500);
}
}
}
//plays the game!
// globals, sorry (@RCK)
int ct = 0;
void play(){
ct++; // increment our tick counter
if(aiCalculatedAlready == false) {
performAI();
}
if (millis() > bounce_tick) {
byte command = getCommand();
if ( command != 4 ) {
bounce_tick = millis() + BOUNCE_DELAY;
}
/* To account for an oversensitive thumbstick,
we want to introduce a timer that prevents
commands from being processed too frequently. @RCK */
// if we're not on the AI, and there is a real command
// but < 7 loops have gone by, pretend it didn't happen.
if (!useAi && command < 4 && ct < 7) {
Serial.print(F("SKIPPED"));
Serial.println(ct);
} else { // ok, we can process this command.
// reset the tick counter if it's a command
if (command < 4)
ct = 0;
// process the command
if ( command == COUNTERCLOCKWISE ) {
Serial.println(F("ROTATE 90"));
bounce_tick = millis() + BOUNCE_DELAY*5;
if ( checkRotate( 1 ) == true ) {
rotate( 1 );
}
} else if ( command == CLOCKWISE ) {
Serial.println(F("ROTATE -90"));
bounce_tick = millis() + BOUNCE_DELAY*5;
if ( checkRotate( 0 ) == true ) {
rotate( 0 );
}
} else if ( command == RIGHT ) {
if ( checkShift( -1, 0 ) == true ) {
Serial.println(F("SHIFT RIGHT"));
shift( -1, 0 );
}
} else if ( command == LEFT ) {
if ( checkShift( 1, 0 ) == true ) {
Serial.println(F("SHIFT LEFT"));
shift( 1, 0 );
}
} else if ( command == DOWN ) {
Serial.print(F("D"));
moveDown();
}
}
}
if ( millis() > next_tick ) {
next_tick = millis()+TICK_DELAY;
moveDown();
}
drawGame();
}
//performs AI player calculations.
void performAI(){
Serial.println(F("AI performed"));
struct TBrick initialBrick;
//save position of the brick in its raw state
memcpy((void*)&initialBrick, (void*)¤tBrick, sizeof(TBrick));
//stores our 20 possible AI moves
struct TAiMoveInfo aiMoves[4 * FIELD_WIDTH];
//counter keeps track of the current index into our aimoves array
byte aiMoveCounter = 0;
//save position of the the brick at the left most rotated position
struct TBrick aiLeftRotatedBrick;
//save position of the brick at the rotated position
struct TBrick aiRotatedBrick;
//first check the rotations(initial, rotated once, twice, thrice)
for(int aiRotation = 0; aiRotation < 4; aiRotation++ )
{
//rotate if possible
if(checkRotate(1) == true)
rotate(1);
//save the rotated brick
memcpy((void*)&aiRotatedBrick, (void*)¤tBrick, sizeof(TBrick));
//shift as far left as possible
while(checkShift(-1,0) == true)
shift(-1, 0);
//save this leftmost rotated position
memcpy((void*)&aiLeftRotatedBrick, (void*)¤tBrick, sizeof(TBrick));
//now check each possible position of X
for(int aiPositionX = 0; aiPositionX < FIELD_WIDTH; aiPositionX++)
{
//next move down until we can't
while(checkGround() == false )
{
shift(0,1);
}
//calculate ai weight of this particular final position
int aiMoveWeight = aiCalculateWeight();
//save the weight, positions and rotations for this ai move
aiMoves[aiMoveCounter].weight = aiMoveWeight;
aiMoves[aiMoveCounter].rotation = currentBrick.rotation;
aiMoves[aiMoveCounter].positionX = currentBrick.positionX;
aiMoves[aiMoveCounter].positionY = currentBrick.positionY;
//move our index up for the next position to save to
aiMoveCounter++;
//drawGame();
//Serial.println(aiMoveWeight);
//delay(500);
//now restore the previous position and shift it right by the column # we are checking
memcpy((void*)¤tBrick, (void*)&aiLeftRotatedBrick, sizeof(TBrick));
if(checkShift(aiPositionX+1,0) == true)
shift(aiPositionX+1,0);
}
//reload rotated start position
memcpy((void*)¤tBrick, (void*)&aiRotatedBrick, sizeof(TBrick));
}
//at this point we have calculated all the weights of every possible position and rotation of the brick
//find move with lowest weight
int lowestWeight = aiMoves[0].weight;
int lowestWeightIndex = 0;
for(int i = 1; i < aiMoveCounter; i++)
{
if(aiMoves[i].weight <= lowestWeight)
{
lowestWeight = aiMoves[i].weight;
lowestWeightIndex = i;
}
}
//save this AI move as the current move
memcpy((void*)&aiCurrentMove, (void*)&aiMoves[lowestWeightIndex], sizeof(TAiMoveInfo));
//restore original brick that we started with
memcpy((void*)¤tBrick, (void*)&initialBrick, sizeof(TBrick));
//update the brick, set the ai flag so we know that we dont need to recalculate
updateBrickArray();
aiCalculatedAlready = true;
}
//calculates the ai weight
//when this function is called, the currentBrick is moved into a final legal position at the bottom of the wall
//which is why we add it to the wall first and then remove it at the end
int aiCalculateWeight(){
int weights = 0;
//add to wall first before calculating ai stuffs
addToWall();
//get the two weights
int highestColumn = getHighestColumn();
int holeCount = getHoleCount();
//if this position will yield a full completed row then its weight is 0, which is the lowest possible
//remember the the lowest weight will be the best move to make
if(getFullLinePossible() == true)
{
weights = 0;
}
else
{
weights = (HIGH_COLUMN_WEIGHT * highestColumn) + (HOLE_WEIGHT * holeCount);
}
removeFromWall(); //undo the wall addition when done
return weights;
}
//returns how high the wall goes
int getHighestColumn(){
int columnHeight = 0;
//count
int maxColumnHeight = 0;
for(int j = 0; j < FIELD_WIDTH; j++)
{
columnHeight = 0;
for(int k = FIELD_HEIGHT-1; k!=0; k--)
{
if(wall[j][k] != 0)
{
columnHeight = FIELD_HEIGHT - k;
//Serial.print(k);
//Serial.println(F(" is k"));
//delay(100);
}
}
if(columnHeight > maxColumnHeight)
maxColumnHeight = columnHeight;
}
return maxColumnHeight;
}
//counts the number of given holes for the ai calculation
int getHoleCount(){
int holeCount = 0;
for(int j = 0; j < FIELD_WIDTH; j++)
{
for(int k = currentBrick.positionY + 2; k < FIELD_HEIGHT; k++)
{
if(wall[j][k] == 0)
holeCount++;
}
}
return holeCount;
}
//determines if a full line is possible given the current wall (for ai)
bool getFullLinePossible()
{
int lineCheck;
for(byte i = 0; i < FIELD_HEIGHT; i++)
{
lineCheck = 0;
for(byte k = 0; k < FIELD_WIDTH; k++)
{
if( wall[k][i] != 0)
lineCheck++;
}
if(lineCheck == FIELD_WIDTH)
{
return true;
}
}
return false;
}
//gets commands according to ai state
byte getCommand(){
/*
if(currentBrick.rotation != aiCurrentMove.rotation)
return COUNTERCLOCKWISE;
if(currentBrick.positionX > aiCurrentMove.positionX)
return LEFT;
if(currentBrick.positionX < aiCurrentMove.positionX)
return RIGHT;
if(currentBrick.positionX == aiCurrentMove.positionX)
return DOWN;
*/
byte playerMove = NUMBEROFMOVES;
chuck.update();
int x = chuck.readJoyX();
int y = chuck.readJoyY();
if (chuck.buttonC && chuck.buttonZ) {
Serial.println(F("Both Button C & Z pushed."));
useAi = !useAi;
Serial.println(F("Toggling useAI mode"));
if (useAi) {
colorGrid(Color(FULL_INTENSITY, 0, 0));
Serial.println(F("useAI mode enabled"));
} else {
colorGrid(Color(0, FULL_INTENSITY, 0));
Serial.println(F("useAI mode disabled"));
}
strip.show();
delay(250);
} else if (chuck.buttonZ){
Serial.println(F("Button Z pushed."));
playerMove = COUNTERCLOCKWISE;
} else if (chuck.buttonC){
Serial.println(F("Button C pushed."));
playerMove = CLOCKWISE;
} else if ((x != -DEFAULT_ZERO_JOY_X) && (y != -DEFAULT_ZERO_JOY_Y)) {
if (x > 75){
Serial.print(F("RIGHT: Joy X > 75.("));
Serial.print(x);
Serial.println(F(")"));
#ifdef BOTTOM_LEFT
playerMove = LEFT;
#else // BOTTOM_LEFT
playerMove = RIGHT;
#endif // BOTTOM_LEFT
} else if (x < -75){
Serial.print(F("LEFT: Joy X < -75.("));
Serial.print(x);
Serial.println(F(")"));
#ifdef BOTTOM_LEFT
playerMove = RIGHT;
#else // BOTTOM_LEFT
playerMove = LEFT;
#endif // BOTTOM_LEFT
} else if ( y < -75 ){
Serial.print(F("DOWN: Joy Y < -75.("));
Serial.print(y);
Serial.println(F(")"));
playerMove = DOWN;
}
}
if (playerMove < NUMBEROFMOVES) {
idleBricks = 0;
Serial.println(F("resetting the idle manual brick count to zero"));
}
if (useAi){
if (playerMove < NUMBEROFMOVES) {
useAi = !useAi;
Serial.println(F("Toggling useAI mode OFF!"));
idleBricks = 0;
} else {
// Serial.print(currentBrick.rotation); Serial.print(F("|"));
// Serial.print(aiCurrentMove.rotation); Serial.print(F("|"));
// Serial.print(currentBrick.positionX); Serial.print(F("|"));
// Serial.print(aiCurrentMove.positionX); Serial.print(F(" "));
if(currentBrick.rotation != aiCurrentMove.rotation)
playerMove = COUNTERCLOCKWISE;
if(currentBrick.positionX > aiCurrentMove.positionX)
playerMove = RIGHT;
if(currentBrick.positionX < aiCurrentMove.positionX)
playerMove = LEFT;
if(currentBrick.positionX == aiCurrentMove.positionX)
playerMove = DOWN;
}
// } else {
// Serial.println("Problem in getcommand useAi");
}
chuck.update();
return playerMove;
}
//checks if the next rotation is possible or not.
bool checkRotate( bool direction )
{
rotate( direction );
bool result = !checkCollision();
rotate( !direction );
return result;
}
//checks if the current block can be moved by comparing it with the wall
bool checkShift(short right, short down)
{
shift( right, down );
bool result = !checkCollision();
shift( -right, -down );
return result;
}
// checks if the block would crash if it were to move down another step
// i.e. returns true if the eagle has landed.
bool checkGround()
{
shift( 0, 1 );
bool result = checkCollision();
shift( 0, -1 );
return result;
}
// checks if the block's highest point has hit the ceiling (true)
// this is only useful if we have determined that the block has been
// dropped onto the wall before!
bool checkCeiling()
{
for( int i = 0; i < 4; i++ )
{
for( int k = 0; k < 4; k++ )
{
if(currentBrick.pattern[i][k] != 0)
{
if( ( currentBrick.positionY + k ) < 0 )
{
return true;
}
}
}
}
return false;
}
//checks if the proposed movement puts the current block into the wall.
bool checkCollision()
{
int x = 0;
int y =0;
for( byte i = 0; i < 4; i++ )
{
for( byte k = 0; k < 4; k++ )
{
if( currentBrick.pattern[i][k] != 0 )
{
x = currentBrick.positionX + i;
y = currentBrick.positionY + k;
if(x >= 0 && y >= 0 && wall[x][y] != 0)
{
//this is another brick IN the wall!
return true;
}
else if( x < 0 || x >= FIELD_WIDTH )
{
//out to the left or right
return true;
}
else if( y >= FIELD_HEIGHT )
{
//below sea level
return true;
}
}
}
}
return false; //since we didn't return true yet, no collision was found
}
//updates the position variable according to the parameters
void shift(short right, short down)
{
currentBrick.positionX += right;
currentBrick.positionY += down;
}
// updates the rotation variable, wraps around and calls updateBrickArray().
// direction: 1 for clockwise (default), 0 to revert.
void rotate( bool direction )
{
if( direction == 1 )
{
if(currentBrick.rotation == 0)
{
currentBrick.rotation = 3;
}
else
{
currentBrick.rotation--;
}
}
else
{
if(currentBrick.rotation == 3)
{
currentBrick.rotation = 0;
}
else
{
currentBrick.rotation++;
}
}
updateBrickArray();
}
void moveDown(){
Serial.print(F("."));
if( checkGround() )
{
#ifdef DEGUB
Serial.print(F("checkGround()"));
Serial.print(F("bounce_tick = "));
Serial.print(bounce_tick);
Serial.print(F(" millis() = "));
Serial.println(millis());
#endif
addToWall();
#ifdef DEGUB
Serial.print(F("addToWall()"));
Serial.print(F("bounce_tick = "));
Serial.print(bounce_tick);
Serial.print(F(" millis() = "));
Serial.println(millis());
#endif
drawGame();
#ifdef DEGUB
Serial.print(F("drawGame()"));
Serial.print(F("bounce_tick = "));
Serial.print(bounce_tick);
Serial.print(F(" millis() = "));
Serial.println(millis());
#endif
if( checkCeiling() )
{
#ifdef DEGUB
Serial.print(F("Ceiling Found"));
Serial.print(F("bounce_tick = "));
Serial.print(bounce_tick);
Serial.print(F(" millis() = "));
Serial.println(millis());
#endif
gameOver();
}
else
{
while( clearLine() )
{
//scoreOneUpLine();
}
nextBrick();
//scoreOneUpBrick();
}
}
else
{
//grounding not imminent
shift( 0, 1 );
}
//scoreAdjustLevel();
//ticks = 0;
}
//put the brick in the wall after the eagle has landed.
void addToWall()
{
for( byte i = 0; i < 4; i++ )
{
for( byte k = 0; k < 4; k++ )
{
if(currentBrick.pattern[i][k] != 0){
wall[currentBrick.positionX + i][currentBrick.positionY + k] = currentBrick.color;
}
}
}
}
//removes brick from wall, used by ai algo
void removeFromWall(){
for( byte i = 0; i < 4; i++ )
{
for( byte k = 0; k < 4; k++ )
{
if(currentBrick.pattern[i][k] != 0){
wall[currentBrick.positionX + i][currentBrick.positionY + k] = 0;
}
}
}
}
//uses the currentBrick_type and rotation variables to render a 4x4 pixel array of the current block
// from the 2-byte binary reprsentation of the block
void updateBrickArray()
{
unsigned int data = pgm_read_word(&(bricks[ currentBrick.type ][ currentBrick.rotation ]));
for( byte i = 0; i < 4; i++ )
{
for( byte k = 0; k < 4; k++ )
{
if(bitRead(data, 4*i+3-k))
currentBrick.pattern[k][i] = currentBrick.color;
else
currentBrick.pattern[k][i] = 0;
}
}
}
//clears the wall for a new game
void clearWall()
{
for( byte i = 0; i < FIELD_WIDTH; i++ )
{
for( byte k = 0; k < FIELD_HEIGHT; k++ )
{
wall[i][k] = 0;
}
}
}
// find the lowest completed line, do the removal animation, add to score.
// returns true if a line was removed and false if there are none.
bool clearLine()
{
int line_check;
for( byte i = 0; i < FIELD_HEIGHT; i++ )
{
line_check = 0;
for( byte k = 0; k < FIELD_WIDTH; k++ )
{
if( wall[k][i] != 0)
line_check++;
}
if( line_check == FIELD_WIDTH )
{
flashLine( i );
for( int k = i; k >= 0; k-- )
{
for( byte m = 0; m < FIELD_WIDTH; m++ )
{
if( k > 0)
{
wall[m][k] = wall[m][k-1];
}
else
{
wall[m][k] = 0;
}
}
}
return true; //line removed.
}
}
return false; //no complete line found
}
//randomly selects a new brick and resets rotation / position.
void nextBrick(){
Serial.print(F("Next Brick"));
Serial.print(F(", useAI mode = "));
Serial.print(useAi);
Serial.print(F(", idleBricks = "));
Serial.println(idleBricks);
Serial.print(F("bounce_tick = "));
Serial.print(bounce_tick);
Serial.print(F(" millis() = "));
Serial.println(millis());
numberOfBricksInGame++;
Serial.print(F("numberOfBricksInGame = "));
Serial.println(numberOfBricksInGame);
if (!useAi) {
numberOfBricksSinceAI++;
if (numberOfBricksSinceAI > (ADD_EXTRA_BRICK_COUNT * 1.5)) {
brick_count = sizeof(bricks)/sizeof(bricks[0]);
} else if (numberOfBricksSinceAI > ADD_EXTRA_BRICK_COUNT) {
brick_count = sizeof(bricks)/sizeof(bricks[0]) - (EXTRABRICKS / 2);
} else {
brick_count = (sizeof(bricks)/sizeof(bricks[0])) - EXTRABRICKS;
}
} else {
numberOfBricksSinceAI = 0;
brick_count = (sizeof(bricks)/sizeof(bricks[0])) - EXTRABRICKS;
}
Serial.print(F("numberOfBricksSinceAI = "));
Serial.println(numberOfBricksSinceAI);
Serial.print(F("Numbers of Bricks in Play = "));
Serial.println(brick_count);
if (!useAi) {
idleBricks++;
if (idleBricks > MAX_IDLEBRICKS) {
useAi = !useAi;
Serial.print(F("MAX_IDLEBRICKS of "));
Serial.print(MAX_IDLEBRICKS);
Serial.println(F(" exceeded"));
Serial.println(F("Swithing to AI mode!"));
idleBricks = 0;
}
Serial.print(F("idle manual bricks = "));
Serial.println(idleBricks);
}
currentBrick.rotation = 0;
currentBrick.positionX = round(FIELD_WIDTH / 2) - 2;
currentBrick.positionY = -3;
currentBrick.type = random( 0, brick_count );
currentBrick.color = pgm_read_byte(&(brick_colors[ currentBrick.type ]));
aiCalculatedAlready = false;
updateBrickArray();
//displayPreview();
}
//effect, flashes the line at the given y position (line) a few times.
void flashLine( int line ){
bool state = 1;
for(byte i = 0; i < 6; i++ )
{
for(byte k = 0; k < FIELD_WIDTH; k++ )
{
if(state)
wall[k][line] = 0b11111111;
else
wall[k][line] = 0;
}
state = !state;
drawWall();
updateDisplay();
delay(200);
}
}
//draws wall only, does not update display
void drawWall(){
for(int j=0; j < FIELD_WIDTH; j++){
for(int k = 0; k < FIELD_HEIGHT; k++ )
{
draw(wall[j][k],FULL_INTENSITY,j,k);
}
}
}
//'Draws' wall and game piece to screen array
void drawGame()
{