void MazeWalker::walkMaze(){

	if(m_isDFS){
		while(!isGoalReached()){
			storeValidMoves();

			if(m_moveStack.empty()){
				m_noExit = true;
				std::cout << "\nNo exit\n\n";
			}
			else{
				move(m_moveStack.top());
				m_moveStack.pop();
			}
		}
	}

	else{
		while(!isGoalReached()){
			storeValidMoves();

			if(m_moveQueue.empty()){
				m_noExit = true;
				std::cout << "\nNo exit\n\n";
			}
			else{
				move(m_moveQueue.front());
				m_moveQueue.pop();
			}
		}
	}

	if(!m_noExit)
		std::cout << "\nExit Found!\n\n";
}
Пример #2
0
bool MazeWalker::walkMaze(){
	
	//Do DFS
	if(m_searchType == Search::DFS){
		//Load starting position into stack
		m_moveStack.push(m_startPos);
		
		//keep this going until either the stack is empty or the goal is reached
		while(!(m_moveStack.empty() || isGoalReached())){
			//store valid moves from start or last iteration
			storeValidMoves();
			//move m_curPos to the top of stack 
			move(m_moveStack.top());
			
			//pop the position from the stack
			m_moveStack.pop();
			
		}	
	}
	else{
		//Load starting position into queue
		m_moveQueue.push(m_startPos);
		
		//keep going until queue is empty or goal is reached
		while(!(m_moveQueue.empty() || isGoalReached())){
			
			//store valid moves at the end of the queue
			storeValidMoves();
			//Remove from the front of the queue
  		  	m_moveQueue.pop();
			
			//Ensures that once the queue is empty no moves will be made.
			if(!m_moveQueue.empty()){
				move(m_moveQueue.front());
			}
		}
	}
	return isGoalReached();		//either it's reached or it isn't
}
Пример #3
0
void MazeWalker::walkMaze()
{
	move(*m_startPos);
	mazePrint();
	while (!isGoalReached())
	{
		storeValidMoves();
		if (m_isDFS)
		{
			if (m_moveStack.empty())
			{
				break;
			}
			move(m_moveStack.top());
			m_moveStack.pop();
		}
		else
		{
			if (m_moveQueue.empty())
			{
				break;
			}
			move(m_moveQueue.front());
			m_moveQueue.pop();
		}
		mazePrint();
	}
	if (isGoalReached())
	{
		std::cout << '\n' << "Exit found!" << '\n';
	}
	else
	{
		std::cout << '\n' << "No way out!" << '\n';
	}
}
Пример #4
0
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;
	
}