// "   YushkoTetris,   "
//        ,      :
// 1)       "YushkoTetris"       .
// 2)    "COPYRIGHTED BY YUSHKO OLEG"       .
// 3)     / -     
//    YushkoTetris    COPYRIGHTED BY YUSHKO OLEG.     
//        ( VasyaTetris)    MODIFIED BY Vasya Pupkin.
// 4)     "YushkoTetris" (  ,   1)  
//            ( ,  ,  )   
//     .
//        :
//     "VasyaTetris"      "YushkoTetris,    "
// 5) 4-    ,      . ,     
//     :   ,     !

program YushkoTetris;
var
   Pole : array[0..11, 0..21] of integer; //     .  -   .
   mPole : array[1..4, 1..4] of integer; //  .
   zPole : array[1..4, 1..4] of integer; //      .

   i,k : integer; //i -       k -        . :     5  ,         1 .
   Speed : integer; //   .   -   .  ,     .   Delay
   GameOver : boolean; //"" - , "" - 

   Figurka : integer; //1..7 -  ,   (      )
   //NextFigurka : integer; //   
   //NextColor1 : integer; //  

   Povorot : integer; // 1..4, 1..2 -  .
   PovorotDone : boolean; // 1000-     
   PovorotDone2: boolean; //     

   Color1 : integer;//  

   //Proverka : boolean;//-     .

   KeyInput : integer;//     

   x,y : integer;//   

   j,z : integer;// 
   u,q   : integer;// 

   w,h : integer;//         
   dw,dh : integer;//     ,   

   Score : integer;// 
   NScore: integer;// 
   Lines : integer;//  
   NLines: integer;// 

   UI : integer;//UI=3, delay=14     .  :    1000/(delay(14)*UI)=1000/(14*3)=~22   
   LSpeed : integer;
const
   //    
   r_01=000;g_01=000;b_01=064;
   r_02=000;g_02=064;b_02=128;
   r_03=000;g_03=128;b_03=255;

   r_04=064;g_04=000;b_04=128;
   r_05=128;g_05=000;b_05=255;

   r_06=000;g_06=064;b_06=000;
   r_07=000;g_07=128;b_07=064;
   r_08=000;g_08=255;b_08=128;

   r_09=064;g_09=128;b_09=000;
   r_10=128;g_10=255;b_10=000;

   r_11=064;g_11=000;b_11=000;
   r_12=128;g_12=064;b_12=000;
   r_13=255;g_13=128;b_13=000;

   r_14=128;g_14=000;b_14=064;
   r_15=255;g_15=000;b_15=128;
{---------------------------------------------------------------------}
function CheckRotatingBrick(tX,tY:integer):boolean;
//     (     )
{tX,tY -      ,           }
//  tX,tY -     ""  ""
begin
    CheckRotatingBrick:=true;

    for j:=1 to 4 do
    for z:=1 to 4 do
    if (tX+j-1<12) and (tX+j-1>-1) and (tY+z-1>-1) and (tY+z-1<22) then
    begin
         if (Pole[tX+j-1,tY+z-1]>0) and (zPole[j,z]>0) then CheckRotatingBrick:=false;
    end;
end;
{---------------------------------------------------------------------}
function CheckFallingBrick(tX,tY:integer):boolean;
//   (      )
//tX,tY -   .
begin
    CheckFallingBrick:=false;

    for j:=1 to 4 do
    for z:=1 to 4 do
    if (tX+j-1<12) and (tX+j-1>-1) and (tY+z-1>-1) and (tY+z-1<22) then
    begin
         if (Pole[tX+j-1,tY+z-1]>0) and (mPole[j,z]>0) then CheckFallingBrick:=true;
    end;
end;
{---------------------------------------------------------------------}
procedure initial_varibles;
// 
begin
     GameOver:=false;
     //Speed:=LSpeed/2;
     x:=4;
     y:=1;
     PovorotDone:=false;
     Color1:=random(15)+1;
     Figurka:=1;
     Speed:=60;
     if speed<50 then k:=LSpeed else k:=Speed;
     UI:=1;
     LSpeed:=60;

     Score:=0;
     Lines:=0;

     {           .   ,   midletpascale    trunc, mod  div
        trunc,    trunc(),     }
     w:=getWidth;
     h:=getHeight;

     if h<=2*w then
     begin
          q:=Trunc(h/25);
          dw:=Trunc((w-q*12)/2);
          dh:=2*q;

          w:=q;
          h:=q;
     end
     else
     begin
          q:=Trunc(w/15);
          dw:=q;//Trunc((w-q*12)/2);
          dh:=Trunc((h-q*24)/2+q);

          w:=q;
          h:=q;
     end;
     {           .   ,   midletpascale    trunc, mod, div
        trunc,    trunc(),     }

     // ,     .
     for i:=0 to 11 do Pole[i,0]:=Color1;
     for i:=0 to 11 do Pole[i,21]:=Color1;
     for i:=0 to 21 do Pole[0,i]:=Color1;
     for i:=0 to 21 do Pole[11,i]:=Color1;
