Thread: How do I retrace my steps after a wrong move?

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

    Unhappy How do I retrace my steps after a wrong move?

    ...it's the same program i've been trying to finish for weeks now. I still can't get it to work properly.
    Can some loving person please help. I know this is qiute a bit of code to spiel....but this is just to help the kind one to undersand my algorithm.

    Please.....
    In the main I read the maze and print it( no problem with that). Then using the advance_maze function. Like so....

    Code:
    .
    int main()
    {
    	Move curr_move;
    	Curr_pos pos;
    	char Maze[40][40];
    	char filename[12];
    	int rows = 0;
    	int columns = 0;
    	MOVEDEQUE A;
    		
    	
    	cout<< "Please enter the name of the maze file you want to read, or (q) to quit.\n" << endl;
    	cout<< "(a) maze1.txt\n(b) maze2.txt\n(c) maze3.txt\n"<< endl;
    	cin.getline (filename, 10);
    	if (filename[0] == 'q')
    	{
    		return 0;
    	}
    	Read_file (Maze, &columns, &rows, pos, filename);
    	curr_move.maze_col = pos.col;
    	curr_move.maze_row = pos.row;
    	curr_move.move_number = 3;
    	Print_m_array (Maze, rows, columns);
    	A.push_back(curr_move);
    	
    	advance_maze(curr_move, A, Maze, &rows, &columns);
    	Print_m_array (Maze, rows, columns);
    	
    	
    	return 0;	
    }
    .

    This is the part that is not working well.

    Code:
    .
    
    void advance_maze( Move &move, MOVEDEQUE &M, char m_array[][40], int *columns, int *rows)
    {
    	
    	int n;
    	n = move.move_number;
    	int nxtrow = 0;
    	int nxtcol = 0;
    	bool blocked;
    	bool foundnxt_step;
    	//Move current_m;
    	
    	//M.push_back(move);
    	blocked = false;
    	foundnxt_step = false;
    	while (!M.empty())
    	{
    		//	move = M.back();
    		//  M.pop_back();
    		//M.push_back(move);
    		
    		if (m_array[move.maze_row][move.maze_col] == '0' || m_array[move.maze_row][move.maze_col] == 'E' )
    		{
    			m_array[move.maze_row][move.maze_col] = '*';
    			
    			nxtrow = move.maze_row + position[n].vertical;
    			nxtcol = move.maze_col + position[n].horizontal;
    			move.maze_row = nxtrow;
    			move.maze_col = nxtcol;
    			
    			for (; n<4;n++)
    			{
    				
    				if ( nxtrow < *rows && nxtcol < *columns && m_array[move.maze_row][move.maze_col] == '0')
    				{
    					
    					M.push_back(move);
    					//	m_array[move.maze_row][move.maze_col] = '*';
    					//move.maze_row = nxtrow;
    					//move.maze_col = nxtcol;
    					foundnxt_step = true;
    					for (; n<4 && foundnxt_step == true; )
    					{
    						m_array[move.maze_row][move.maze_col] = '*';
    						nxtrow = move.maze_row + position[n].vertical;
    						nxtcol = move.maze_col + position[n].horizontal;
    						move.maze_row = nxtrow;
    						move.maze_col = nxtcol;
    						
    						if(m_array[move.maze_row][move.maze_col] != '0')
    						{
    							foundnxt_step = false;
    							move = M.back();
    							//m_array[move.maze_row][move.maze_col] = '0';
    							n = 0;
    						}
    						else
    						{
    							M.push_back(move);
    							//m_array[move.maze_row][move.maze_col] = '*';
    						}
    					}
    					
    					
    					
    					/*if (m_array[nxtrow][nxtcol] != '0')
    					{
    					foundnxt_step = false;
    					n=0;
    				}*/
    					
    				}
    				else if (m_array[move.maze_row][move.maze_col] == 'X')
    				{
    					M.pop_back();
    					cout<< " Yah!!! Maze solved!! "<< endl;
    					break;
    				}
    				if (foundnxt_step == false)
    				{
    					//M.pop_back
    					for (; n<4 && foundnxt_step == false; n++ )
    					{
    						//n++;
    						nxtrow = move.maze_row + position[n].vertical;
    						nxtcol = move.maze_col + position[n].horizontal;
    						move.maze_row = nxtrow;  
    						move.maze_col = nxtcol;   
    						
    						
    						if (m_array[move.maze_row][move.maze_col] == '0')/// Why does it complain about a stack
    						{                                                    /// overflow here?
    							foundnxt_step = true;
    							///M.push_back(move);
    							//n =0;
    						}
    					}
    					if (n>3 && m_array[nxtrow][nxtcol] != '0')
    					{
    						blocked = true;
    						M.pop_back();
    						move =  M.back();
    					}
    					
    				}
    				
    			}
    		}
    		
    	}
    	
    	if (M.empty() && blocked == true)
    	{
    		cout<< "Awwww... sorry, this maze cannot be solved."<< endl;
    	}
    }
    .

    Thanks for taking a peek.!Really i'm very grateful.

  2. #2
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    Although my programs aren't very big yet, what i do is compile after most things i do, that way i know if something starts working improperly, or it doesn't compile, that it's just a few ctrl+z presses away.

    I don't think i'm going to read all that code. :-/ Someone else more knowledgable will help i'm sure.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I would like to help, but unfortunately the code is rather hard to follow without more information. Is there anyway you can go back and comment everything then repost the code? Otherwise it'll take forever trying to guess what logic and methods you are using which will make it impossible to fix your error.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: How do I retrace my steps after a wrong move?

    Code:
    for (; n<4 && foundnxt_step == false; n++ )
    {
         //n++;
         nxtrow = move.maze_row + position[n].vertical;
         nxtcol = move.maze_col + position[n].horizontal;
         move.maze_row = nxtrow;  
         move.maze_col = nxtcol;   
    						
    						
         if (m_array[move.maze_row][move.maze_col] == '0')/// Why does it complain about a stack
         {					              /// overflow here?
              foundnxt_step = true;
              ///M.push_back(move);
              //n =0;
         }
    }
    the bolded code increases move.maze exponentially... the code I would have used is as follows (in place of bold code):
    Code:
    move.maze_row+=position[n].vertical;
    mxtrow=move.maze_row
    Last edited by major_small; 05-15-2003 at 11:23 AM.

  5. #5
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    Code:
    if (m_array[move.maze_row][move.maze_col] == '0')/// Why does it complain about a stack
         {					              /// overflow here?
              foundnxt_step = true;
              ///M.push_back(move);
              //n =0;
         }
    it maybe giving that error because the bounds of an array are n-1, so you may just have to subtract 1 from both the row and column count

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-06-2005, 05:45 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. How do I move a pointer?
    By darksaidin in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2003, 04:43 PM
  4. Pathfinding AI? (Not the A* Algorithim)
    By harryP in forum C++ Programming
    Replies: 22
    Last Post: 08-01-2003, 02:32 PM
  5. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM