Lines_98_v02

Author: Inkov Evgeni
Miscellaneous
It plays sound alertsUses files from the file systemIt writes information to fileIt reads information from a file
0 Views
0 Downloads
0 Favorites
Lines_98_v02
ÿþ//+------------------------------------------------------------------+

//|                                                 Lines_98_v02.mq4 |

//+------------------------------------------------------------------+

// A 4K@0<8

//+------------------------------------------------------------------+

#define Rang 6 // :>;8G5AB2> H0@>2, A :>B>@KE =0G8=05BAO C40;5=85

//+------------------------------------------------------------------+

#property copyright "Inkov Evgeni"

#property link      "ew123@mail.ru"

//+------------------------------------------------------------------+

#property version   "1.00"

#property strict

//+------------------------------------------------------------------+

input bool sound=true;

//+------------------------------------------------------------------+

color clr_Text =clrWhite;

color clr_Take =clrWhite;

color clr_Fon  =clrLightGray;

color clr_1    =clrBlue;

color clr_2    =clrRed;

color clr_3    =clrBrown;

color clr_4    =clrMagenta;

color clr_5    =clrOrange;

color clr_hole =clrBlack; // F25B 4K@K

//+------------------------------------------------------------------+

int x0_=0,y0_=0,di_=0;

int X_Take=-1,Y_Take=-1;

string pref="L98_";

//..........................

int mas[9][9]; // 0-?CAB.<5AB>, 1...5 - F25B,6-4K@0

int mas_copy[9][9]; // 0-?CAB.<5AB>, 1...5 - F25B,6-4K@0

int sum;

string name_glob=pref+"sum";

int mas_next[3][3]; //0-x,1-y,2-n_clr

//..........................

string but_Load=pref+"Load";

string but_Save=pref+"Save";

         // 70?8AL 2 D09;5: 81 OG59:0

int n_col; // 0-?CAB>, 1...5 =><5@ F25B0 >A=.H0@>2, -1...-5, =>< F25B0 A;54.H0@>2

string name_file=pref;

int contr_sum;

int contr_sum_save;

int kol_ball;

int x_hole,y_hole;



int mas_hole[][2]; //0-x,1-y,

int kol_hole=1,kol_hole_pred;

enum type_way

{

   up=0,

   dw=1,

   lt=2,

   rt=3

};

int mas_way[][3]; //0-x,1-y,2-way (type_way)

int kol_way;



int time_sec=0;

int max_time=9;

bool pause=false;

//+------------------------------------------------------------------+

int OnInit()

{

   if (UninitializeReason()!=REASON_CHARTCHANGE)

   {

      ChartSetInteger(0,CHART_SHOW_OBJECT_DESCR,false);

      ChartSetInteger(0,CHART_FOREGROUND,false);

      //...........................

      find_krd_panel(x0_,y0_,di_);

      //...........................

      ArrayInitialize(mas,0);

      ArrayInitialize(mas_copy,0);

      ArrayInitialize(mas_next,0);

      out_Panel(x0_,y0_,di_,X_Take,Y_Take);

      //...........................

      kol_hole_pred=1;

      kol_hole=1;

      ArrayResize(mas_hole,kol_hole);

      ArrayInitialize(mas_hole,-1);

   }

   EventSetMillisecondTimer(1000);

   //...........................

   contr_sum=Load_file(false);

   contr_sum_save=0;

   //...........................

   pause=false;

   OnTick();

   

   return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

   if (UninitializeReason()!=REASON_CHARTCHANGE)

   {

      ObjectsDeleteAll(0,pref);

      EventKillTimer();

   }

   Comment("");

}

//+------------------------------------------------------------------+

void OnTick()

{

}

//+------------------------------------------------------------------+

void OnTimer()

{

   time_sec--;

   if (time_sec<0)time_sec=max_time;

   osn_proc();

   

}

//+------------------------------------------------------------------+

void osn_proc()

{

   find_krd_panel(x0_,y0_,di_);

   out_Panel(x0_,y0_,di_,X_Take,Y_Take);

   //...........................

   int count_0_box=find_count_0_box(mas);

   if (count_0_box==0)

   {

      ArrayInitialize(mas_hole,-1);



      if (sound)PlaySound("timeout.wav");

      ArrayInitialize(mas,0);

      ObjectsDeleteAll(0,pref+"s");

      return;

   }

   

   if (count_0_box==81)

   {

      sum=0;

      ust_hole();  // CAB.4K@C

      ust_3_ball();

      ust_3_ball_next();

   }

   out_box();

   //...................

   out_ball_next(mas_next[0][0],mas_next[0][1],0);

   out_ball_next(mas_next[1][0],mas_next[1][1],1);

   out_ball_next(mas_next[2][0],mas_next[2][1],2);

   //.....................

   if (time_sec==0)out_next();

}

//------------------------

void out_box()

{

   contr_sum=0;

   kol_ball=0;

   for(int i=0;i<9;i++)

      for(int j=0;j<9;j++)

      {

         if (mas[i][j]>0)

         {

            kol_ball++;

            set_ball(i,j,mas[i][j],di_);

            contr_sum+=(i+1)*(j+1)*(mas[i][j]+1);

         }

         else

            contr_sum+=(i+1)*(j+1);

      }

}

//--------------------------

void ust_hole()

{

   if (ArraySize(mas_hole)==0)

   {

      ArrayResize(mas_hole,1);

      ArrayInitialize(mas_hole,-1);

      kol_hole=1;

      kol_hole_pred=1;

   }

   if (mas_hole[0][0]>=0) // C40;8BL ?@56=85 4K@K

      for(int i=0;i<kol_hole;i++)

      {

         int x1=mas_hole[i][0];

         int y1=mas_hole[i][1];

         mas[x1,y1]=0;

         int x=x0_-di_*(8-x1);

         int y=y0_+di_*(8-y1);

         out_Rect(x1,y1,x,y,di_+2,clr_Fon,"f");

      }

   //................................

   max_time=9;

   kol_hole=1;

   if (sum>50)

   {  // 87<5=8BL :>;.4K@

      if (sum>550){kol_hole=12;max_time=5;}else

      if (sum>500){kol_hole=11;max_time=5;}else

      if (sum>450){kol_hole=10;max_time=6;}else

      if (sum>400){kol_hole=9; max_time=6;}else

      if (sum>350){kol_hole=8; max_time=7;}else

      if (sum>300){kol_hole=7; max_time=7;}else

      if (sum>250){kol_hole=6; max_time=8;}else

      if (sum>200){kol_hole=5; max_time=8;}else

      if (sum>150){kol_hole=4; max_time=9;}else

      if (sum>100){kol_hole=3; max_time=9;}else

      {kol_hole=2;max_time=9;}  

      if (kol_hole_pred != kol_hole)

      {

         ArrayResize(mas_hole,kol_hole);

         kol_hole_pred=kol_hole;

      }

   }

   //................................

   for(int i=0;i<kol_hole;i++)

   {

      ust_ball(6);

      mas_hole[i][0]=x_hole;

      mas_hole[i][1]=y_hole;

   }

}

//-----------------------------

void ust_3_ball()

{

   int n_clr1=find_n_color_ball();

   int n_clr2=find_n_color_ball();

   int n_clr3=find_n_color_ball();

   ust_ball(n_clr1);

   ust_ball(n_clr2);

   ust_ball(n_clr3);

}

///-------------------------------

void ust_3_ball_next()

{

   mas_next[0][2]=find_n_color_ball();

   mas_next[1][2]=find_n_color_ball();

   mas_next[2][2]=find_n_color_ball();

   ArrayCopy(mas_copy,mas);

   ust_ball_next(0);

   ust_ball_next(1);

   ust_ball_next(2);

}

///-------------------------------

int find_n_color_ball()

{

   return(rand_n(4)+1);

}

//----------------------

void ust_ball(int n_clr)

{

   int count_0_box=find_count_0_box(mas);

   if (count_0_box>0)

   {

      int r=rand_n(count_0_box-1);

      set_rand_ball(r,n_clr);

   }

}

//----------------------

void ust_ball_next(int n_next)

{

   int count_0_box=find_count_0_box(mas_copy);

   if (count_0_box>0)

   {

      int r=rand_n(count_0_box-1);

      set_rand_ball_next(r,n_next);

   }

}

//----------------------

void set_rand_ball (int r,int n_clr)

{

   bool br=false;

   int n=-1;

   for(int i=0;i<9;i++)

   {

      for(int j=0;j<9;j++)

      {

         if (mas[i][j]>0)continue;

         n++;

         if (n==r)

         {

            mas[i][j]=n_clr;

            out_ball(i,j);

            if (n_clr==6)

            {

               x_hole=i;

               y_hole=j;

            }

            br=true;

            break;

         }

      }

      if (br)break;

   }

}

//-----------------------

void set_rand_ball_next (int r,int n_next)

{

   bool br=false;

   int n=-1;

   for(int i=0;i<9;i++)

   {

      for(int j=0;j<9;j++)

      {

         if (mas[i][j]>0)continue;

         n++;

         if (n==r)

         {

            mas_next[n_next][0]=i;

            mas_next[n_next][1]=j;

            out_ball_next(i,j,n_next);

            br=true;

            break;

         }

      }

      if (br)break;

   }

}

//-----------------------

void out_ball(int i,int j)

{

   int dd=di_/6;

   int d=0;

   int n_clr=mas[i][j];

   if (n_clr<=5)

      for(int k=0;k<6;k++)

      {

         d+=dd;

         set_ball(i,j,n_clr,d);

         Sleep(20);

         WindowRedraw();

      }

   else

      set_ball(i,j,n_clr,di_);

}

//-------------------

void out_ball_next(int i,int j,int n_next)

{

   int d=di_/3;

   set_ball(i,j,mas_next[n_next][2],d);

   WindowRedraw();

}

//-------------------

void set_ball(int i,int j,int n_clr, int d)

{

   color clr=clr_1;

   switch(n_clr)

   {

      case 1 : clr=clr_1;break;

      case 2 : clr=clr_2;break;

      case 3 : clr=clr_3;break;

      case 4 : clr=clr_4;break;

      case 5 : clr=clr_5;break;

      case 6 : clr=clr_hole;break;

   }

   //...........................

   int x=x0_-di_*(8-i);

   int y=y0_+di_*(8-j);

   if (n_clr<6)

      LabelCreate(0,pref+StringConcatenate("s",i,j),0,x+di_/2,(int)MathCeil(y+di_/2+di_*0.03),"l","wingdings",(int)MathCeil(d*0.7),clr);

    else

      out_Rect(i,j,x,y,d+2,clr_hole,"f");

}

//------------------------------

int find_count_0_box(int& M[9][9])