end;
{---------------------------------------------------------------------}
procedure interface_update(a:boolean);
//  
// - "" -      (    ,    )
begin
		{ }
		setColor(255, 255, 255);
		fillRect(0, 0, getWidth, getHeight);

    // 
    for j:=0 to 11 do
    for z:=0 to 21 do
    if Pole[j,z]>0 then
    begin
         if Pole[j,z]=01 then setColor(r_01,g_01,b_01);
         if Pole[j,z]=02 then setColor(r_02,g_02,b_02);
         if Pole[j,z]=03 then setColor(r_03,g_03,b_03);
         if Pole[j,z]=04 then setColor(r_04,g_04,b_04);
         if Pole[j,z]=05 then setColor(r_05,g_05,b_05);
         if Pole[j,z]=06 then setColor(r_06,g_06,b_06);
         if Pole[j,z]=07 then setColor(r_07,g_07,b_07);
         if Pole[j,z]=08 then setColor(r_08,g_08,b_08);
         if Pole[j,z]=09 then setColor(r_09,g_09,b_09);
         if Pole[j,z]=10 then setColor(r_10,g_10,b_10);
         if Pole[j,z]=11 then setColor(r_11,g_11,b_11);
         if Pole[j,z]=12 then setColor(r_12,g_12,b_12);
         if Pole[j,z]=13 then setColor(r_13,g_13,b_13);
         if Pole[j,z]=14 then setColor(r_14,g_14,b_14);
         if Pole[j,z]=15 then setColor(r_15,g_15,b_15);
         fillRect(j*w+dw, z*h+dh, w-1, h-1);
    end;

    //  
    if a=true then
    for j:=1 to 4 do
    for z:=1 to 4 do
    if mPole[j,z]>0 then
    begin
         if mPole[j,z]=01 then setColor(r_01,g_01,b_01);
         if mPole[j,z]=02 then setColor(r_02,g_02,b_02);
         if mPole[j,z]=03 then setColor(r_03,g_03,b_03);
         if mPole[j,z]=04 then setColor(r_04,g_04,b_04);
         if mPole[j,z]=05 then setColor(r_05,g_05,b_05);
         if mPole[j,z]=06 then setColor(r_06,g_06,b_06);
         if mPole[j,z]=07 then setColor(r_07,g_07,b_07);
         if mPole[j,z]=08 then setColor(r_08,g_08,b_08);
         if mPole[j,z]=09 then setColor(r_09,g_09,b_09);
         if mPole[j,z]=10 then setColor(r_10,g_10,b_10);
         if mPole[j,z]=11 then setColor(r_11,g_11,b_11);
         if mPole[j,z]=12 then setColor(r_12,g_12,b_12);
         if mPole[j,z]=13 then setColor(r_13,g_13,b_13);
         if mPole[j,z]=14 then setColor(r_14,g_14,b_14);
         if mPole[j,z]=15 then setColor(r_15,g_15,b_15);
         fillRect((j+x-1)*w+dw, (z+y-1)*h+dh, w-1, h-1);
    end;

    //    (, , )
    setColor(r_01,g_01,b_01);
    drawText('Score='+integerToString(Score)+'|Lines='+integerToString(Lines), w, 0);
    drawText('COPYRIGHTED BY YUSHKO OLEG', w, (22)*h+dh+h);

    repaint;
