Thread: using deques to solve mazes.

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

    using deques to solve mazes.

    I heeded the advice I was given. I made my code more readable... I thought through it. I sought help from my peers and my proff, but still to no avail. I really need help. Can some lovely person please help me.

    The first part works without a problem.
    Code:
    .
    struct move_record{
    	int horizontal;
    	int vertical;
    };
    
    struct Move{
    		int maze_row;
    		int maze_col;
    		int move_number;
    };
    
    struct Curr_pos{
    	int row;
    	int col;
    };
    
    
    
    using namespace std;
    
    typedef deque<Move> MOVEDEQUE;
    
    
    
    
    /* Declaring Prototypes*/
    
    void Read_file (char m_array[][40], int *columns, int *rows, Curr_pos &start, char *filename);
    void Print_m_array (char m_array[][40], int rows, int columns);
    bool legal_move(char [40][40],Move &);
    void advance_maze( Move &move, MOVEDEQUE &M, char m_array[][40]);
    
    
    move_record position[4] = 
    {
    	{-1,0},
    	{ 1,0},
    	{0,-1},
    	{ 0,1}
    };
    					
    
    
    
    int main()
    {
    	Move curr_move;
    	Curr_pos start;
    	char Maze[40][40];
    	char filename[12];
    	int rows = 0;
    	int columns = 0;
    	MOVEDEQUE A;
    	bool blocked;
    
    			
    		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, start, filename);
    //		printf("%d,%d \n", start.row, start.col);
    		curr_move.maze_col = start.col;
    		curr_move.maze_row = start.row;
    		curr_move.move_number = 0;
    		Print_m_array (Maze, rows, columns);
    		Maze[curr_move.maze_row][curr_move.maze_col] = '0';
    		A.push_back(curr_move);
    		blocked = false;
    		advance_maze(curr_move, A, Maze);
    		Print_m_array (Maze, rows, columns);
    	
    	return 0;	
    }
    .

    *****The section with the problem is this:
    Code:
    .
    
    void advance_maze( Move &move, MOVEDEQUE &M, char m_array[][40])
    {
    
    	int n;
    	n = move.move_number;
    	bool blocked;
    	
    	//M.push_back(move);
    	while (!M.empty())
    	{
    		move = M.back();
    		M.pop_back();
    		printf("%c\n",m_array[move.maze_row][move.maze_col]);
    /*** I don't  understand why it doesn't execute this if statement ... not even the first time. It shd...right?
    		if (m_array[move.maze_row][move.maze_col] == 0)
    		{
    			m_array[move.maze_row][move.maze_col] = '*';
    			M.push_back(move);
    			Print_m_array (m_array, move.maze_row, move.maze_col);
    			m_array[move.maze_row][move.maze_col]= 
    				m_array [move.maze_row +position[n].vertical][move.maze_col + position[n].horizontal];
    		}
    		else if (m_array[move.maze_row][move.maze_col] == 'X')
    		{
    			cout<< " Yah!!! Maze solved!! "<< endl;
    			break;
    		}
    			if (m_array[move.maze_row][move.maze_col] == 1) 
    		{
    			M.pop_back();
    			n++;
    			m_array[move.maze_row][move.maze_col] =
    				m_array[move.maze_row +position[n].vertical][move.maze_col+position[n].horizontal];
    		}
    		if (n>3)
    		{
    			blocked = true;
    			M.pop_back();
    			n = 0;
    			m_array[move.maze_row +position[n].vertical][move.maze_col+position[n].horizontal];
    		}
    	}
    
    	if (M.empty() && blocked == true)
    	{
    		cout<< "Awwww... sorry, this maze cannot be solved."<< endl;
    	}
    }
    .

    thanks for taking a look.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I made my code more readable...I really need help."

    Maybe use blue and a different font?

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    16
    Is this better?

    [size= huge]
    Code:
    .[color= blue]
    
    struct move_record{
    	int horizontal;
    	int vertical;
    };
    
    struct Move{
    		int maze_row;
    		int maze_col;
    		int move_number;
    };
    
    struct Curr_pos{
    	int row;
    	int col;
    };
    
    
    
    using namespace std;
    
    typedef deque<Move> MOVEDEQUE;
    
    
    
    
    /* Declaring Prototypes*/
    
    void Read_file (char m_array[][40], int *columns, int *rows, Curr_pos &start, char *filename);
    void Print_m_array (char m_array[][40], int rows, int columns);
    bool legal_move(char [40][40],Move &);
    void advance_maze( Move &move, MOVEDEQUE &M, char m_array[][40]);
    
    
    move_record position[4] = 
    {
    	{-1,0},
    	{ 1,0},
    	{0,-1},
    	{ 0,1}
    };
    					
    
    
    
    int main()
    {
    	Move curr_move;
    	Curr_pos start;
    	char Maze[40][40];
    	char filename[12];
    	int rows = 0;
    	int columns = 0;
    	MOVEDEQUE A;
    	bool blocked;
    
    			
    		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, start, filename);
    //		printf("%d,%d \n", start.row, start.col);
    		curr_move.maze_col = start.col;
    		curr_move.maze_row = start.row;
    		curr_move.move_number = 0;
    		Print_m_array (Maze, rows, columns);
    		Maze[curr_move.maze_row][curr_move.maze_col] = '0';
    		A.push_back(curr_move);
    		blocked = false;
    		advance_maze(curr_move, A, Maze);
    		Print_m_array (Maze, rows, columns);
    	
    	return 0;	
    }
    .

    *****The section with the problem is this:

    Code:
    .
    
    void advance_maze( Move &move, MOVEDEQUE &M, char m_array[][40])
    {
    
    	int n;
    	n = move.move_number;
    	bool blocked;
    	
    	//M.push_back(move);
    	while (!M.empty())
    	{
    		move = M.back();
    		M.pop_back();
    		printf("%c\n",m_array[move.maze_row][move.maze_col]);
    /*** I don't  understand why it doesn't execute this if statement ... not even the first time. It shd...right?
    		if (m_array[move.maze_row][move.maze_col] == 0)
    		{
    			m_array[move.maze_row][move.maze_col] = '*';
    			M.push_back(move);
    			Print_m_array (m_array, move.maze_row, move.maze_col);
    			m_array[move.maze_row][move.maze_col]= 
    				m_array [move.maze_row +position[n].vertical][move.maze_col + position[n].horizontal];
    		}
    		else if (m_array[move.maze_row][move.maze_col] == 'X')
    		{
    			cout<< " Yah!!! Maze solved!! "<< endl;
    			break;
    		}
    			if (m_array[move.maze_row][move.maze_col] == 1) 
    		{
    			M.pop_back();
    			n++;
    			m_array[move.maze_row][move.maze_col] =
    				m_array[move.maze_row +position[n].vertical][move.maze_col+position[n].horizontal];
    		}
    		if (n>3)
    		{
    			blocked = true;
    			M.pop_back();
    			n = 0;
    			m_array[move.maze_row +position[n].vertical][move.maze_col+position[n].horizontal];
    		}
    	}
    
    	if (M.empty() && blocked == true)
    	{
    		cout<< "Awwww... sorry, this maze cannot be solved."<< endl;
    	}
    }
    
    [/size]
    .[/color]

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

    Unhappy An unending loop.

    I really need your help. I've been at this for a week now and I can't seem to find what i'm doing wrong. it's either I end up printing "this maze can't be soved" or I wait forever and the solved maze never gets printed...which means my loop never ends.

    the only time I solved mazes I used reccurrsion, now my proff wants us to use deques ....this didn' t sound like much.....but i'm stuck. Oh some one please help me.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Unhappy Trying to answer again(the first time I created a new thread)

    I donīt know what is a maze... but Iīll try to help.
    Just a note: How can I change color and such?
    Anyway...
    Maze[curr_move.maze_row][curr_move.maze_col] = '0';

    This char is the char '0', number 48 in the ascII table

    void advance_maze( Move &move, MOVEDEQUE &M, char m_arraymove.maze_col]);
    /*** I don't understand why it doesn't execute this if statement ... not even the first time. It shd...right?
    if (m_array[move.maze_row][move.maze_col] == 0)


    This one above is the char number 0 in the ascII table, but the char is a little face if Iīm not mistaken.
    So, you must use the ' ' here too, because '0'!=0

    if (m_array[move.maze_row][move.maze_col] == '0')

    I donīt know if this is the error, but this is strange for me anyway.
    hope that helps
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve efficiently
    By jack_carver in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 07:56 AM
  2. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  3. help solve linking problem, thanks
    By Raison in forum Windows Programming
    Replies: 8
    Last Post: 05-29-2004, 11:14 AM
  4. Plz solve my problem !
    By pankaj8096 in forum C Programming
    Replies: 12
    Last Post: 01-16-2003, 12:59 AM
  5. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM