How do I pop off only the unwanted moves?

This is a discussion on How do I pop off only the unwanted moves? within the C++ Programming forums, part of the General Programming Boards category; Hi, this little function is finally able to solve the maze. However it prints out all the moves (even the ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    How do I pop off only the unwanted moves?

    Hi,
    this little function is finally able to solve the maze. However it prints out all the moves (even the once that lead nowhere). I can't figure out where to pop of the moves in order to print out only the right moves that lead to the end of the maze.

    Code:
    .
    
    bool SolveMaze(char array[][40], position pos)
    
    {
    
    	stack<position> s;
    
    	position curr_pos, next_pos;
    
    	bool checked[40][40];
    
    
    
    
    
    	for(int i = 0; i < LENGTH; i++)
    
    		for(int j = 0; j < WIDTH; j++) {
    
    			checked[i][j] = false;
    
    		}
    
    
    
    	s.push(pos);
    
    	while(!s.empty())
    	{
    
    		curr_pos = s.top();
    
    		s.pop();
    		
    
    		if(array[curr_pos.x][curr_pos.y] == 'X')
    
    			return true;
    
    
    
    		// visit the current position
    
    		if(array[curr_pos.x][curr_pos.y] == '0' || array[curr_pos.x][curr_pos.y] == 'E')
    		{
    
    			if(array[curr_pos.x][curr_pos.y] == '0') 
    
    				array[curr_pos.x][curr_pos.y] = '*';
    
    			else if(checked[curr_pos.x][curr_pos.y] == true)
    
    				return false;
    
    
    
    			if(checked[curr_pos.x][curr_pos.y] == false)
    			{
    
    				next_pos.x = curr_pos.x;
    
    				next_pos.y = curr_pos.y + 1;
    
    				s.push(next_pos);
    
    
    
    				next_pos.x = curr_pos.x;
    
    				next_pos.y = curr_pos.y - 1;
    
    				s.push(next_pos);
    
    
    
    				next_pos.x = curr_pos.x + 1;
    
    				next_pos.y = curr_pos.y;
    
    				s.push(next_pos);
    
    
    
    				next_pos.x = curr_pos.x - 1;
    
    				next_pos.y = curr_pos.y;
    
    				s.push(next_pos);
    
    			} 
    			else
    			{ 
    
    				checked[curr_pos.x][curr_pos.y] = true;
    
    			}
    
    		}
    		
    	}
    
    	return false;
    
    }
    .

    Can someone please help me?
    Thanks.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I'd help, but I have no idea about what is going on in your code. For example I have no idea what the different map symbols mean ('E','X', '0','*'), don't know what you are printing since nowhere in this function is anything printed, nor completely understand some of your code like why you have something like:
    Code:
    if (checked[curr_pos.x][curr_pos.y] == false)
    {
       ...
    }
    else
        checked[curr_pos.x][curr_pos.y] == true;  // isn't it already true?? otherwise it wouldn't get here
    Could you supply the entire code and explain more what you are trying to do? Also, if the function returns true or false, what does that mean?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Michael Jackson: dead at age 50
    By Sebastiani in forum General Discussions
    Replies: 78
    Last Post: 07-25-2009, 11:27 AM
  2. push n pop
    By salmansalman in forum C++ Programming
    Replies: 3
    Last Post: 05-27-2008, 10:41 AM
  3. Queues
    By ramayana in forum C Programming
    Replies: 22
    Last Post: 01-01-2006, 02:08 AM
  4. Stack using push and pop
    By silicon in forum C++ Programming
    Replies: 5
    Last Post: 11-03-2003, 04:54 PM
  5. pop function
    By Troll_King in forum C Programming
    Replies: 12
    Last Post: 10-15-2001, 05:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21