{

   int n=0;

   for(int i=0;i<9;i++)

      for(int j=0;j<9;j++)

         if (M[i][j]==0)n++;

   return(n);

}

//----------------------

int rand_n(int n)

{

   int r=(int)MathRound(1.0*MathRand()/32767*n);

   return(r);

}

//-------------------

void out_Panel(int x0,int y0,int di,int x_take,int y_take)

{        // 2K25AB8 ?>4;>6:C

   out_Rect(9,9,x0-8*di,y0-di,9*di,clrBlack,"p",0,BORDER_FLAT,CORNER_LEFT_UPPER,clrNONE,STYLE_SOLID,2,false);

   //.....................

   for(int i=0;i<9;i++) // 2K25AB8 D>=>2K5 :;5B:8

      for(int j=0;j<9;j++)

      {

         int xi=x0-di*(8-i);

         int yi=y0+di*(8-j);

         out_Rect(i,j,xi,yi,di+2,clr_Fon,"f");

      }

   if (x_take>=0 && y_take>=0)

      ObjectSetInteger(0,pref+"f"+IntegerToString(x_take)+IntegerToString(y_take),OBJPROP_BGCOLOR,clr_Take);

   //.....................

   LabelCreate(0,pref+"t",0,x0+4*di/5,y0,IntegerToString(sum),"Arial Black",di/3,clr_Text,0,ANCHOR_RIGHT_LOWER);

   

   if (!GlobalVariableCheck(name_glob))GlobalVariableSet(name_glob,0);

   int max_sum=(int)GlobalVariableGet(name_glob);

   if (sum>max_sum)GlobalVariableSet(name_glob,sum);

   LabelCreate(0,pref+"x",0,x0-8*di+di/2,y0,IntegerToString(max_sum),"Arial Black",di/3,clrRed,0,ANCHOR_LEFT_LOWER);

   //.....................

   ButtonCreate(but_Load,x0-5*di-di/5,y0-5*di/8,(int)(di*1.3),di/2,"Load",di/4);

   ButtonCreate(but_Save,x0-3*di+di/7,y0-5*di/8,(int)(di*1.3),di/2,"Save",di/4,clrMagenta);

   if (contr_sum_save==contr_sum)

      ObjectSetInteger(0,but_Save,OBJPROP_BGCOLOR,clrGray);

   else

      ObjectSetInteger(0,but_Save,OBJPROP_BGCOLOR,clrMagenta);

   ButtonCreate(pref+"p",(int)(x0-3.6*di),(int)(y0-5.0*di/8),(int)(di/2),di/2,IntegerToString(time_sec),di/4,clrAqua);

}

//----------------------

void find_krd_panel(int& x0,int& y0,int& di)

{

   double max=WindowPriceMax();

   double min=WindowPriceMin();

   int x_max,y_max;

   int x_min,y_min;

   ChartTimePriceToXY(0,0,Time[0],max,x_max,y_max);

   ChartTimePriceToXY(0,0,Time[0],min,x_min,y_min);



   x0=600;

   y0=y_max+40;

   int d=y_min-y0-15;

   di=(int)MathFloor(1.0*d/9.0);

}

//+------------------------------------------------------------------+

void out_Rect(int i,int j,int x, int y,int width,color clr_fon,string s,

               long             z_order=1,                // ?@8>@8B5B =0 =060B85 <KHLN

               ENUM_BORDER_TYPE border=BORDER_FLAT,       // B8? 3@0=8FK

               ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // C3>; 3@0D8:0 4;O ?@82O7:8

               color            clr=clrBlue,              // F25B ?;>A:>9 3@0=8FK (Flat)

               ENUM_LINE_STYLE  style=STYLE_SOLID,        // AB8;L ?;>A:>9 3@0=8FK

               int              line_width=2,             // B>;I8=0 ?;>A:>9 3@0=8FK

               bool             back=false,               // =0 704=5< ?;0=5

               bool             selection=false,          // 2K45;8BL 4;O ?5@5<5I5=89

               bool             hidden=true               // A:@KB 2 A?8A:5 >1J5:B>2

             )

{



   int height=width;

   string name=pref+StringConcatenate(s,i,j);

   if (ObjectFind(0,name)<0)

   {

      ObjectCreate(0,name,OBJ_RECTANGLE_LABEL,0,0,0);

      //--- CAB0=>28< B8? 3@0=8FK

      ObjectSetInteger(0,name,OBJPROP_BORDER_TYPE,border);

         //--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

      ObjectSetInteger(0,name,OBJPROP_CORNER,corner);

         //--- CAB0=>28< F25B ?;>A:>9 @0<:8 (2 @568<5 Flat)

      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);

         //--- CAB0=>28< AB8;L ;8=88 ?;>A:>9 @0<:8

      ObjectSetInteger(0,name,OBJPROP_STYLE,style);

         //--- CAB0=>28< B>;I8=C ?;>A:>9 3@0=8FK

      ObjectSetInteger(0,name,OBJPROP_WIDTH,line_width);

         //--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< ?5@5<5I5=8O <5B:8 <KHLN

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,selection);

      ObjectSetInteger(0,name,OBJPROP_SELECTED,selection);

         //--- A:@>5< (true) 8;8 >B>1@078< (false) 8<O 3@0D8G5A:>3> >1J5:B0 2 A?8A:5 >1J5:B>2

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,hidden);

         //--- CAB0=>28< ?@8>@8B5B =0 ?>;CG5=85 A>1KB8O =060B8O <KH8 =0 3@0D8:5

      ObjectSetInteger(0,name,OBJPROP_ZORDER,z_order);   

   }

         //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

      ObjectSetInteger(0,name,OBJPROP_BACK,back);

   

   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);

      //--- CAB0=>28< @07<5@K <5B:8

   ObjectSetInteger(0,name,OBJPROP_XSIZE,width);

   ObjectSetInteger(0,name,OBJPROP_YSIZE,height);

      //--- CAB0=>28< F25B D>=0

   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clr_fon);

}

