C++ Maze

hello so i am trying to do this

Please HELP!!!!!

----------------------------------
There are four positions to examine:

up (x, y-1)
left (x-1, y)
right (x+1, y)
down (x, y+1)

for EACH of these position, you must

1. check if the position is within bounds of the Maze
2.if it is within bounds, check if it a wall

if both are true, and ONLY if both are true, you add the position to the vector.


this is what i have so far
vector<Position*> Maze::getNeighbors(Position* position) {

vector<Position*> v;
int x = position->getX();
int y = position ->getY();


if(y - 1 > 0 && this->positions[x][y-1]->isWall()) v.push_back(this->positions[x][y-1]);

if(x - 1 > 0 && this->positions[y][x-1]->isWall()) v.push_back(this->positions[y][x-1]);

if(x + 1 > 0 && this->positions[y][x+1]->isWall()) v.push_back(this->positions[y][x+1]);

if(y + 1 > 0 && this->positions[x][y+1]->isWall()) v.push_back(this->positions[x][y+1]);

if(position->getY() - 1 > 0) v.push_back(this->positions[position->getX()][position->getY()-1]);

if(position->getX() - 1 > 0) v.push_back(this->positions[position->getX()-1][position->getY()]);

if(position->getY() + 1 < this->getHeight()) v.push_back(this->positions[position->getX()][position->getY()+1]);

if(position->getX() + 1 < this->getWidth()) v.push_back(this->positions[position->getX()+1][position->getY()]);



//if (this->positions [position->getX()+1][position->getY()]);
// if (this->positions [position->getX()][position->getY()-1]);
// if (this->positions [position->getX()-1][position->getY()]);
// if (this->positions [position->getX()][position->getY()+1]);

return v;

}

No one has answered in part because what you are doing seems a little convoluted, and also because what you are asking is not entirely clear.

If I understand correctly, you have a 2D array/vector/something where each element may be a “wall” or not-a-wall/something-else.

So, what is this function’s purpose?
I think it is trying to solve something that is not well defined.


For example, your 2D maze vector/array/whatever should be designed such that it is impossible to reach the edges without first hitting a wall. This should be a design prerequisite. This would simplify a whole lot of stuff in your code.
Topic archived. No new replies allowed.