Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- this is a work in progress for my little maze program I'm working on
- There's lot of room for improvement I'm sure. For example I'd like
- the constructor for this class to be able to take a clickableMatrix
- */
- /*
- this class takes a matrix and returns boolean depending on whether or not there is
- a path between the "start" and "end". a path is consecutive elements containing 0
- adjacently but not diagonally.
- So far, it works fine with any maze I draw.
- */
- template <class T>
- class pathFinder : public clickableMatrix<T>
- {
- vector <T> path;
- public:
- bool check(clickableMatrix<T> cm){
- path = cm.get();
- return check();
- }
- bool check(){
- if (typeid(T) == typeid(int)){
- for_each(path.begin(), path.end(), [&](T val){ val = (val ? WALL : 0); });
- pair <int, int> temp;
- int counter;
- typedef pair < pair < int, int >, int > qp; // qp.first holds a coordinate. qp.second holds a pathing priority value
- queue< qp > q;
- q.push(qp(this->end, 0));
- while (!q.empty()){
- temp = q.front().first;
- counter = q.front().second;
- q.pop();
- path[oneD(temp)] = counter;
- if (!path[oneD(temp.first + 1, temp.second)])
- if (oneD(temp.first + 1, temp.second) == oneD(begin)){
- path[oneD(temp.first + 1, temp.second)] = counter + 1;
- return true;
- }
- else
- q.push(qp(twoD(oneD(temp.first + 1, temp.second)), counter + 1));
- if (!path[oneD(temp.first - 1, temp.second)])
- if (oneD(temp.first - 1, temp.second) == oneD(begin)){
- path[oneD(temp.first - 1, temp.second)] = counter + 1;
- return true;
- }
- else
- q.push(qp(twoD(oneD(temp.first - 1, temp.second)), counter + 1));
- if (!path[oneD(temp.first, temp.second + 1)])
- if (oneD(temp.first, temp.second + 1) == oneD(begin)){
- path[oneD(temp.first, temp.second + 1)] = counter + 1;
- return true;
- }
- else
- q.push(qp(twoD(oneD(temp.first, temp.second + 1)), counter + 1));
- if (!path[oneD(temp.first, temp.second - 1)])
- if (oneD(temp.first, temp.second - 1) == oneD(begin)){
- path[oneD(temp.first, temp.second - 1)] = counter + 1;
- return true;
- }
- else
- q.push(qp(twoD(oneD(temp.first, temp.second - 1)), counter + 1));
- }
- }
- return false;
- }
- pathFinder(int x, int y) : clickableMatrix<T>(x, y){};
- };
Advertisement
Add Comment
Please, Sign In to add comment