//-------------------------------

void out_Rect1(int i,int j,int x, int y,int width,int height,color clr_fon,string s,

               long             z_order=1,                // ?@8>@8B5B =0 =060B85 <KHLN

               ENUM_BORDER_TYPE border=BORDER_FLAT,       // B8? 3@0=8FK

               ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // C3>; 3@0D8:0 4;O ?@82O7:8

               color            clr=clrBlue,              // F25B ?;>A:>9 3@0=8FK (Flat)

               ENUM_LINE_STYLE  style=STYLE_SOLID,        // AB8;L ?;>A:>9 3@0=8FK

               int              line_width=2,             // B>;I8=0 ?;>A:>9 3@0=8FK

               bool             back=false,               // =0 704=5< ?;0=5

               bool             selection=false,          // 2K45;8BL 4;O ?5@5<5I5=89

               bool             hidden=true               // A:@KB 2 A?8A:5 >1J5:B>2

             )

{



   string name=pref+StringConcatenate(s,i,j);

   if (ObjectFind(0,name)<0)

   {

      ObjectCreate(0,name,OBJ_RECTANGLE_LABEL,0,0,0);

      //--- CAB0=>28< B8? 3@0=8FK

      ObjectSetInteger(0,name,OBJPROP_BORDER_TYPE,border);

         //--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

      ObjectSetInteger(0,name,OBJPROP_CORNER,corner);

         //--- CAB0=>28< F25B ?;>A:>9 @0<:8 (2 @568<5 Flat)

      ObjectSetInteger(0,name,OBJPROP_COLOR,clr);

         //--- CAB0=>28< AB8;L ;8=88 ?;>A:>9 @0<:8

      ObjectSetInteger(0,name,OBJPROP_STYLE,style);

         //--- CAB0=>28< B>;I8=C ?;>A:>9 3@0=8FK

      ObjectSetInteger(0,name,OBJPROP_WIDTH,line_width);

         //--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< ?5@5<5I5=8O <5B:8 <KHLN

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,selection);

      ObjectSetInteger(0,name,OBJPROP_SELECTED,selection);

         //--- A:@>5< (true) 8;8 >B>1@078< (false) 8<O 3@0D8G5A:>3> >1J5:B0 2 A?8A:5 >1J5:B>2

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,hidden);

         //--- CAB0=>28< ?@8>@8B5B =0 ?>;CG5=85 A>1KB8O =060B8O <KH8 =0 3@0D8:5

      ObjectSetInteger(0,name,OBJPROP_ZORDER,z_order);   

   }

         //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

      ObjectSetInteger(0,name,OBJPROP_BACK,back);

   

   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);

      //--- CAB0=>28< @07<5@K <5B:8

   ObjectSetInteger(0,name,OBJPROP_XSIZE,width);

   ObjectSetInteger(0,name,OBJPROP_YSIZE,height);

      //--- CAB0=>28< F25B D>=0

   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clr_fon);

}

//-------------------------------

bool LabelCreate(const long              chart_ID=0,               // ID 3@0D8:0

                 const string            name="Label",             // 8<O <5B:8

                 const int               sub_window=0,             // =><5@ ?>4>:=0

                 const int               x=0,                      // :>>@48=0B0 ?> >A8 X

                 const int               y=0,                      // :>>@48=0B0 ?> >A8 Y

                 const string            text="Label",             // B5:AB

                 const string            font="Arial",             // H@8DB

                 const int               font_size=10,             // @07<5@ H@8DB0

                 const color             clr=clrRed,               // F25B

                 const double            angle=0.0,                // =0:;>= B5:AB0

                 const ENUM_ANCHOR_POINT anchor=ANCHOR_CENTER, // A?>A>1 ?@82O7:8

                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // C3>; 3@0D8:0 4;O ?@82O7:8

                 const bool              back=false,               // =0 704=5< ?;0=5

                 const bool              selection=false,          // 2K45;8BL 4;O ?5@5<5I5=89

                 const bool              hidden=true,              // A:@KB 2 A?8A:5 >1J5:B>2

                 const long              z_order=0)                // ?@8>@8B5B =0 =060B85 <KHLN

  {

//--- A1@>A8< 7=0G5=85 >H81:8

   ResetLastError();

//--- A>74048< B5:AB>2CN <5B:C

   if (ObjectFind(chart_ID,name)<0)ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0);

//--- CAB0=>28< :>>@48=0BK <5B:8

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

//--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//--- CAB0=>28< B5:AB

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- CAB0=>28< H@8DB B5:AB0

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//--- CAB0=>28< @07<5@ H@8DB0

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//--- CAB0=>28< C3>; =0:;>=0 B5:AB0

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- CAB0=>28< A?>A>1 ?@82O7:8

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- CAB0=>28< F25B

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< ?5@5<5I5=8O <5B:8 <KHLN

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- A:@>5< (true) 8;8 >B>1@078< (false) 8<O 3@0D8G5A:>3> >1J5:B0 2 A?8A:5 >1J5:B>2

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- CAB0=>28< ?@8>@8B5B =0 ?>;CG5=85 A>1KB8O =060B8O <KH8 =0 3@0D8:5

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- CA?5H=>5 2K?>;=5=85

   return(true);

  }

//+------------------------------------------------------------------+

bool ButtonCreate(const string  name="button",      // 8<O :=>?:8

                  const int x=0,                    // :>>@48=0B0 ?> >A8 X

                  const int y=0,                    // :>>@48=0B0 ?> >A8 Y

                  const int width=0,                // H8@8=0 :=>?:8

                  const int height=0,               // 2KA>B0 :=>?:8

                  const string text="",             // B5:AB

                  const int font_size=10,            // @07<5@ H@8DB0

                  const color  back_clr=clrYellow,    // F25B D>=0 

                  const string  font="Arial Black",     // H@8DB

                  const bool back=false,            // =0 704=5< ?;0=5

                  const long z_order=1,             // ?@8>@8B5B =0 =060B85 <KHLN

                  const color clr=clrBlack,          // F25B B5:AB0

                  const bool state=false,           // =060B0/>B60B0                                                  

                  const color border_clr=clrBlue,   // F25B 3@0=8FK  

                  //------------------------------------------------               

                  const bool selection=false,       // 2K45;8BL 4;O ?5@5<5I5=89

                  const bool hidden=false)          // A:@KB 2 A?8A:5 >1J5:B>2

  {

//--- A1@>A8< 7=0G5=85 >H81:8

   ResetLastError();



   if(ObjectFind(name)==-1)

     {

      //--- A>74048< :=>?:C

      ObjectCreate(0,name,OBJ_BUTTON,0,0,0);

      //--- CAB0=>28< C3>; 3@0D8:0, >B=>A8B5;L=> :>B>@>3> 1C4CB >?@545;OBLAO :>>@48=0BK B>G:8

      ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_LEFT_UPPER);

      //--- A:@>5< (true) 8;8 >B>1@078< (false) 8<O 3@0D8G5A:>3> >1J5:B0 2 A?8A:5 >1J5:B>2

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,hidden);

      //--- CAB0=>28< ?@8>@8B5B =0 ?>;CG5=85 A>1KB8O =060B8O <KH8 =0 3@0D8:5

      ObjectSetInteger(0,name,OBJPROP_ZORDER,z_order);

     }

   //--- CAB0=>28< F25B B5:AB0

   ObjectSetInteger(0,name,OBJPROP_COLOR,clr);

   //--- CAB0=>28< @07<5@ H@8DB0

   ObjectSetInteger(0,name,OBJPROP_FONTSIZE,font_size);

   //--- CAB0=>28< H@8DB B5:AB0

   ObjectSetString(0,name,OBJPROP_FONT,font);

   //--- >B>1@078< =0 ?5@54=5< (false) 8;8 704=5< (true) ?;0=5

   ObjectSetInteger(0,name,OBJPROP_BACK,back);

   //--- CAB0=>28< F25B 3@0=8FK

   ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,border_clr);

      //--- CAB0=>28< F25B D>=0

   ObjectSetInteger(0,name,OBJPROP_BGCOLOR,back_clr);

   ObjectSetInteger(0,name,OBJPROP_COLOR,clr);

   

   ObjectSetInteger(0,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(0,name,OBJPROP_YDISTANCE,y);

   //--- CAB0=>28< @07<5@ :=>?:8

   ObjectSetInteger(0,name,OBJPROP_XSIZE,width);

   ObjectSetInteger(0,name,OBJPROP_YSIZE,height);

   ObjectSetString(0,name,OBJPROP_TEXT,text);

   ObjectSetInteger(0,name,OBJPROP_STATE,state);

      //--- 2:;NG8< (true) 8;8 >B:;NG8< (false) @568< ?5@5<5I5=8O :=>?:8 <KHLN

   ObjectSetInteger(0,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(0,name,OBJPROP_SELECTED,selection);

//--- CA?5H=>5 2K?>;=5=85

   return(true);

  }

//+------------------------------------------------------------------+

void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)

{     // ?@>F54C@0 2K7K205BAO 2> 2@5<O :.-;. 459AB28O >?5@0B>@0

   ResetLastError();//--- A1@>A8< 7=0G5=85 >H81:8

            //--- ?@>25@:0 A>1KB8O =060B8O :=>?:8 <KH8 =0 >1J5:B5

   if(id==CHARTEVENT_OBJECT_CLICK)

   {

      if(StringSubstr(sparam,0,5)==pref+"f")  // 2K1@0BL H0@

      {

         int i=StrToInteger(StringSubstr(sparam,5,1));

         int j=StrToInteger(StringSubstr(sparam,6,1));

         if (i>=0 && i<9 && j>=0 && j<9)

         {

            if (mas[i][j]>0 && mas[i][j]<6)  // =060B H0@

            {

               if (X_Take>=0 && Y_Take>=0)

                  ObjectSetInteger(0,pref+"f"+IntegerToString(X_Take)+IntegerToString(Y_Take),OBJPROP_BGCOLOR,clr_Fon);

               X_Take=i;

               Y_Take=j;

               select_ball(i,j);

               ObjectSetInteger(0,pref+"f"+IntegerToString(i)+IntegerToString(j),OBJPROP_BGCOLOR,clr_Take);

               return;

            }

            //..................................

            if (mas[i][j]==0 && X_Take>=0 && Y_Take>=0) // =060B> ?CAB>5 ?>;5

            {

               int mas_temp[9][9];

               ArrayCopy(mas_temp,mas);

               kol_way=0;

               bool find=find_way(mas_temp,X_Take,Y_Take,i,j);

               if (!find)

               {

                  if (sound)PlaySound("wait.wav");

                  return;

               }

               int n_clr=mas[X_Take][Y_Take];

               del_ball(X_Take,Y_Take);

               mas[X_Take][Y_Take]=0;

               out_way(X_Take,Y_Take,n_clr);

               //..................................

               mas[i][j]=n_clr;



               out_ball(i,j);

               

               out_next();

               

               return;

            }

            //.......................

         }

         return;

      }

      if(sparam==but_Load) 

      {

         Load_file();

         return;

      }

      if(sparam==but_Save) 

      {

         Save_file();

         return;

      }

      if(sparam==pref+"p") // ?0C70

      {

         pause=!pause;

         if (pause)

         {

            ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrGray);

            ObjectsDeleteAll(0,pref+"s");

            ObjectsDeleteAll(0,pref+"f");

            

            out_Rect(9,9,x0_-8*di_,y0_-di_,9*di_,clrBlack,"p",0,BORDER_FLAT,CORNER_LEFT_UPPER,clrNONE,STYLE_SOLID,2,false);

            for(int i=0;i<9;i++) // 2K25AB8 D>=>2K5 :;5B:8

               for(int j=0;j<9;j++)

               {

                  int xi=x0_-di_*(8-i);

                  int yi=y0_+di_*(8-j);

                  out_Rect(i,j,xi,yi,di_+2,clr_Fon,"f");

               }

            EventKillTimer();

         }

         else

         {

            ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrAqua);

            EventSetMillisecondTimer(1000);

         }

         return;

      }

   }

}

//------------------------

void out_next()

{

   int del_1=del_line_ball();

   //.......................

   out_box();

   WindowRedraw();

   ObjectSetInteger(0,pref+"f"+IntegerToString(X_Take)+IntegerToString(Y_Take),OBJPROP_BGCOLOR,clr_Fon);

   X_Take=-1;

   Y_Take=-1;

   int del_2=del_line_ball();

   if (del_1+del_2==0)

   {

      bool ust_1=false,ust_2=false,ust_3=false;

      if (mas[mas_next[0][0]][mas_next[0][1]]==0)

      {

         mas[mas_next[0][0]][mas_next[0][1]]=mas_next[0][2];

         out_ball(mas_next[0][0],mas_next[0][1]);

         ust_1=true;

      }

      

      if (mas[mas_next[1][0]][mas_next[1][1]]==0)

      {

         mas[mas_next[1][0]][mas_next[1][1]]=mas_next[1][2];

         out_ball(mas_next[1][0],mas_next[1][1]);

         ust_2=true;

      }

      if (mas[mas_next[2][0]][mas_next[2][1]]==0)

      {

         mas[mas_next[2][0]][mas_next[2][1]]=mas_next[2][2];

         out_ball(mas_next[2][0],mas_next[2][1]);

         ust_3=true;

      }

      if (!ust_1)ust_ball(mas_next[0][2]);

      if (!ust_2)ust_ball(mas_next[1][2]);

      if (!ust_3)ust_ball(mas_next[2][2]);

      

      int del_3=del_line_ball();

      if (del_3>=Rang)del_3=Rang+(del_3-Rang+3)*(del_3-Rang)/2;

      sum+=del_3;

      

      ust_hole();

      ust_3_ball_next();

   }

   else

   {

      int sum1=del_1+del_2;

      if (sum1>=Rang)sum1=Rang+(sum1-Rang+3)*(sum1-Rang)/2;

      sum+=sum1;

   }

   time_sec=max_time+1;

}

//-------------------------------

void select_ball(int i,int j)

{

   int dd=di_/6;

   int d=di_;

   for(int k=0;k<3;k++)

   {

      d-=dd;

      set_ball(i,j,mas[i][j],d);

      Sleep(20);

      WindowRedraw();

   }

//   d=0;

   for(int k=0;k<3;k++)

   {

      d+=dd;

      set_ball(i,j,mas[i][j],d);

      Sleep(20);

      WindowRedraw();

   }

}

//----------------------

void del_ball(int i,int j,int sl=10)

{

   int dd=di_/6;

   int d=di_;

   for(int k=0;k<6;k++)

   {

      d-=dd;

      set_ball(i,j,mas[i][j],d);

      Sleep(sl);

      WindowRedraw();

   }

   mas[i][j]=0;

   ObjectDelete(pref+"s"+IntegerToString(i)+IntegerToString(j));

   WindowRedraw();

}

//----------------------

int del_line_ball()

{//        0;3>@8B< >1=0@C65=8O ;N1>9 D83C@K A> A:>?;5=85< 5 8 1>;55 H0@>2 >4=>3> F25B0

// 1. 15@Q< >G5@54=CN :;5B:C x1,y1

// 2. :>;_>48=0:=0

// 3. A>740Q< <0AA82 c ?@87=0:o< ":;5B:0 ?@>A<>B@5=0"(=0)

// 4. 70?8AK205< 2 <0AA82 C40;5=8O :>>@4.  x1,y1, 8 2 <0AA825  2 MB>< <5AB5 AB028< 1

//    >G8I05< <0AA82 C40;5=8O

// 5. 2E>48< 2 ?@>F54C@C "?>8A: @O4><"(x1,y1,F25B)

//             @>F54C@0 "?>8A: @O4><"(x_inp,y_inp,2E_F25B)

// 6.    ?@>A<0B@8205< 2A5 8 :;5B>: 2>:@C3 2E>4=>9: x_inp,y_inp

// 7.    5A;8 C :;5B:8 x2,y2 =1 8;8 F25B =5 @025= 2E_F25B, 845< : A;54CNI59 :;5B:5

// 8.    5A;8 F25B x2,y2 A>2?0405B 8 =0, B> :>;_>48=0:++, =1, 70?8AK205< 2 <0AA82 C40;5=8O x2,y2

// 9.    8 70E>48< 2 ?@>F54C@C "?>8A: @O4><"(x2,y2,F25B), 8=0G5 ?.6

// 9.    5A;8 ?@>H;8 2A5 8 :;5B>:, 2KE>4 87 ?@>F54C@K

// 10.5A;8 :>;_>48=0:>=5, C40;O5< H0@K A :;5B>:, 70?8A0==KE 2 <0AA825 C40;5=8O, 2KE>4 87 ?@>A<>B@0

// 11.5A;8 :>;_>48=0:<5, B> ?.1 

   bool mas_kp[9][9];

   

   int mas_del[][2]; // 0-x,1-y

   int kol_del=0;

   bool br=false;

   

   for(int j=0;j<9;j++)

   {

      for(int i=0;i<9;i++)

      {

         int n_clr=mas[i][j];

         if (n_clr>0 && n_clr<=5)

         {

            ArrayInitialize(mas_kp,false);

            mas_kp[i][j]=true;

            

            kol_del=1;

            ArrayResize(mas_del,kol_del);

            mas_del[kol_del-1][0]=i;

            mas_del[kol_del-1][1]=j;

            find_near(i,j,n_clr,mas_kp,mas_del,kol_del); // =09B8 @O4>< (@5:C@A8O)

            if (kol_del>=Rang)

            {

               br=true;

               break;

            }

         }

      }

      if (br)break;

   }

   //..................................

   if (kol_del>=Rang)// C40;5=85 H0@>2 ?> :>>@4. <0AA820 mas_del

   {

      for(int i=0;i<kol_del;i++)

      del_ball(mas_del[i][0],mas_del[i][1],20);

   }

   else

      kol_del=0;

   //.......................................

   return(kol_del);

}

//--------------------------

void find_near(int x_inp,int y_inp,int n_clr,bool& mas_kp[9][9],int& mas_del[][2],int& kol_del)

{

   for(int k=-1;k<=1;k++)

      for(int p=-1;p<=1;p++)

      {

         int i=x_inp+k;

         int j=y_inp+p;

         if (i>=0 && i<9 && j>=0 && j<9)

            if (!mas_kp[i][j] && mas[i][j]==n_clr)calc_bal(i,j,n_clr,mas_kp,mas_del,kol_del);

      }

}

//---------------------------------------

void calc_bal(int i,int j,int n_clr,bool& mas_kp[9][9],int& mas_del[][2],int& kol_del)

{

   mas_kp[i][j]=true;

   

   kol_del++;

   ArrayResize(mas_del,kol_del);

   mas_del[kol_del-1][0]=i;

   mas_del[kol_del-1][1]=j;

   find_near(i,j,n_clr,mas_kp,mas_del,kol_del);

}

//------------------------- 

bool find_way(int& mas_[9][9],int inp_x,int inp_y,int out_x,int out_y)

{  // inp_x,inp_y - :;5B:0 A H0@><; out_x,out_y - ?CAB0O :;5B:0

   if (inp_x==out_x && inp_y==out_y)return(true); // ?@8H;8

   //...........................

   bool out=false;

   int x,y;

   for(int i=0;i<4;i++) // ?5@518@05< =0?@02;5=8O

      switch(i)

      {

         case 0 :    // 225@E

            y=inp_y;

            y++;

            if (y>8)break; // 25@E=89 :@09

            if (mas_[inp_x][y]>0)break; // C65 B0< 1K;8 8;8 70=OB> H0@><

            mas_[inp_x][y]=9;// :;5B:0 ?CAB0O,70?8H5< ?@87=0:, GB> <K BC40 ?>945<

            out=find_way(mas_,inp_x,y,out_x, out_y); // CE>48< =0 =>2CN :;5B:C

            if (out)out_temp_rect(inp_x,y,up);

            if (out)return(true);

            break;

         case 1 :    // 2;52>

            x=inp_x;

            x--;

            if (x<0)break; // ;52K9 :@09

            if (mas_[x][inp_y]>0)break; // C65 B0< 1K;8 8;8 70=OB> H0@><

            mas_[x][inp_y]=9;// :;5B:0 ?CAB0O,70?8H5< ?@87=0:, GB> <K BC40 ?>945<

            out=find_way(mas_,x,inp_y,out_x, out_y); // CE>48< =0 =>2CN :;5B:C

            if (out)out_temp_rect(x,inp_y,lt);

            if (out)return(true);

            break;

         case 2 :    // 2=87

            y=inp_y;

            y--;

            if (y<0)break; // =86=89 :@09

            if (mas_[inp_x][y]>0)break; // C65 B0< 1K;8 8;8 70=OB> H0@><

            mas_[inp_x][y]=9;// :;5B:0 ?CAB0O,70?8H5< ?@87=0:, GB> <K BC40 ?>945<

            out=find_way(mas_,inp_x,y,out_x, out_y); // CE>48< =0 =>2CN :;5B:C

            if (out)out_temp_rect(inp_x,y,dw);

            if (out)return(true);

            break;

         case 3 :    // 2?@02>

            x=inp_x;

            x++;

            if (x>8)break; // ?@02K9 :@09

            if (mas_[x][inp_y]>0)break; // C65 B0< 1K;8 8;8 70=OB> H0@><

            mas_[x][inp_y]=9;// :;5B:0 ?CAB0O,70?8H5< ?@87=0:, GB> <K BC40 ?>945<

            out=find_way(mas_,x,inp_y,out_x, out_y); // CE>48< =0 =>2CN :;5B:C

            if (out)out_temp_rect(x,inp_y,rt);

            if (out)return(true);

            break;

      }

   return(out);

}

