//wiring: connect the matrix 9, 14, 8, 12, 1, 7, 2, 5 pins to the 22-29 on the arduino and
//the 13, 3, 4, 10, 6, 11, 15, 16 pins to pins 32-39
// 1 => 26
// 2 => 28
// 3 => 31
// 4 => 32
// 5 => 29
// 6 => 34
// 7 => 27
// 8 => 24
// 9 => 22
//10 => 33
//11 => 35
//12 => 25
//13 => 30
//14 => 23
//15 => 36
//16 => 37
//int rowb[8]={46, 47, 48, 49, 50, 51, 52, 53}; //13, 3, 4, 10, 6, 11, 15, 16
//int colb[8]={38, 39, 40, 41, 42, 43, 44, 45}; //9, 14, 8, 12, 1, 7, 2, 5
//int rowa[8]={30, 31, 32, 33, 34, 35, 36, 37};
//int cola[8]={22, 23, 24, 25, 26, 27, 28, 29};
int cola[8]={46, 47, 48, 49, 50, 51, 52, 53}; //13, 3, 4, 10, 6, 11, 15, 16
int rowa[8]={38, 39, 40, 41, 42, 43, 44, 45}; //9, 14, 8, 12, 1, 7, 2, 5
int colb[8]={30, 31, 32, 33, 34, 35, 36, 37};
int rowb[8]={22, 23, 24, 25, 26, 27, 28, 29};
int buttons[4]={2, 3, 4, 5};
void show(int s[16][8]) //Brightness 0-10. int as boolean is false, only if it's 0
{
for(int k=0; k<5; k++) //for the brightness
{
//for(int i=0; i<8; i++){for(int j=0; j<8; j++){if(s[i][j]<0)s[i][j]=0;}}
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++){digitalWrite(cola[j], HIGH); digitalWrite(rowa[j], LOW); digitalWrite(colb[j], HIGH); digitalWrite(rowb[j], LOW);}
digitalWrite(cola[i], LOW);
digitalWrite(colb[i], LOW);
for(int j=0; j<8; j++)
{
boolean vara=false;
if(s[i][j]>k) vara=true;
digitalWrite(rowa[j], vara);
boolean varb=false;
if(s[i+8][j]>k) varb=true;
digitalWrite(rowb[j], varb);
}
delayMicroseconds(50);
}
}
}
int button()
{
for(int i=0; i<4; i++)
{
if(digitalRead(buttons[i])) return i+1;
}
return 0;
}
int time, time2;
int tomb[16][8];
int fix[16][8];
long long int go=0;
int b;
boolean forms[5][9]={
{0,1,0,0,1,0,0,1,1}, //L
{0,1,0,0,1,0,1,1,0}, //reverse L
{0,1,0,0,1,1,0,1,0}, //T
{0,0,0,0,1,1,1,1,0}, //S
{0,0,0,1,1,0,0,1,1}, //Z
};
boolean actual[3][3];
int x, y;
boolean reached=true;
boolean ended=false;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
pinMode(12,OUTPUT); //for reset
digitalWrite(12, LOW);
for(int i=0; i<8; i++)
{
pinMode(rowa[i], OUTPUT);
pinMode(cola[i], OUTPUT);
pinMode(rowb[i], OUTPUT);
pinMode(colb[i], OUTPUT);
}
for(int i=0; i<4; i++) pinMode(buttons[i], INPUT);
time=0;
time2=0;
for(int i=0; i<16; i++){for(int j=0; j<8; j++) fix[i][j]=0;}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
if(button()>0) b=button();
if(time+3<millis()/100) //otherwise it's gonna be too fast
{
//if reached the bottom, generate a new one
if(reached){
reached=false;
int n=random(5);
for(int i=0; i<3; i++){for(int j=0; j<3; j++)actual[i][j]=forms[n][i*3+j];}
x=3;
y=14;
}
time=millis()/100;
//handling the buttons
switch(b){
case 1: //up, rotate
boolean tmp[3][3];
tmp[0][0]=actual[2][0];
tmp[0][1]=actual[1][0];
tmp[0][2]=actual[0][0];
tmp[1][0]=actual[2][1];
tmp[1][1]=actual[1][1];
tmp[1][2]=actual[0][1];
tmp[2][0]=actual[2][2];
tmp[2][1]=actual[1][2];
tmp[2][2]=actual[0][2];
for(int i=0; i<3; i++){for(int j=0; j<3; j++)actual[i][j]=tmp[i][j];}
break;
case 2: //left
if(!(((x==1)&&(actual[0][0]||actual[1][0]||actual[2][0]))||((x==0)&&(actual[0][1]||actual[1][1]||actual[2][1]))))x--;
break;
case 3: //right
if(!(((x==6)&&(actual[0][2]||actual[1][2]||actual[2][2]))||((x==7)&&(actual[0][1]||actual[1][1]||actual[2][1]))))x++;
break;
case 4: //down, go down
y--;
break;
}
b=0;
// for(int i=0; i<16; i++)
// {
// for(int j=0; j<8; j++) if(go==i && j>1 && j<6)tomb[i][j]=5; else tomb[i][j]=0;
// }
} //if "it's button time" end
if(time2+1<millis()/500)
{
time2=millis()/500;
y--;
}//if "it's falling time" end
//generate the graphics
for(int i=0; i<16; i++){for(int j=0; j<8; j++) tomb[i][j]=fix[i][j];} //the fix parts first
//then the moving part
for(int i=0; i<3; i++){for(int j=0; j<3; j++){if(actual[i][j]) tomb[y-1+i][x-1+j]=10;}}
//did it reached the bottom?
if((actual[2][0] && (fix[y-1][x-1]>0))||(actual[2][1] && (fix[y-1][x]>0))||( actual[2][2] && (fix[y-1][x+1]>0))||
(actual[1][0] && (fix[y][x-1]>0))||(actual[1][1] && (fix[y][x]>0))||( actual[1][2] && (fix[y][x+1]>0))||
(actual[0][0] && (fix[y+1][x-1]>0))||(actual[0][1] && (fix[y+1][x]>0))||( actual[0][2] && (fix[y+1][x+1]>0)) ||
((actual[1][0] || actual[1][1] ||actual[1][2]) && (y==0))||
((actual[0][0] || actual[0][1] ||actual[0][2]) && (y==1))
)
{
reached=true;
for(int i=0; i<3; i++){for(int j=0; j<3; j++){if(actual[i][j]) fix[y-1+i][x-1+j]=5;}}
if(y==14) ended=true;
//full line
for(int i=0; i<16; i++){
long int ch=1;
for(int j=0; j<8; j++) ch=ch*fix[i][j];
if(ch>0){
for(int k=i; k<15; k++)
{for(int l=0; l<8; l++)
fix[k][l]=fix[k+1][l];
}
for(int k=0; k<8; k++) fix[15][k]=0;
}
}
}//if reached the bottom end
show(tomb); //it has to be shown all the time, otherwise it would been blinking
//if game over
if(ended)
{
int time3=millis()/1000;
while(time3+3>millis()/1000)
{show(tomb);
if(button()>0) digitalWrite(12, HIGH);
}
while(true){
for(int i=0; i<16; i++)
{
for(int j=0; j<8; j++) tomb[i][j]=0;
time3=millis()/500;
while(time3+1>millis()/500)
{show(tomb);
if(button()>0) digitalWrite(12, HIGH);
}
}
for(int i=0; i<16; i++)
{
for(int j=0; j<8; j++) tomb[i][j]=5;
time3=millis()/500;
while(time3+1>millis()/500) show(tomb);
}
}
}
}