100 prisoners game

Author: © 2022, Alexey Viktorov
Miscellaneous
It opens Message Boxes to the userIt issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
100 prisoners game
ÿþ/********************************************************************\

|                                             100 prisoners game.mq5 |

                                           One hundred prisoners game

|                                            © 2022, Alexey Viktorov |

|                       https://www.mql5.com/ru/users/alexeyvik/news |

\********************************************************************/

#property copyright "© 2022, Alexey Viktorov"

#property link      "https://www.mql5.com/ru/users/alexeyvik/news"

#property version   "1.00"

#define zz " ***** "

sinput  color   butColor    =   clrLightGray;     //  &25B :=>?>:

sinput  color   textColor   =   clrDarkSlateGray; //  &25B B5:AB0 =0 :=>?:0E

//sinput  string  TplName     =   "";               //  <O H01;>=0

int ZK[100];

int box[100];

long zk = 0;

bool nzk = true,

     newGame = true;

string  tplName;

long chartID;

/*******************Expert initialization function*******************/

int OnInit()

 {

  chartID = ChartID();

//---

//ChartSaveTemplate(chartID, "game");

  tplName = "Game_100.tpl";//TplName != "" ? TplName : "default";

  ushort Bytes[];

  string chName, newName;

  if(ChartSaveTemplate(chartID, "\\Files\\"+tplName) && FileLoad(tplName, Bytes))

    chName = ShortArrayToString(Bytes);

  int pozNumb = StringFind(chName, "<expert>");

  int pozEnd = StringFind(chName, "</expert>", pozNumb+8);

  ArrayRemove(Bytes, pozNumb, pozEnd-pozNumb+9);

  newName = ShortArrayToString(Bytes);

  string newPatsh = StringSubstr(chName,pozNumb);

  FileSave(tplName, Bytes);

//---

  ChartSetInteger(chartID, CHART_MODE, CHART_LINE);

  ChartSetInteger(chartID, CHART_COLOR_CHART_LINE, clrNONE);

  ChartSetInteger(chartID, CHART_SHOW_BID_LINE, false);

  ChartSetInteger(chartID, CHART_SHOW_ASK_LINE, false);

  ChartSetInteger(chartID, CHART_SHOW_LAST_LINE, false);

  ChartSetInteger(chartID, CHART_SHOW_PERIOD_SEP, false);

  long windTotal = ChartGetInteger(chartID, CHART_WINDOWS_TOTAL, 0);

  for(int i = (int)windTotal; i-- > 0;)

   {

    int indTotal = ChartIndicatorsTotal(chartID, i);

    for(int j = indTotal; j-- >0;)

     {

      string indName = ChartIndicatorName(chartID, i, j);

      if(!ChartIndicatorDelete(chartID, i, indName))

        Print(zz, GetLastError(), zz, indName, zz, "E@5= B515");

     }

   }

  NewGame();

  buttonCreate("New ", 105, 30, 355, 35);

  buttonCreate("New Game", 105, 30, 520, 35);

  buttonCreate("End Game", 105, 30, 685, 35);

  return(INIT_SUCCEEDED);

 }/******************************************************************/



/************************Expert tick function************************/

void OnTick()

 {

 }/******************************************************************/



/***********************OnChartEvent function************************/

void OnChartEvent(const int id,         // 845=B8D8:0B>@ A>1KB8O

                  const long& lparam,   // ?0@0<5B@ A>1KB8O B8?0 long

                  const double& dparam, // ?0@0<5B@ A>1KB8O B8?0 double

                  const string& sparam  // ?0@0<5B@ A>1KB8O B8?0 string

                 )

 {

  static int cnt = 0;

  if(id == CHARTEVENT_OBJECT_CLICK)

   {

    if(sparam == "End Game")

      ChartApplyTemplate(chartID, "\\Files\\"+tplName);

    if(sparam == "New Game")

     {

      ObjectSetInteger(chartID, sparam, OBJPROP_STATE, false);

      NewGame();

      newGame = true;

      nzk = true;

      cnt = 0;

     }

    if(sparam == "New ")

     {

      if(!newGame)

       {

        MessageBox("0G=8B5 =>2CN 83@C");

        return;

       }

      nzk = true;

      ObjectSetInteger(chartID, sparam, OBJPROP_STATE, false);

      for(int i = 0; i < ObjectsTotal(chartID); i++)

       {

        string butName = ObjectName(chartID, i);

        if(StringSubstr(butName, 0, 2) == "b ")

         {

          ObjectSetInteger(chartID, butName, OBJPROP_BGCOLOR, butColor);

          ObjectSetInteger(chartID, butName, OBJPROP_STATE, false);

          ObjectSetString(chartID, butName, OBJPROP_TEXT, butName);

         }

       }

     }

    if(StringFind(sparam, " ") == 0)

     {

      if(!nzk)

       {

        color bgc = (color)ObjectGetInteger(chartID, sparam, OBJPROP_BGCOLOR);

        if(bgc == clrKhaki || bgc == clrMediumTurquoise)

         {

          ObjectSetInteger(chartID, sparam, OBJPROP_STATE, true);

          ObjectSetInteger(chartID, sparam, OBJPROP_BGCOLOR, bgc);

          ChartRedraw();

          return;

         }

        if(ObjectGetInteger(chartID, sparam, OBJPROP_STATE))

          ObjectSetInteger(chartID, sparam, OBJPROP_STATE, false);

        ChartRedraw();

        return;

       }

      zk = StringToInteger(StringSubstr(sparam, 3));

      ObjectSetInteger(chartID, sparam, OBJPROP_BGCOLOR, clrKhaki);

      ObjectSetInteger(chartID, sparam, OBJPROP_STATE, true);

      ChartRedraw();

      nzk = false;

     }

    if(StringFind(sparam, "b ") == 0)

     {

      if(!newGame)

       {

        ObjectSetInteger(chartID, sparam, OBJPROP_STATE, false);

        return;

       }

      cnt++;

      long numb = StringToInteger(StringSubstr(sparam, 2));

      ObjectSetString(chartID, sparam, OBJPROP_TEXT, (string)box[numb-1]);

      ObjectSetInteger(chartID, sparam, OBJPROP_STATE, true);

      ObjectSetInteger(chartID, sparam, OBJPROP_BGCOLOR, clrKhaki);

      Comment(" >?KB:0 =><5@   ", cnt);

      if(zk == box[numb-1])

       {

        //Print(zz, zk, " = ", box[numb-1], " A53> ?>?KB>: ", cnt);

        Alert(zz, zk, " = ", box[numb-1], " A53> ?>?KB>: ", cnt);

        ObjectSetInteger(chartID, " "+(string)zk, OBJPROP_BGCOLOR, clrMediumTurquoise);

        cnt = 0;

       }

      if(cnt == 50)

       {

        int index = 0;

        while(box[index] != zk && !IsStopped())

         {

          index++;

         }

        ObjectSetString(chartID, "b "+(string)index, OBJPROP_TEXT, (string)box[index]);

        ObjectSetInteger(chartID, "b "+(string)index, OBJPROP_BGCOLOR, clrMediumOrchid);

        ChartRedraw();

        MessageBox("025B=K9 =><5@ 2 :>@>1:5 ! "+(string)index, "07=8BL, =5;L7O ?><8;>20BL");

        Print(zz, index, zz, box[index]);

        newGame = false;

        nzk = false;

       }

     }

   }

 }/******************************************************************/



/******************Expert deinitialization function******************/

void OnDeinit(const int reason)

 {

  ChartApplyTemplate(chartID, "\\Files\\"+tplName);

  Comment("");

 }/******************************************************************/



/****************************buttonCreate****************************/

void buttonCreate(string name, int w, int h, int x, int y)

 {

  if(ObjectFind(chartID, name) < 0)

   {

    if(ObjectCreate(chartID, name, OBJ_BUTTON, 0, 0, 0))

     {

      ObjectSetInteger(chartID, name, OBJPROP_XDISTANCE, x);

      ObjectSetInteger(chartID, name, OBJPROP_YDISTANCE, y);

      ObjectSetInteger(chartID, name, OBJPROP_XSIZE, w);

      ObjectSetInteger(chartID, name, OBJPROP_YSIZE, h);

      ObjectSetString(chartID, name, OBJPROP_TEXT, name);

      ObjectSetInteger(chartID, name, OBJPROP_BGCOLOR, butColor);

      ObjectSetInteger(chartID, name, OBJPROP_COLOR, textColor);

     }

   }

  else

   {

    ObjectSetString(chartID, name, OBJPROP_TEXT, name);

    ObjectSetInteger(chartID, name, OBJPROP_BGCOLOR, butColor);

    ObjectSetInteger(chartID, name, OBJPROP_STATE, false);

   }

  ChartRedraw();

 }/******************************************************************/



/******************************NewGame*******************************/

void NewGame()

 {

  int tempBox[100];

  int y = 25, x = 10;

  ArrayInitialize(box, 0);

  ArrayInitialize(tempBox, 0);

  srand((int)GetMicrosecondCount());

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

   {

    int value = rand()%100+1;

    ArraySort(tempBox);

    int index = ArrayBsearch(tempBox, value);

    if(tempBox[index] == value)

      i--;

    else

     {

      tempBox[0] = value;

      box[i] = value;

      x = 10+(50*(i/25));

      y = i%25 == 0 ? 25 : y+20;

      buttonCreate(" "+string(i+1), 50, 20, x, y);

     }

   }

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

   {

    //x = 300+(55*(i/10));

    //y = i%10 == 0 ? 80 : y+55;

    y = 80+(55*(i/10));

    x = i%10 == 0 ? 300 : x+55;

    buttonCreate("b "+string(i+1), 50, 50, x, y);

   }

 }/******************************************************************/

Comments