end;
{---------------------------------------------------------------------}
procedure NewBrick;
//   
begin
    if speed<50 then k:=LSpeed else k:=Speed;//       , -   

    //      ( - 1/28)
    Figurka:=random(28)+1;
    if (Figurka>=1) and  (Figurka<=5)  then Figurka:=1;
    if (Figurka>=6) and  (Figurka<=9)  then Figurka:=2;
    if (Figurka>=10) and (Figurka<=13) then Figurka:=3;
    if (Figurka>=14) and (Figurka<=18) then Figurka:=4;
    if (Figurka>=19) and (Figurka<=22) then Figurka:=5;
    if (Figurka>=23) and (Figurka<=27) then Figurka:=6;
    if  (Figurka=28) then                   Figurka:=7;

    //  
    Color1:=random(15)+1;
    Povorot:=1;//  

    //     Figurka (1 - , 7 -   . . +    )
    If Figurka=1 then
    begin
    mPole[1,1]:=000000;mPole[2,1]:=Color1;mPole[3,1]:=Color1;mPole[4,1]:=000000;
    mPole[1,2]:=000000;mPole[2,2]:=Color1;mPole[3,2]:=Color1;mPole[4,2]:=000000;
    mPole[1,3]:=000000;mPole[2,3]:=000000;mPole[3,3]:=000000;mPole[4,3]:=000000;
    mPole[1,4]:=000000;mPole[2,4]:=000000;mPole[3,4]:=000000;mPole[4,4]:=000000;
    x:=4;y:=1;
    end;

    If Figurka=2 then
    begin
    mPole[1,1]:=000000;mPole[2,1]:=000000;mPole[3,1]:=Color1;mPole[4,1]:=Color1;
    mPole[1,2]:=000000;mPole[2,2]:=Color1;mPole[3,2]:=Color1;mPole[4,2]:=000000;
    mPole[1,3]:=000000;mPole[2,3]:=000000;mPole[3,3]:=000000;mPole[4,3]:=000000;
    mPole[1,4]:=000000;mPole[2,4]:=000000;mPole[3,4]:=000000;mPole[4,4]:=000000;
    x:=4;y:=1;
    end;

    If Figurka=3 then
    begin
    mPole[1,1]:=000000;mPole[2,1]:=Color1;mPole[3,1]:=Color1;mPole[4,1]:=000000;
    mPole[1,2]:=000000;mPole[2,2]:=000000;mPole[3,2]:=Color1;mPole[4,2]:=Color1;
    mPole[1,3]:=000000;mPole[2,3]:=000000;mPole[3,3]:=000000;mPole[4,3]:=000000;
    mPole[1,4]:=000000;mPole[2,4]:=000000;mPole[3,4]:=000000;mPole[4,4]:=000000;
    x:=4;y:=1;
    end;

    If Figurka=4 then
    begin
    mPole[1,1]:=000000;mPole[2,1]:=Color1;mPole[3,1]:=000000;mPole[4,1]:=000000;
    mPole[1,2]:=Color1;mPole[2,2]:=Color1;mPole[3,2]:=Color1;mPole[4,2]:=000000;
    mPole[1,3]:=000000;mPole[2,3]:=000000;mPole[3,3]:=000000;mPole[4,3]:=000000;
    mPole[1,4]:=000000;mPole[2,4]:=000000;mPole[3,4]:=000000;mPole[4,4]:=000000;
    x:=5;y:=1;
    end;

    If Figurka=5 then
    begin
    mPole[1,1]:=000000;mPole[2,1]:=Color1;mPole[3,1]:=000000;mPole[4,1]:=000000;
    mPole[1,2]:=000000;mPole[2,2]:=Color1;mPole[3,2]:=Color1;mPole[4,2]:=Color1;
    mPole[1,3]:=000000;mPole[2,3]:=000000;mPole[3,3]:=000000;mPole[4,3]:=000000;
    mPole[1,4]:=000000;mPole[2,4]:=000000;mPole[3,4]:=000000;mPole[4,4]:=000000;
    x:=4;y:=1;
    end;

    If Figurka=6 then
    begin
    mPole[1,1]:=000000;mPole[2,1]:=000000;mPole[3,1]:=Color1;mPole[4,1]:=000000;
    mPole[1,2]:=Color1;mPole[2,2]:=Color1;mPole[3,2]:=Color1;mPole[4,2]:=000000;
    mPole[1,3]:=000000;mPole[2,3]:=000000;mPole[3,3]:=000000;mPole[4,3]:=000000;
    mPole[1,4]:=000000;mPole[2,4]:=000000;mPole[3,4]:=000000;mPole[4,4]:=000000;
    x:=5;y:=1;
    end;

    If Figurka=7 then
    begin
    mPole[1,1]:=000000;mPole[2,1]:=000000;mPole[3,1]:=000000;mPole[4,1]:=000000;
    mPole[1,2]:=Color1;mPole[2,2]:=Color1;mPole[3,2]:=Color1;mPole[4,2]:=Color1;
    mPole[1,3]:=000000;mPole[2,3]:=000000;mPole[3,3]:=000000;mPole[4,3]:=000000;
    mPole[1,4]:=000000;mPole[2,4]:=000000;mPole[3,4]:=000000;mPole[4,4]:=000000;
    x:=4;y:=0;
    end;