//-----------------------------------

void out_temp_rect(int i,int j,type_way way)

{

   kol_way++;

   ArrayResize(mas_way,kol_way,10);

   mas_way[kol_way-1][0]=i;

   mas_way[kol_way-1][1]=j;

}

//-----------------------------

void out_way(int x_take,int y_take,int n_clr)

{

   color clr=0;

   switch(n_clr)

   {

      case 1 : clr=clr_1;break;

      case 2 : clr=clr_2;break;

      case 3 : clr=clr_3;break;

      case 4 : clr=clr_4;break;

      case 5 : clr=clr_5;break;

      case 6 : clr=clr_hole;break;

   }

   for(int k=kol_way-1;k>=0;k--)

   {

      int i=mas_way[k][0];

      int j=mas_way[k][1];

      int x=x0_-di_*(8-i);

      int y=y0_+di_*(8-j);

   

      LabelCreate(0,pref+"w",0,x+di_/2,(int)MathCeil(y+di_/2+di_*0.03),"l","wingdings",(int)MathCeil(di_*0.7),clr);

      Sleep(10);

      ChartRedraw();

   }

   ObjectDelete(pref+"w");

}

//---------------------------------

void Save_file()

{

   int n=0;

   int filehandle=FileOpen(name_file,FILE_WRITE|FILE_BIN);

   for(int i=0;i<9;i++)

      for(int j=0;j<9;j++)

      {

         n=0;

         if (mas[i][j]>0)

            n=mas[i][j];

         else

            for(int k=0;k<3;k++)

               if (mas_next[k][0]==i && mas_next[k][1]==j)

               {

                  n=-mas_next[k][2];

                  break;

               }

         FileWriteInteger(filehandle,n);

      }

   FileWriteInteger(filehandle,sum);

   FileWriteInteger(filehandle,X_Take);

   FileWriteInteger(filehandle,Y_Take);

   

   FileWriteInteger(filehandle,kol_hole);

   for(int i=0;i<kol_hole;i++)

   {

      FileWriteInteger(filehandle,mas_hole[i][0]);

      FileWriteInteger(filehandle,mas_hole[i][1]);

   }

   FileWriteInteger(filehandle,contr_sum);

   contr_sum_save=contr_sum;

   //....................................



   FileClose(filehandle);

}

//-------------------------

int Load_file(bool put=true)

{

   int filehandle=FileOpen(name_file,FILE_READ|FILE_BIN);

   if (filehandle==INVALID_HANDLE)return(0);

   

   ArrayInitialize(mas,0);

   ArrayInitialize(mas_next,0);

   ObjectsDeleteAll(0,pref+"s");

   

   int k=0;

   int n=0;

   for(int i=0;i<9;i++)

      for(int j=0;j<9;j++)

      {

         n=FileReadInteger(filehandle);

         if (put)

            if (n>0)

               mas[i][j]=n;

            else

               if (n<0)

               {

                  if (k<3)

                  {

                     mas_next[k][0]= i;

                     mas_next[k][1]= j;

                     mas_next[k][2]=-n;

                  }

                  k++;

               }

      }

   if (put)

   {

      sum=FileReadInteger(filehandle);

      X_Take=FileReadInteger(filehandle);

      Y_Take=FileReadInteger(filehandle);

      //........................

      kol_hole=FileReadInteger(filehandle);

      ArrayResize(mas_hole,kol_hole);

      for(int i=0;i<kol_hole;i++)

      {

         mas_hole[i][0]=FileReadInteger(filehandle);

         mas_hole[i][1]=FileReadInteger(filehandle);

      }

   }

   else

   {

      FileReadInteger(filehandle);

      FileReadInteger(filehandle);

      FileReadInteger(filehandle);

   }

   int contr_sum_1=FileReadInteger(filehandle);

   //........................

   FileClose(filehandle);

   //.....................

   if (put) 

   {

      contr_sum_save=contr_sum_1;

      //...........................

      out_box();

      if (X_Take>=0 && Y_Take>=0)

         ObjectSetInteger(0,pref+"f"+IntegerToString(X_Take)+IntegerToString(Y_Take),OBJPROP_BGCOLOR,clr_Fon);

      

      out_ball_next(mas_next[0][0],mas_next[0][1],0);

      out_ball_next(mas_next[1][0],mas_next[1][1],1);

      out_ball_next(mas_next[2][0],mas_next[2][1],2);

      WindowRedraw();

   }

   return(contr_sum_1);

}

//-------------------------

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---