Advertisement
raoul632

RandomWalker

Feb 23rd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>     /* srand, rand */
  4. #include <time.h>       /* time */
  5. #include <stack>          // std::stack
  6. #include <exception>
  7.  
  8. const int sizeMapHeight = 50;
  9. const int sizeMapWidth = 50;
  10.  
  11.  
  12. enum WalkDirection {
  13.   North = 0,
  14.   South,
  15.   East,
  16.   West
  17. };
  18.  
  19. class RandomWalk {
  20.  
  21. public :
  22.     RandomWalk(int _HeightOfDMap, int _WidthOfDMap, int _numIteration) : mHeightOfDMap(_HeightOfDMap),mWidthOfMap(_WidthOfDMap), numIteration(_numIteration) {
  23.         std::vector<bool> vMapCopy(mHeightOfDMap * mWidthOfMap ,false);
  24.         vMap = vMapCopy;
  25.         walkerPosition = 0; //rand() % (mHeightOfDMap * mWidthOfMap); //on place un point aléatoire
  26.         vMap.at(walkerPosition) = true;
  27.     }
  28.  
  29.     void Walk() {
  30.         /*
  31.         2D -> 1D
  32.         i = x + width*y;
  33.         and the reverse operation is
  34.         x = i % width;    // % is the "modulo operator", the remainder of i / width;
  35.         y = i / width;    // where "/" is an integer division
  36.  
  37.         1D -> 2D
  38.         x = i % width;
  39.         y = (i / width)%height;
  40.         z = i / (width*height);
  41.         */
  42.         //move random direction and reverse the track when there is no option
  43.         int it{ 0 };
  44.         std::stack<int> previousWalk;//keep track of the walk
  45.        
  46.         int caseComplete{ 0 };
  47.         int numberOfMoveByTurn{ 0 };
  48.         bool isExit = false;
  49.         //it <= numIteration &&
  50.         while (!isExit  )
  51.         {
  52.             bool asWalk = false;
  53.             Direction = static_cast<WalkDirection>(rand() % 4); //n s e w
  54.             int x = walkerPosition %  mWidthOfMap;
  55.             int y = walkerPosition / mHeightOfDMap;
  56.            
  57.             int index{ 0 };
  58.             auto calculIndex = [=](int x, int y) {return x + y * mWidthOfMap; };
  59.             auto checkDirection = [&](int index) {
  60.                 if (index >= 0 && index <= ((mHeightOfDMap * mWidthOfMap) - 1)) {
  61.                     if (vMap.at(index) != true)
  62.                     {      
  63.                         vMap.at(index) = true;
  64.                         previousWalk.push(walkerPosition);
  65.                         walkerPosition = index;    
  66.                         std::cout << walkerPosition << std::endl;
  67.                         it++;
  68.                         numberOfMoveByTurn = 0;
  69.  
  70.                         if (it >= ((mHeightOfDMap * mWidthOfMap) * 0.50))
  71.                             isExit = true;
  72.                     }
  73.                     else
  74.                     {
  75.                         numberOfMoveByTurn++;
  76.  
  77.                         if (numberOfMoveByTurn > 4) //arbitrary number
  78.                         {
  79.                             if (previousWalk.size() > 0)
  80.                             {
  81.                                 walkerPosition = previousWalk.top();
  82.                                 previousWalk.pop();
  83.                             }
  84.                             else
  85.                             {
  86.                                 isExit = true;
  87.                                
  88.                             }
  89.                         }
  90.                     }
  91.                    
  92.                    
  93.                 }};
  94.             if (Direction == North ) {
  95.                 if ((y - 1) >= 0)
  96.                 {
  97.                     y -= 1;
  98.                     index = calculIndex(x, y);
  99.                     checkDirection(index);
  100.                 }
  101.             }
  102.             else if (Direction == South) {
  103.                 if ((y + 1) <= mHeightOfDMap)
  104.                 {
  105.                     y += 1;
  106.                     index = calculIndex(x, y);
  107.                     checkDirection(index);
  108.                 }
  109.             }
  110.             else if (Direction == West ) {
  111.                 if (x - 1 >= 0)
  112.                 {
  113.                     x -= 1;
  114.                     index = calculIndex(x, y);
  115.                     checkDirection(index);
  116.                 }
  117.             }
  118.             else if (Direction == East ) {
  119.                 if (x + 1 <= mWidthOfMap)
  120.                 {
  121.                     x += 1;
  122.                     index = calculIndex(x, y);
  123.                     checkDirection(index);
  124.                 }
  125.             }
  126.         }
  127.         std::cout << "iteration number to know if the result wanted is good" << it << std::endl;
  128.     }
  129.  
  130.     void ShowMap() {
  131.         for (int y = 0; y < mHeightOfDMap; y++)
  132.         {
  133.             for (int x = 0; x < mWidthOfMap; x++)
  134.             {
  135.                 if (vMap.at(x + y * mWidthOfMap) == true) {
  136.                     std::cout << '*';
  137.                    
  138.                 }
  139.                 else {
  140.                     std::cout << '.';
  141.                 }
  142.                    
  143.             }
  144.             std::cout << '\n';
  145.         }
  146.     }
  147.  
  148.  
  149.  
  150.     std::vector<bool> GetMap() {
  151.         return vMap;
  152.     }
  153.  
  154.  
  155. private:
  156.     const int mHeightOfDMap;
  157.     const int mWidthOfMap;
  158.     int numIteration, walkerPosition;
  159.     std::vector<bool> vMap;
  160.     WalkDirection Direction;
  161.  
  162.  
  163.  
  164. };
  165.  
  166.  
  167. int main()
  168. {
  169.  
  170.     srand(time(NULL));
  171.     while (1) {
  172.         RandomWalk rndWalk(10, 10, 30);
  173.         rndWalk.Walk();
  174.         rndWalk.ShowMap();
  175.         std::cin.get();
  176.     }
  177.     std::cin.get();
  178.     return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement