dartosgamer

Chasing alorithm

Oct 6th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. void wait(int sec)
  5. {
  6.      clock_t endwait;
  7.      
  8.      endwait = clock() + sec * CLOCKS_PER_SEC;
  9.      while (clock() < endwait) {}
  10. }
  11.  
  12. int main()
  13. {
  14.    
  15.     int grid[10][10] =
  16.     {
  17.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  18.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  19.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  20.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  21.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  22.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  23.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  24.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  25.         { 0, 0, 0, 0, 0, 0, 0, 0, 0},
  26.         { 0, 0, 0, 0, 0, 0, 0, 0, 0}
  27.     };
  28.    
  29.     int currentX = 0;
  30.     int currentY = 0;
  31.    
  32.     int goalX = 5;
  33.     int goalY = 5;
  34.    
  35.     int deltaX = goalX - currentX;
  36.     int deltaY = goalY - currentY;
  37.    
  38.     grid[goalX][goalY] = 2;
  39.     grid[currentX][currentY] = 1;
  40.    
  41.     while(true)
  42.     {
  43.                int directionX;
  44.                int directionY;
  45.                
  46.                if(deltaX != 0)
  47.                {
  48.                          directionX = deltaX / abs(deltaX);
  49.                }
  50.                
  51.                if (deltaY != 0)
  52.                {
  53.                      directionY = deltaY / abs(deltaY);
  54.                }
  55.                
  56.                grid[currentX][currentY] = 0;
  57.                
  58.                currentX += directionX;
  59.                currentY += directionY;
  60.                
  61.                grid[currentX][currentY] = 1;
  62.                
  63.                
  64.                wait(1);
  65.                system("clear");
  66.                
  67.                for (int i = 0; i < 9; i++)
  68.                {
  69.                    for(int h = 0; h < 9; h++)
  70.                    {
  71.                            std::cout << grid[i][h];
  72.                    }
  73.                    
  74.                    std::cout << std::endl;
  75.                    
  76.                }
  77.                
  78.                if((goalX == currentX + 1 || goalX == currentX - 1 && goalY == currentY + 1 || goalY == currentY - 1)) break;
  79.     }
  80.    
  81.     system("pause");
  82.     return 0;
  83. }
  84.                
  85.  
Advertisement
Add Comment
Please, Sign In to add comment