Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <stdlib.h> /* srand, rand */
- #include <time.h> /* time */
- #include <stack> // std::stack
- #include <exception>
- const int sizeMapHeight = 50;
- const int sizeMapWidth = 50;
- enum WalkDirection {
- North = 0,
- South,
- East,
- West
- };
- class RandomWalk {
- public :
- RandomWalk(int _HeightOfDMap, int _WidthOfDMap, int _numIteration) : mHeightOfDMap(_HeightOfDMap),mWidthOfMap(_WidthOfDMap), numIteration(_numIteration) {
- std::vector<bool> vMapCopy(mHeightOfDMap * mWidthOfMap ,false);
- vMap = vMapCopy;
- walkerPosition = 0; //rand() % (mHeightOfDMap * mWidthOfMap); //on place un point aléatoire
- vMap.at(walkerPosition) = true;
- }
- void Walk() {
- /*
- 2D -> 1D
- i = x + width*y;
- and the reverse operation is
- x = i % width; // % is the "modulo operator", the remainder of i / width;
- y = i / width; // where "/" is an integer division
- 1D -> 2D
- x = i % width;
- y = (i / width)%height;
- z = i / (width*height);
- */
- //move random direction and reverse the track when there is no option
- int it{ 0 };
- std::stack<int> previousWalk;//keep track of the walk
- int caseComplete{ 0 };
- int numberOfMoveByTurn{ 0 };
- bool isExit = false;
- //it <= numIteration &&
- while (!isExit )
- {
- bool asWalk = false;
- Direction = static_cast<WalkDirection>(rand() % 4); //n s e w
- int x = walkerPosition % mWidthOfMap;
- int y = walkerPosition / mHeightOfDMap;
- int index{ 0 };
- auto calculIndex = [=](int x, int y) {return x + y * mWidthOfMap; };
- auto checkDirection = [&](int index) {
- if (index >= 0 && index <= ((mHeightOfDMap * mWidthOfMap) - 1)) {
- if (vMap.at(index) != true)
- {
- vMap.at(index) = true;
- previousWalk.push(walkerPosition);
- walkerPosition = index;
- std::cout << walkerPosition << std::endl;
- it++;
- numberOfMoveByTurn = 0;
- if (it >= ((mHeightOfDMap * mWidthOfMap) * 0.50))
- isExit = true;
- }
- else
- {
- numberOfMoveByTurn++;
- if (numberOfMoveByTurn > 4) //arbitrary number
- {
- if (previousWalk.size() > 0)
- {
- walkerPosition = previousWalk.top();
- previousWalk.pop();
- }
- else
- {
- isExit = true;
- }
- }
- }
- }};
- if (Direction == North ) {
- if ((y - 1) >= 0)
- {
- y -= 1;
- index = calculIndex(x, y);
- checkDirection(index);
- }
- }
- else if (Direction == South) {
- if ((y + 1) <= mHeightOfDMap)
- {
- y += 1;
- index = calculIndex(x, y);
- checkDirection(index);
- }
- }
- else if (Direction == West ) {
- if (x - 1 >= 0)
- {
- x -= 1;
- index = calculIndex(x, y);
- checkDirection(index);
- }
- }
- else if (Direction == East ) {
- if (x + 1 <= mWidthOfMap)
- {
- x += 1;
- index = calculIndex(x, y);
- checkDirection(index);
- }
- }
- }
- std::cout << "iteration number to know if the result wanted is good" << it << std::endl;
- }
- void ShowMap() {
- for (int y = 0; y < mHeightOfDMap; y++)
- {
- for (int x = 0; x < mWidthOfMap; x++)
- {
- if (vMap.at(x + y * mWidthOfMap) == true) {
- std::cout << '*';
- }
- else {
- std::cout << '.';
- }
- }
- std::cout << '\n';
- }
- }
- std::vector<bool> GetMap() {
- return vMap;
- }
- private:
- const int mHeightOfDMap;
- const int mWidthOfMap;
- int numIteration, walkerPosition;
- std::vector<bool> vMap;
- WalkDirection Direction;
- };
- int main()
- {
- srand(time(NULL));
- while (1) {
- RandomWalk rndWalk(10, 10, 30);
- rndWalk.Walk();
- rndWalk.ShowMap();
- std::cin.get();
- }
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement