MazeWalker::MazeWalker(char** mazePtr, int startRow, int startCol, int rows, int cols, bool dfs): m_startPos(startRow,startCol) , m_curPos(startRow,startCol){ m_maze = mazePtr; m_rows = rows; m_cols = cols; m_isDFS = dfs; m_noExit = false; m_visited = new bool *[m_rows]; for(int i = 0; i < m_rows; i++){ m_visited[i] = new bool[m_cols]; for(int j = 0; j<m_cols; j++){ m_visited[i][j] = false; } } Position firstMove = Position(startRow,startCol); move(firstMove); walkMaze(); }
bool MazeWalker::walkMaze() { /* set up position; beware that value may exceed or less than the m_rows and m_cols, so we need to set conditions to check */ char up_ = 'n'; char right_ = 'n'; char down_ = 'n'; char left_ = 'n'; //direction_ is the character at the specific location w.r.t the m_curPos to the maze //So the first time this method is called, it's at the start position if(m_curPos.getRow()-1 >= 0) up_ = m_maze[ m_curPos.getRow()-1 ][ m_curPos.getCol() ]; if(m_curPos.getCol()+1 < m_cols) right_ = m_maze[ m_curPos.getRow() ][ m_curPos.getCol()+1 ]; if(m_curPos.getRow()+1 < m_rows) down_ = m_maze[ m_curPos.getRow()+1 ][ m_curPos.getCol() ]; if(m_curPos.getCol()-1 >= 0) left_ = m_maze[ m_curPos.getRow() ][ m_curPos.getCol()-1 ]; //positions ---- use to check if they exceeds the boundary of the maze int up_move = m_curPos.getRow()-1; int right_move = m_curPos.getCol()+1; int down_move = m_curPos.getRow()+1; int left_move = m_curPos.getCol()-1; if(m_searchType == Search::DFS) { if(isGoalReached()) { while(m_moveStack.size() > 0) m_moveStack.pop(); return true; } else { if((up_ == 'P' || up_ == 'E') && up_move >= 0) //so it's within range { Position up = Position( m_curPos.getRow()-1 , m_curPos.getCol() ); m_moveStack.push(up); } if((right_ == 'P' || right_ == 'E') && right_move < m_cols) //so it's within range { Position right = Position( m_curPos.getRow() , m_curPos.getCol()+1 ); m_moveStack.push(right); } if((down_ == 'P' || down_ == 'E') && down_move < m_rows) //so it's within range { Position down = Position( m_curPos.getRow()+1 , m_curPos.getCol() ); m_moveStack.push(down); } if((left_ == 'P' || left_ == 'E') && left_move >= 0 ) //so it's within range { Position left = Position( m_curPos.getRow() , m_curPos.getCol()-1 ); m_moveStack.push(left); } while(m_moveStack.size() > 0) { Position temp = Position(m_moveStack.top().getRow(), m_moveStack.top().getCol()); m_moveStack.pop(); //pop if( m_visited[ temp.getRow() ][ temp.getCol() ] == 0 ) { move(temp); //move to that temporary position storeValidMoves(); //mark as visited walkMaze(); } }//endwhile }//end else }//end if DFS if(m_searchType == Search::BFS) { m_moveQueue.push(m_curPos); //EnQueue( m.StartNode ) while(m_moveQueue.size() > 0) { m_curPos = Position(m_moveQueue.front().getRow(), m_moveQueue.front().getCol()); //store valid moves after first run if(m_curPos.getRow() != m_startPos.getRow() || m_curPos.getCol() != m_startPos.getCol()) storeValidMoves(); //conditions if(m_curPos.getRow()-1 >= 0) up_ = m_maze[ m_curPos.getRow()-1 ][ m_curPos.getCol() ]; else up_ = 'n'; if(m_curPos.getCol()+1 < m_cols) right_ = m_maze[ m_curPos.getRow() ][ m_curPos.getCol()+1 ]; else right_ = 'n'; if(m_curPos.getRow()+1 < m_rows) down_ = m_maze[ m_curPos.getRow()+1 ][ m_curPos.getCol() ]; else down_ = 'n'; if(m_curPos.getCol()-1 >= 0) left_ = m_maze[ m_curPos.getRow() ][ m_curPos.getCol()-1 ]; else left_ = 'n'; int up_move = m_curPos.getRow()-1; int right_move = m_curPos.getCol()+1; int down_move = m_curPos.getRow()+1; int left_move = m_curPos.getCol()-1; m_moveQueue.pop(); if(isGoalReached()) { while(m_moveQueue.size() > 0) { m_moveQueue.pop(); } return true; } else { //first, we need to store all valid neighbors into the queue //for each neighbor: up - right - down - left if((up_ == 'P' || up_ == 'E') && up_move >= 0) //so it's within range { //create a position Position up = Position( m_curPos.getRow()-1 , m_curPos.getCol() ); //push to quene if( m_visited[ up.getRow() ][ up.getCol() ] == 0 ) m_moveQueue.push(up); } if((right_ == 'P' || right_ == 'E') && right_move < m_cols) //so it's within range { Position right = Position( m_curPos.getRow() , m_curPos.getCol()+1 ); if( m_visited[ right.getRow() ][ right.getCol() ] == 0 ) m_moveQueue.push(right); } if((down_ == 'P' || down_ == 'E') && down_move < m_rows) //so it's within range { Position down = Position( m_curPos.getRow()+1 , m_curPos.getCol() ); if( m_visited[ down.getRow() ][ down.getCol() ] == 0 ) m_moveQueue.push(down); } if((left_ == 'P' || left_ == 'E') && left_move >= 0 ) //so it's within range { Position left = Position( m_curPos.getRow() , m_curPos.getCol()-1 ); if( m_visited[ left.getRow() ][ left.getCol() ] == 0 ) m_moveQueue.push(left); } }//end else }//end while } return false; }