end;
{---------------------------------------------------------------------}
procedure PovorotFigurki;
//  ,   
begin
    PovorotDone2:=false;//      PovorotFigurki

    If (Figurka=2) and (PovorotDone=false) then
    begin
         if (Povorot=1) and (PovorotDone2=false) then
         begin
              //  .
              zPole[1,1]:=000000;zPole[2,1]:=Color1;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=Color1;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              //   ,         
              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=2) and (PovorotDone2=false) then
         begin
              //  .
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=Color1;zPole[4,1]:=Color1;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              //   ,         
              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              //    ,       - ( , ):
              if (CheckRotatingBrick(x-1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x-1;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;
    end;

    If (Figurka=3) and (PovorotDone=false) then
    begin
         if (Povorot=1) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=Color1;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=Color1;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=2) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=Color1;zPole[3,1]:=Color1;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=000000;zPole[3,2]:=Color1;zPole[4,2]:=Color1;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x-1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x-1;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;
    end;

    If (Figurka=4) and (PovorotDone=false) then
    begin
         if (Povorot=1) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=Color1;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=Color1;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=2) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=Color1;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=Color1;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=3;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x+1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x+1;
                   Povorot:=3;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=3) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=Color1;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=Color1;zPole[2,2]:=Color1;zPole[3,2]:=000000;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=Color1;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=4;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=4) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=Color1;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=Color1;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x-1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x-1;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;
    end;

    If (Figurka=5) and (PovorotDone=false) then
    begin
         if (Povorot=1) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=Color1;zPole[4,1]:=Color1;
              zPole[1,2]:=000000;zPole[2,2]:=000000;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=Color1;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x,y+1)=true) and (PovorotDone2=false) then
              begin
                   y:=y+1;
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=2) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=Color1;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=000000;zPole[4,3]:=Color1;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=3;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x+1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x+1;
                   Povorot:=3;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=3) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=Color1;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=000000;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=Color1;zPole[3,3]:=Color1;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=4;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=4) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=Color1;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=Color1;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x+1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x+1;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x-1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x-1;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;
    end;

    If (Figurka=6) and (PovorotDone=false) then
    begin
         if (Povorot=1) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=Color1;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=000000;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=Color1;zPole[3,3]:=Color1;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x,y+1)=true) and (PovorotDone2=false) then
              begin
                   y:=y+1;
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=2) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=Color1;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=Color1;zPole[2,3]:=000000;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=3;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x+1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x+1;
                   Povorot:=3;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=3) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=Color1;zPole[2,1]:=Color1;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=000000;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=Color1;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=4;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=4) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=Color1;zPole[4,1]:=000000;
              zPole[1,2]:=Color1;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x-1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x-1;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;
    end;

    If (Figurka=7) and (PovorotDone=false) then
    begin
         if (Povorot=1) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=Color1;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=000000;zPole[2,2]:=Color1;zPole[3,2]:=000000;zPole[4,2]:=000000;
              zPole[1,3]:=000000;zPole[2,3]:=Color1;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=Color1;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x,y+1)=true) and (PovorotDone2=false) then
              begin
                   y:=y+1;
                   Povorot:=2;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;

         if (Povorot=2) and (PovorotDone2=false) then
         begin
              zPole[1,1]:=000000;zPole[2,1]:=000000;zPole[3,1]:=000000;zPole[4,1]:=000000;
              zPole[1,2]:=Color1;zPole[2,2]:=Color1;zPole[3,2]:=Color1;zPole[4,2]:=Color1;
              zPole[1,3]:=000000;zPole[2,3]:=000000;zPole[3,3]:=000000;zPole[4,3]:=000000;
              zPole[1,4]:=000000;zPole[2,4]:=000000;zPole[3,4]:=000000;zPole[4,4]:=000000;

              if CheckRotatingBrick(x,y)=true then
              begin
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x+1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x+1;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x-1,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x-1;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;

              if (CheckRotatingBrick(x-2,y)=true) and (PovorotDone2=false) then
              begin
                   x:=x-2;
                   Povorot:=1;
                   PovorotDone2:=true;

                   mPole[1,1]:=zPole[1,1];mPole[2,1]:=zPole[2,1];mPole[3,1]:=zPole[3,1];mPole[4,1]:=zPole[4,1];
                   mPole[1,2]:=zPole[1,2];mPole[2,2]:=zPole[2,2];mPole[3,2]:=zPole[3,2];mPole[4,2]:=zPole[4,2];
                   mPole[1,3]:=zPole[1,3];mPole[2,3]:=zPole[2,3];mPole[3,3]:=zPole[3,3];mPole[4,3]:=zPole[4,3];
                   mPole[1,4]:=zPole[1,4];mPole[2,4]:=zPole[2,4];mPole[3,4]:=zPole[3,4];mPole[4,4]:=zPole[4,4];
              end;
         end;
    end;
end;
{---------------------------------------------------------------------}
procedure FigurkaUpala;
// 
begin
    for j:=1 to 4 do
    for z:=1 to 4 do
    if (x+j-1<12) and (x+j-1>-1) and (y+z-1>-1) and (y+z-1<22) then //, -   Pole[-1,10], ,   Pole[0..11,0..21]
    begin
         if mPole[j,z]<>0 then Pole[x+j-1,y+z-1]:=mPole[j,z];
    end;
end;
{---------------------------------------------------------------------}
procedure userInput;
//  
begin
    KeyInput:=getKeyPressed;//  
    if (KeyInput = KE_KEY6) or (keyToAction(KeyInput) = GA_RIGHT) then//   
        begin
             PovorotDone:=false;//    ,    .     -  .
             if CheckFallingBrick(x+1,y)=false then
             Begin
                  x:=x+1;// !
                  if CheckFallingBrick(x,y+1)=true then if speed<50 then k:=LSpeed else k:=Speed;//    ,    k   .        .      !
                  interface_update(true);// 
             End;
        end;
    if (KeyInput = KE_KEY4) or (keyToAction(KeyInput) = GA_LEFT) then//   
        begin
             PovorotDone:=false;//    ,    .     -  .
             if CheckFallingBrick(x-1,y)=false then
             begin
                  x:=x-1;// !
                  if CheckFallingBrick(x,y+1)=true then if speed<50 then k:=LSpeed else k:=Speed;//    ,    k   .        .      !
                  interface_update(true);// 
             end;
        end;
    if (KeyInput = KE_KEY8) or (keyToAction(KeyInput) = GA_DOWN) then//    1 
        begin
             PovorotDone:=false;//    ,    .     -  .
             if CheckFallingBrick(x,y+1)=false then
             begin
                  y:=y+1;k:=Speed;//  +     k!
                  if CheckFallingBrick(x,y+1)=true then if speed<50 then k:=LSpeed else k:=Speed;//    ,    k   .        .      !
                  interface_update(true);// 
             end;
        end;
    if (KeyInput = KE_KEY2) or (keyToAction(KeyInput) = GA_UP) or (keyToAction(KeyInput) = GA_FIRE) then// 
        begin
             PovorotFigurki;//  
             PovorotDone:=true;//    ,    .     -  .
             if CheckFallingBrick(x,y+1)=true then if speed<50 then k:=LSpeed else k:=Speed;//    ,    k   .        .      !
             interface_update(true);// 
        end;
    if (KeyInput = KE_KEY9) then GameOver:=true;
    if (KeyInput = KE_NONE) then PovorotDone:=false;
end;
{---------------------------------------------------------------------}
procedure CheckFullLine;
//    + ,   
var
   a : array [1..20] of boolean;
   Ta: boolean;
begin
    Ta:=false;//     ""
    NScore:=0;
    NLines:=0;

    //  
    For z:=1 to 20 do
    begin
         a[z]:=true;
         for j:=1 to 10 do if Pole[j,z]=0 then begin a[z]:=false; end;
         if a[z]=true then
         begin
              Ta:=true;//  ""
              NLines:=NLines+1;

              if NLines=1 then NScore:=100;
              if NLines=2 then NScore:=300;
              if NLines=3 then NScore:=700;
              if NLines=4 then NScore:=1500;

