candyapplecorn

unreadable

Jul 1st, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. /*
  2. this is a work in progress for my little maze program I'm working on
  3. There's lot of room for improvement I'm sure. For example I'd like
  4. the constructor for this class to be able to take a clickableMatrix
  5. */
  6. /*
  7. this class takes a matrix and returns boolean depending on whether or not there is
  8. a path between the "start" and "end". a path is consecutive elements containing 0
  9. adjacently but not diagonally.
  10. So far, it works fine with any maze I draw.
  11. */
  12.  
  13. template <class T>
  14. class pathFinder : public clickableMatrix<T>
  15. {
  16.     vector <T> path;
  17. public:
  18.     bool check(clickableMatrix<T> cm){
  19.         path = cm.get();
  20.         return check();
  21.     }
  22.     bool check(){
  23.         if (typeid(T) == typeid(int)){
  24.             for_each(path.begin(), path.end(), [&](T val){ val = (val ? WALL : 0); });
  25.             pair <int, int> temp;
  26.             int counter;
  27.             typedef pair < pair < int, int >, int > qp; // qp.first holds a coordinate. qp.second holds a pathing priority value
  28.             queue< qp > q;
  29.             q.push(qp(this->end, 0));
  30.             while (!q.empty()){
  31.                 temp = q.front().first;
  32.                 counter = q.front().second;
  33.                 q.pop();
  34.                 path[oneD(temp)] = counter;
  35.                 if (!path[oneD(temp.first + 1, temp.second)])
  36.                     if (oneD(temp.first + 1, temp.second) == oneD(begin)){
  37.                         path[oneD(temp.first + 1, temp.second)] = counter + 1;
  38.                         return true;
  39.                     }
  40.                     else
  41.                         q.push(qp(twoD(oneD(temp.first + 1, temp.second)), counter + 1));
  42.                 if (!path[oneD(temp.first - 1, temp.second)])
  43.                     if (oneD(temp.first - 1, temp.second) == oneD(begin)){
  44.                         path[oneD(temp.first - 1, temp.second)] = counter + 1;
  45.                         return true;
  46.                     }
  47.                     else
  48.                         q.push(qp(twoD(oneD(temp.first - 1, temp.second)), counter + 1));
  49.                 if (!path[oneD(temp.first, temp.second + 1)])
  50.                     if (oneD(temp.first, temp.second + 1) == oneD(begin)){
  51.                         path[oneD(temp.first, temp.second + 1)] = counter + 1;
  52.                         return true;
  53.                     }
  54.                     else
  55.                         q.push(qp(twoD(oneD(temp.first, temp.second + 1)), counter + 1));
  56.                 if (!path[oneD(temp.first, temp.second - 1)])
  57.                     if (oneD(temp.first, temp.second - 1) == oneD(begin)){
  58.                         path[oneD(temp.first, temp.second - 1)] = counter + 1;
  59.                         return true;
  60.                     }
  61.                     else
  62.                         q.push(qp(twoD(oneD(temp.first, temp.second - 1)), counter + 1));
  63.             }
  64.         }
  65.         return false;
  66.     }
  67.     pathFinder(int x, int y) : clickableMatrix<T>(x, y){};
  68. };
Advertisement
Add Comment
Please, Sign In to add comment