title: How to run the functions independently on C++
body: <p>I am trying to make a game on the console using C++. At the moment, I have three functions in the main.</p>
  
  <pre><code>int main() {
  
  ...
  
  while (!level1 &amp;&amp; !player.dead) {
  
      drowing(l1_map);
  
      PlayerMovements(empty, l1_map);
  
      Enemy1Movements(enemy1, l1_map, lastloc);
  
      cls();
  
    }
  
  }
  </code></pre>
  
  <p>The first function draws the map:</p>
  
  <pre><code>void drowing(char l1_map[26][50]) {
  
  
      for (unsigned int i = 0; i &lt; 26; i++) {
  
          cout &lt;&lt; endl;
  
          for (unsigned int x = 0; x &lt; 50; x++) {
  
              if (x == 0) {
                  cout &lt;&lt; " ";
              }
  
  
              //Draws the map and change colours;
              //player
              if (l1_map[i][x] == player.character) {
                  if (l1_map[player.locX][player.locY] == l1_map[enemy1.locX][enemy1.locY]) {
  
                      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 252);
                      cout &lt;&lt; l1_map[i][x];
                  }
  
                  if (l1_map[player.locX][player.locY] != l1_map[enemy1.locX][enemy1.locY]) {
  
                      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 250);
                      cout &lt;&lt; l1_map[i][x];
                  }
  
              }//wall
              else if (l1_map[i][x] == wall) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 128);
                  cout &lt;&lt; l1_map[i][x];
              }//enemy
              else if (l1_map[i][x] == enemy1.character) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 252);
                  cout &lt;&lt; l1_map[i][x];
              }//empty space
              else if (l1_map[i][x] == 32) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 509);
                  cout &lt;&lt; l1_map[i][x];
              }//key1
              else if (l1_map[i][x] == key1.character &amp;&amp; !key1.picked) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 509);
                  cout &lt;&lt; l1_map[i][x];
              }//key2
              else if (l1_map[i][x] == key2.character &amp;&amp; !key1.picked) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
                  cout &lt;&lt; l1_map[i][x];
              }//key3
              else if (l1_map[i][x] == key3.character &amp;&amp; !key1.picked) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
                  cout &lt;&lt; l1_map[i][x];
              }//doors1
              else if (l1_map[i][x] == doors1.character) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
                  cout &lt;&lt; l1_map[i][x];
              }//doors2
              else if (l1_map[i][x] == doors2.character) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
                  cout &lt;&lt; l1_map[i][x];
              }//doors3
              else if (l1_map[i][x] == doors3.character) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
                  cout &lt;&lt; l1_map[i][x];
              }//doors4
              else if (l1_map[i][x] == doors3.character) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
                  cout &lt;&lt; l1_map[i][x];
              }//exit
              else if (l1_map[i][x] == get_out1.character &amp;&amp; !get_out1.open) {
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 139);
                  cout &lt;&lt; l1_map[i][x];
  
              }
              else
              {
  
                  cout &lt;&lt; l1_map[i][x];
                  SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
  
              }
  
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>Second function allows player to move on the map using keyboard arrows:</p>
  
  <pre><code>void PlayerMovements(char&amp; empty, char l1_map[26][50]) {
  
  
  
      char awall = 219;
  
      if (_kbhit()) {
  
  
          switch ((_getch())) { //what button is pressed;
  
  
                                // move up
          case 72:
  
              if (l1_map[player.locX - 1][player.locY] != wall &amp;&amp; l1_map[player.locX - 1][player.locY] != '[') {
  
                  player.locX--;
  
  
                  if (l1_map[player.locX + 1][player.locY] == player.character) {
  
                      l1_map[player.locX + 1][player.locY] = empty;
  
                  }
  
                  if (key3.locX == player.locX &amp;&amp; key3.locY == player.locY) {
  
                      l1_map[key3.locX][key3.locY] = ' ';
  
                  }
  
              }
  
  
  
              break;
  
              //move down
  
          case 80:
  
  
              if (l1_map[player.locX + 1][player.locY] != wall &amp;&amp; l1_map[player.locX + 1][player.locY] != '[') {
  
                  player.locX++;
  
                  if (l1_map[player.locX - 1][player.locY] == player.character) {
  
                      l1_map[player.locX - 1][player.locY] = empty;
  
                  }
  
  
  
              }
  
              break;
  
          case 75:
  
              //left
              if (l1_map[player.locX][player.locY - 1] != wall  &amp;&amp; l1_map[player.locX][player.locY - 1] != '[') {
  
                  player.locY--;
  
  
                  if (l1_map[player.locX][player.locY + 1] == player.character) {
  
  
                      l1_map[player.locX][player.locY + 1] = empty;
  
                  }
  
              }
  
  
              break;
  
  
          case 77: // player moves right
  
              if (l1_map[player.locX][player.locY + 1] != wall &amp;&amp; l1_map[player.locX][player.locY + 1] != '[') {
  
                  player.locY++;
  
                  if (l1_map[player.locX][player.locY - 1] = player.character) {
  
                      l1_map[player.locX][player.locY - 1] = empty;
                  }
  
  
  
              }
  
              break;
          }
  
      }
  
  }
  </code></pre>
  
  <p>Third function is the enemy that moves up and down:</p>
  
  <pre><code>int Enemy1Movements(enemy&amp; enemy1, char l1_map[26][50], char&amp; lastloc) {
  
      //this_thread::sleep_for(chrono::milliseconds(500));
      char empty = ' ';
  
      bool ValidUp = false;
      bool ValidDown = false;
      bool ValidLeft = false;
      bool ValidRight = false;
  
  
  
      if (l1_map[enemy1.locX + 1][enemy1.locY] != wall &amp;&amp; l1_map[enemy1.locX + 1][enemy1.locY] != '-') {
  
          ValidDown = true;
      }
      if (l1_map[enemy1.locX - 1][enemy1.locY] != wall &amp;&amp; l1_map[enemy1.locX - 1][enemy1.locY] != '-') {
          ValidUp = true;
      }
      if (l1_map[enemy1.locX][enemy1.locY - 1] != wall &amp;&amp; l1_map[enemy1.locX - 1][enemy1.locY] != '-') {
          ValidLeft = true;
      }
      if (l1_map[enemy1.locX][enemy1.locY + 1] != wall &amp;&amp; l1_map[enemy1.locX + 1][enemy1.locY] != '-') {
          ValidRight = true;
      }
  
  
      ////enemy move up and down
      //////////////////////////
  
  
      if (lastloc != 'u' &amp;&amp; ValidDown) {
  
  
          enemy1.locX++;
          lastloc = 'd';
  
  
  
  
          if (l1_map[enemy1.locX - 1][enemy1.locY] == enemy1.character) {
  
              l1_map[enemy1.locX - 1][enemy1.locY] = 32;
  
          }
  
      }
      else if (lastloc == 'd' &amp;&amp; ValidUp) {
  
          enemy1.locX--;
          lastloc = 'u';
  
  
          if (l1_map[enemy1.locX + 1][enemy1.locY] == enemy1.character) {
  
              l1_map[enemy1.locX + 1][enemy1.locY] = 32;
  
          }
  
  
      }
      else {
  
          if (ValidUp) {
  
              enemy1.locX--;
              lastloc = 'u';
  
  
              if (l1_map[enemy1.locX + 1][enemy1.locY] == enemy1.character) {
  
                  l1_map[enemy1.locX + 1][enemy1.locY] = 32;
  
              }
  
          }
          else {
  
              enemy1.locX++;
              lastloc = 'd';
  
              if (l1_map[enemy1.locX - 1][enemy1.locY] == enemy1.character) {
  
                  l1_map[enemy1.locX - 1][enemy1.locY] = 32;
  
              }
  
  
  
          }
      }
  
  
  
      return NULL;
  
  
  
  }
  </code></pre>
  
  <p><strong>What I am tryng to achieve:</strong></p>
  
  <p>I need to slow down the <code>EnemyMovemens()</code> function, because the enemy moves to fast. Or to make <code>PlayerMovements()</code> function independent from other functions.  </p>
  
  <p><strong>The Problem:</strong></p>
  
  <p>I have tried to use threads. Functions like <code>this_thread::sleep_for()</code> and <code>Sleep()</code>. To put them in <code>EnemyMovements()</code> function. In that case, it slow down all app and my player moves very slowly.</p>
  
  <p>I dont need a code, because I love to learn by myself. I just need a guidance what direction to move now.</p>
  
  <p><strong>OS: Win 10</strong></p>