//              if NScore>0 then NScore:=NScore*2;
 //             if NScore=0 then NScore:=100;
         end;
    end;

    //  
    if Ta=true then
    For u:=1 to 10 do
    begin
         if a[01]=true then  Pole[u,1]:=0;
         if a[02]=true then  Pole[11-u,2]:=0;
         if a[03]=true then  Pole[u,3]:=0;
         if a[04]=true then  Pole[11-u,4]:=0;
         if a[05]=true then  Pole[u,5]:=0;
         if a[06]=true then  Pole[11-u,6]:=0;
         if a[07]=true then  Pole[u,7]:=0;
         if a[08]=true then  Pole[11-u,8]:=0;
         if a[09]=true then  Pole[u,9]:=0;
         if a[10]=true then  Pole[11-u,10]:=0;
         if a[11]=true then  Pole[u,11]:=0;
         if a[12]=true then  Pole[11-u,12]:=0;
         if a[13]=true then  Pole[u,13]:=0;
         if a[14]=true then  Pole[11-u,14]:=0;
         if a[15]=true then  Pole[u,15]:=0;
         if a[16]=true then  Pole[11-u,16]:=0;
         if a[17]=true then  Pole[u,17]:=0;
         if a[18]=true then  Pole[11-u,18]:=0;
         if a[19]=true then  Pole[u,19]:=0;
         if a[20]=true then  Pole[11-u,20]:=0;

         interface_update(false);delay(50);// 
    end;

    //   
    For i:=2 to 20 do
    begin
         if a[i]=true then
         begin
              for j:=i downto 2 do
              for z:=1 to 10 do Pole[z,j]:=Pole[z,j-1];

              for z:=1 to 10 do Pole[z,1]:=0;
         end;
    end;

    //   ,    .   !
    if a[1]=true then for z:=1 to 10 do Pole[z,1]:=0;

    Score:=Score+NScore;// 
    Lines:=Lines+NLines;// 

    //       
(*    if (Lines>=00) and (Lines<=09) then Speed:=60;
    if (Lines>=10) and (Lines<=19) then Speed:=40;
    if (Lines>=20) and (Lines<=29) then Speed:=31;
    if (Lines>=30) and (Lines<=39) then Speed:=24;
    if (Lines>=40) and (Lines<=49) then Speed:=19;
    if (Lines>=50) and (Lines<=59) then Speed:=15;
    if (Lines>=60) and (Lines<=69) then Speed:=12;
    if (Lines>=70) and (Lines<=79) then Speed:=10;
    if (Lines>=80) and (Lines<=89) then Speed:=9;
    if (Lines>=90)                 then Speed:=8;*)
    //Lines:=90;

    if (Lines>=00) and (Lines<=09) then begin Speed:=60; LSpeed:=35; end;
    if (Lines>=10) and (Lines<=19) then begin Speed:=40; LSpeed:=33; end;
    if (Lines>=20) and (Lines<=29) then begin Speed:=31; LSpeed:=31; end;
    if (Lines>=30) and (Lines<=39) then begin Speed:=24; LSpeed:=28; end;
    if (Lines>=40) and (Lines<=49) then begin Speed:=16; LSpeed:=25; end;
    if (Lines>=50) and (Lines<=59) then begin Speed:=13; LSpeed:=22; end;
    if (Lines>=60) and (Lines<=69) then begin Speed:=09; LSpeed:=19; end;
    if (Lines>=70) and (Lines<=79) then begin Speed:=07; LSpeed:=16; end;
    if (Lines>=80) and (Lines<=89) then begin Speed:=04; LSpeed:=13; end;
    if (Lines>=90)                then begin Speed:=02; LSpeed:=10; end;
    // ,       ,   
    interface_update(false);
end;
{---------------------------------------------------------------------}
begin
    initial_varibles;// ,   
    NewBrick;//   
    KeyInput:=getKeyPressed;//    
    interface_update(true);// 

    //  .       1 .
    while (GameOver=false) do
    begin
         //  .      ,  ,       "" ,      
         while (k>0) and (GameOver=false) do
         begin
              k:=k-1;
              if UI=3 then begin userInput;UI:=1; end else UI:=UI+1;// 3      
              delay(14);//  
         end;
         if CheckFallingBrick(x,y)=true then GameOver:=true;// ,     
         if CheckFallingBrick(x,y+1)=true then
         begin 
              FigurkaUpala;CheckFullLine;NewBrick;
              if CheckFallingBrick(x,y)=true then GameOver:=true;//      - game over
              if speed<50 then k:=LSpeed else k:=Speed;//      4 ,     
              interface_update(true);// 
         end
         else
         begin
              y:=y+1;//   1 ,    
              interface_update(true);// 
              k:=Speed;//   k  "  "
              if CheckFallingBrick(x,y+1)=true then if speed<50 then k:=LSpeed else k:=Speed;//    ,    k   .        .      !
         end;
     //    if CheckFallingBrick(x,y+1)=true then k:=Speed*2 else k:=Speed;
    end;
  delay(5000);//   
end.

