i had construct a maze which i could setup the n x n with user input successfully.The problem is i cannot correctly specify the visited area with 'R',which is a symbol of a robot.

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <vector>

using namespace std;


void movement(vector<vector<char> >,int,int);
int n,colsend,rowend;
int main()
{

int column,row;


cout <<"Set the value of n x n squares area:"<<endl;
cout <<"Enter the value for n:";
cin >> n;
vector<vector<char> > square(n,vector<char>(n,'.'));
cout<<"\n\n";

for(int x=0;x<n;x++)
{cout<<setw(7);
for(int y=0;y<n;y++)
{
cout << square[x][y];
cout<<setw(3);
}
cout<<endl;
}
cout<<"\nThis is the square area robot could move."<<endl;
cout <<"\nNow specific the robot starting point and destination to move:"<<endl;
cout <<"Enter which column to start(starting from 0) :";
do{
cin >> column;
if(column > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(column>(n-1));
cout <<"Enter which column to end(starting from 0) :";
do{
cin >> colsend;
if(colsend > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(colsend>(n-1));



cout <<"Enter which row to start(starting from 0) :";
do{
cin >> row;
if(row > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(row >(n-1));
cout <<"Enter which row to end(starting from 0) :";
do{
cin >> rowend;
if(rowend > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(rowend >(n-1));


cout<<"The row and column of the starting point is ("<<row<<","<<column<<")"<<endl;
cout<<"The row and column of the destination is ("<<rowend<<","<<colsend<<")"<<endl;

square[row][column]='R';
square[rowend][colsend]='X';
for(int x=0;x<n;x++)
{cout<<setw(7);
for(int y=0;y<n;y++)
{
cout << square[x][y];
cout<<setw(3);
}
cout<<endl;
}
cout <<"\n(. is square)         (R is robot)";
movement(square,row,column);



getch();
return 0;

}

void movement(vector<vector<char> >square,int ro,int col)
{    
     char direction;
     int numsquare;
     int temp = 0;
     
     
     
     cout <<"\nNow specific the movement, press:"<<endl;
     cout <<"w for move up."<<endl;
     cout <<"s for move down."<<endl;
     cout <<"d for move right."<<endl;
     cout <<"a for move left."<<endl;
     do{
     cin >> direction;
     if(direction=='w')
     {
                    cout<<"How many square you want to move up:";
                    do{
                    cin >>numsquare;
                    if((ro-numsquare)<0)
                    cout <<"The number of square is not in the range,enter again:";
                    }while((ro-numsquare)<0);
                    
                    while(temp!= numsquare)
                    {        
                             ro-=1;
                             square[ro][col] = 'R';
                             temp++;
                             }
                             cout<<endl;
                             for(int x=0;x<n;x++)
                             {cout<<setw(7);
                             for(int y=0;y<n;y++)
                             {
                             cout << square[x][y];
                             cout<<setw(3);
                             }
                             cout<<endl;
                             }
                             
                             }
                                 
                                 
                                 cout<<"\nRowend value is "<<rowend<<endl;
                                 cout<<"\nRow value is"<<ro<<endl;
                                 if(ro!=rowend)
                                 {
                                 cout <<"Not reach yet.Enter the square again:";
                                 }
                                 
                                 }while(ro!=rowend);
                                 }

For example w is a part of direction to move upward of the 'R',in the maze.But the loop seems cannot remember the math operation on the ro variable to test whether it matching with rowend variable value.Anything i could done so the 'R' symbol could correctly place on the maze?

1st - your program should throw an error if both the w and r starts
at the same position.

I am not understanding your question well, is that you have a problem
distinguishing the position of the R?

Whats your program trying to simulate?

1st - your program should throw an error if both the w and r starts
at the same position.

I am not understanding your question well, is that you have a problem
distinguishing the position of the R?

Whats your program trying to simulate?

The program is about to simulate the robot move in an 2 dimensional array.First,the user needs to setup the starting point for the robot to move and then destination for the robot to stop.
The program then let the user determine which path to occupy by using the directional w,a,s,d to move .Once the robot move ,the 'R' would marked the visited area .

So the user enters say row 0 and col 0 for the starting position
for R and the ending position say row 5 and col 5. And you want
help to calculate the movement for the robot. Is this correct?

Well, I have a little trouble understanding your question.

I just tested out your program....

By this i figure 2 things,

1. You want the computer to move exactly the number of squares specified by the user without any intervention.

2. You want the computer to stop moving if you reach the rowend So what exactly are your requirements??

Yes,that is exactly what i meant,sorry to cause any misunderstanding.

Okay, I'm confused. I'm assuming you're debugging up first and so that's why only 'w' is supported.

do{
	cin >>numsquare;
	if ((ro-numsquare) < 0 )
		cout <<"The number of square is not in the range,enter again:";
	}while((ro-numsquare)<0);

???? You wish to move up numsquare, but I don't see what temp
has to do with it!  And you stay in the loop keep going up until you hit temp?  If temp is the wrong value you will exception fault!
And you're sticking lots of 'R''s into a column in square! Shouldn't you be erasing the old ones?

	while(temp!= numsquare)
	{        
		ro-=1;
		square[ro][col] = 'R';
		temp++;
	}

Have a timer class so that 'R' moves every second to a new position,
instead of moving all at once. It will look and feel more natural

To have the 'R' move to position where the user specifies, you can
do this :

First get RowNum, ColNum. Then determine the difference between
the 'R' position and the specified position. To do this say
'R' is at position 0,0. Where the first number is the row and the
second it the column number. Then say the user specifies the
coordinate 5,5. Where again, the first number is row and second is
col.
The difference between the final position and initial position
is (5-0),(5-0). Where is first set of number is the row coordinate and
the other is the col. So you can now increment the initial row
coordinate of 'R' by 1 say every one seconds. And when it reached
the difference (5-0) = 5 row number, stop the 'R'. And then go onto
the column coordinate. And find the difference, in the final and the
initial column coordinate. And update it every second until the
difference is met.

Have a timer class so that 'R' moves every second to a new position,
instead of moving all at once. It will look and feel more natural

To have the 'R' move to position where the user specifies, you can
do this :

First get RowNum, ColNum. Then determine the difference between
the 'R' position and the specified position. To do this say
'R' is at position 0,0. Where the first number is the row and the
second it the column number. Then say the user specifies the
coordinate 5,5. Where again, the first number is row and second is
col.
The difference between the final position and initial position
is (5-0),(5-0). Where is first set of number is the row coordinate and
the other is the col. So you can now increment the initial row
coordinate of 'R' by 1 say every one seconds. And when it reached
the difference (5-0) = 5 row number, stop the 'R'. And then go onto
the column coordinate. And find the difference, in the final and the
initial column coordinate. And update it every second until the
difference is met.

Could you please describe more detail about how to use the time class to move 'R' to new position according to every second ?Anyway thanks for every feedback from u guys,it really helps me a lot ,i had solve the problem.This my latest compilation of source code:

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <vector>

using namespace std;

void display(vector<vector<char> >);
void movement(vector<vector<char> >,int,int);
int numvisit(vector<vector<char> >);
int n,colsend,rowend,visit,nonvisit,column,row;
int main()
{



cout <<"Set the value of n x n squares area:"<<endl;
cout <<"Enter the value for n:";
cin >> n;
vector<vector<char> > square(n,vector<char>(n,'.'));
cout<<"\n\n";

for(int x=0;x<n;x++)
{cout<<setw(7);
for(int y=0;y<n;y++)
{
cout << square[x][y];
cout<<setw(3);
}
cout<<endl;
}
cout<<"\nThis is the square area robot could move."<<endl;
cout <<"\nNow specific the robot starting point and destination to move:"<<endl;
cout <<"Enter which column to start(starting from 0) :";
do{
cin >> column;
if(column > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(column>(n-1));
cout <<"Enter which column to end(starting from 0) :";
do{
cin >> colsend;
if(colsend > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(colsend>(n-1));



cout <<"Enter which row to start(starting from 0) :";
do{
cin >> row;
if(row > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(row >(n-1));
cout <<"Enter which row to end(starting from 0) :";
do{
cin >> rowend;
if(rowend > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(rowend >(n-1));


cout<<"The row and column of the starting point is ("<<row<<","<<column<<")"<<endl;
cout<<"The row and column of the destination is ("<<rowend<<","<<colsend<<")"<<endl;

square[row][column]='R';
square[rowend][colsend]='X';
for(int x=0;x<n;x++)
{cout<<setw(7);
for(int y=0;y<n;y++)
{
cout << square[x][y];
cout<<setw(3);
}
cout<<endl;
}
cout <<"\n(. is square)         (R is robot)";
movement(square,row,column);




getch();
return 0;

}
int numvisit(vector<vector<char> >square)
{
   
      for(int x=0;x<n;x++)
      {
              for(int y=0;y<n;y++)
              {              
                  if(square[x][y]=='R')
                  {visit+=1;
                  }
                  else{nonvisit+=1;
                  }
                  }
                   }  
         cout <<"Number of visits :"<<visit<<endl;
         cout <<"Number of nonvisited :"<<nonvisit; }   
void display(vector<vector<char> >square)
{                          for(int x=0;x<n;x++)
                             {cout<<setw(7);
                             for(int y=0;y<n;y++)
                             {
                             cout << square[x][y];
                             cout<<setw(3);
                             }
                             cout<<endl;
                             }
                             }
void movement(vector<vector<char> >square,int ro,int col)
{    
     char direction;
     int numsquare;
     int temp = 0;
     
     
     
     cout <<"\nNow specific the movement, press:"<<endl;
     cout <<"w for move up."<<endl;
     cout <<"s for move down."<<endl;
     cout <<"d for move right."<<endl;
     cout <<"a for move left."<<endl;
     do{
     cin >> direction;
     if(direction=='w')
     {
                    cout<<"How many square you want to move up:";
                    cin >>numsquare;
                    
                    
                    
                    while(temp != numsquare)
                    {        
                             ro-=1;
                             square[ro][col] = 'R';
                             temp++;
                             }
                             cout<<endl;
                             display(square);
                             
                             }
                            
               if(direction =='s')
               {
                    cout<<"How many square you want to move down:";
                    do{
                    cin >>numsquare;
                    if((ro+numsquare)>sizeof(square))
                    cout <<"The number of square is not in the range,enter again:";
                    }while((ro+numsquare)>sizeof(square));
                    while(temp != numsquare)
                    {        ro+=1;
                             square[ro][col] = 'R';
                             temp++;
                             }
                             cout<<endl;
                              display(square);
                             }
                     if(direction == 'a')
                   { cout<<"How many square you want to move to left:";
                    do{
                    cin >>numsquare;
                    if((col-numsquare)<0)
                    cout <<"The number of square is not in the range,enter again:";
                    }while(numsquare<0);
                    cout<<col;
                    while(temp != numsquare)
                    {        col-=1;
                             square[ro][col] = 'R';
                             temp++;
                             }
                             cout<<endl;
                             cout <<col;
                              display(square);
                          } 
            if(direction =='d')
            {
                    cout<<"How many square you want to move to right:";
                    do{
                    cin >>numsquare;
                    if((col+numsquare)>sizeof(square))
                    cout <<"The number of square is not in the range,enter again:";
                    }while((col+numsquare)>sizeof(square));
                    while(temp != numsquare)
                    {        col+=1;
                             square[ro][col] = 'R';
                             temp++;
                             }
                             cout<<endl;
                             display(square);
                             }   
                                 
                                 if(ro!=rowend || col!=colsend)
                                 {
                                 cout <<"Not reach yet.Enter the direction again(w,a,s,d):";
                                 
                                 }
                                 temp = 0;
                                 }while(ro!=rowend || col!=colsend);
                                 numvisit(square);
                                 
                                 }

Forget it. I though you were doing animation where
the user specifies the starting and ending position of the robot,
and you simulate it.

Forget it. I though you were doing animation where
the user specifies the starting and ending position of the robot,
and you simulate it.

Okay, here is another condition if i want to let robot move in the maze randomly instead of manually key in which way and how many steps until the destination,any suggestion of how to do it?It seems more complex because in c++ there is no random function

rand() tries to simulate random numbers.

you can do this :

enum Direction {LEFT,RIGHT,UP,DOWN};
int getRandDir() { return rand()%4; }
int main()
{
   Direction curr_direction =    getRandDir()
  switch(curr_direction)
  {
    case LEFT : if(canMoveLeft) then moveLeft; break;
    case RIGHT : if(canMoveRight) then moveRight; break;
    // same for UP and DOWN 
   }
}

Of course you need someType of algorithm to get Robot to that destination

Well, Normally if you need a shortest path algorithm the probable chioce would be a BREADTH FIRST SEARCH. but in this case, because there are no Obstructions..... It would be best to use basic math.

Your approach should be something like what you have with you already. but they are missing some things. SO it would be great to do something like this.
Note that this is the case if the robot wishes to get into the destination in the lowest possible number of steps.

First take in both x,y coordinates.

x1,y1 is the Origin and x2,y2 is the Destination.

if y2-y1 is lesser than zero The robot has to move Mod (y2-y1) steps downward.
if y2-y1 is greater than the robot has to move y2-y1 steps upward.

Then
The same applys to the x coordinates for left and right movement.

i have doing some modification to change the robot automatically find a path instead of manually assign

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <vector>

using namespace std;

enum Direction {LEFT = 1,RIGHT = 2,UP = 3,DOWN =4};
int getRandDir() ;
int getRandSquare() ;
void display(vector<vector<char> >);
void movement(vector<vector<char> >,int,int);
int numvisit(vector<vector<char> >);
int n,colsend,rowend,visit,nonvisit,column,row;
int main()
{
cout <<"Set the value of n x n squares area:"<<endl;
cout <<"Enter the value for n:";
cin >> n;
vector<vector<char> > square(n,vector<char>(n,'.'));
cout<<"\n\n";

for(int x=0;x<n;x++)
{cout<<setw(7);
for(int y=0;y<n;y++)
{
cout << square[x][y];
cout<<setw(3);
}
cout<<endl;
}

cout<<"\nThis is the square area robot could move."<<endl;
cout <<"\nNow specific the robot starting point and destination to move:"<<endl;
cout <<"Enter which column to start(starting from 0) :";
do{
cin >> column;
if(column > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(column>(n-1));
cout <<"Enter which column to end(starting from 0) :";
do{
cin >> colsend;
if(colsend > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(colsend>(n-1));



cout <<"Enter which row to start(starting from 0) :";
do{
cin >> row;
if(row > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(row >(n-1));
cout <<"Enter which row to end(starting from 0) :";
do{
cin >> rowend;
if(rowend > (n-1))
cout<<"The column size is more than the square areas,please enter again:";
}while(rowend >(n-1));


cout<<"The row and column of the starting point is ("<<row<<","<<column<<")"<<endl;
cout<<"The row and column of the destination is ("<<rowend<<","<<colsend<<")"<<endl;

square[row][column]='R';
square[rowend][colsend]='X';
for(int x=0;x<n;x++)
{cout<<setw(7);
for(int y=0;y<n;y++)
{
cout << square[x][y];
cout<<setw(3);
}
cout<<endl;
}
cout <<"\n(. is square)         (R is robot)"<<endl;
cout <<"\nNow execute the path of the robot"<<endl;
system("PAUSE");
movement(square,row,column);


getch();
return 0;

}
int getRandDir() { 
     srand((unsigned)time(0));
     int random_integer;
    random_integer = (rand()%5);
    return random_integer; }
    
    int getRandSquare() { 
     srand((unsigned)time(0));
     int random_integer;
    random_integer = (rand()%5);
    return random_integer; }
    
    void display(vector<vector<char> >square)
{                          for(int x=0;x<n;x++)
                             {cout<<setw(7);
                             for(int y=0;y<n;y++)
                             {
                             cout << square[x][y];
                             cout<<setw(3);
                             }
                             cout<<endl;
                             }
                             }
    
    void movement(vector<vector<char> >square,int ro,int col)
{    
     char direction;
     int numsquare;
     int random = 0;
     int a,b,c,d;
     int temp = 0;
      
      while(random == 0){     
      getRandDir();
      random = getRandDir();}
      cout <<random;
      
      
      if(random ==1)//move left
      {
      do{
      getRandSquare();
      a = getRandSquare();
      cout<<a;
      }while((col-a)<0);
      
      while(temp != a)
      {        
               col-=1;
               square[ro][col] = 'R';
               temp++;
      }
      display(square);
      }
      
      
      else if(random ==2)//move right
      { 
      do{
      getRandSquare();
      b =  getRandSquare();     
      cout<<b;
      }while((col+b)>n);
             
      while(temp != b)
      {        
               col+=1;
               square[ro][col] = 'R';
               temp++;
      }
      display(square);
      }
      
      
      else if(random ==3)//move up
      { 
      do{
       getRandSquare();
      c =  getRandSquare(); 
      cout<<c;     
      }while((ro-c)<0);
              
      while(temp != c)
      {        
               ro-=1;
               square[ro][col] = 'R';
               temp++;
      }
      display(square);
      }
      
      else if(random ==4)//move down
      { 
      do{
       getRandSquare();
      d =  getRandSquare();  
      cout<<d;     
      }while((ro+d)>n);
              
      while(temp != d)
      {        
               ro+=1;
               square[ro][col] = 'R';
               temp++;
      }
      display(square);
      }
      
      
      
      
      }

it is not complete yet ,the problem is if the number generated is 4 ,which is to make choice to move to right the infinite loop error happened ,why ?In contrary, the other 3 direction up ,down ,left works fine

I am unable to understand the logic behid Do while loops. .

Why would you look for the robot crossing the array bound ? Instead it should be that you try to avoid the array bounds and let it stay inside the map itself.

And secondly, See that the the variable temp is not local to itself, It might leave the while loops recursing towards the whole integer set and then again back to zero and then to the value of the variables a,b,c or d. Causing Undesred Results.

1) Find the slope between the initial coordinate and the final
coordinate.

2) Increase x and y by the slope.

Although there might be some caution you might have to take.

This is basically what I and sky suggested earlier. It might help you to hear it again.

Okay ,i really have a hard time to solve the coding with random() function.Now i switch everyting to the recursion form which is more easily and reduce the complexity:

#include <iostream>
#include <conio.h>
#include <iomanip>
#define FALSE 0
#define TRUE 1
#define NROWS 3
#define MCOLS 3

using namespace std;
static int steps;

// Symbols:
// '.' = open
// '#' = blocked
// 'S' = start
// 'G' = goal
// '+' = path
// 'x' = bad path
char maze[NROWS][MCOLS] = 
{
    {'S','.','.'},
    {'.','.','.'},
    {'.','.','G'}
    
};


void display_maze(void);
int find_path(int x, int y);


int main()
{
    display_maze();

    if ( find_path(0, 0) == TRUE )
       { 
       cout<<"Success!\n";
       }
    else
        cout<<"Failed\n";
      

    display_maze();
    cout<<"Steps"<<steps;
    getch();
    return 0;
}
// main()


void
display_maze(void)
{
    cout<<"MAZE:\n";
    
     for(int a=0;a<NROWS;a++)
    {
    for(int b=0;b<MCOLS;b++)
    {       cout<<setw(3);
            cout<<maze[a][b];
            }
            cout<<endl;}
            
    return;
}


int
find_path(int x, int y)
{
    // If x,y is outside maze, return false.
    if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;
// If x,y is the goal, return true.
    if ( maze[y][x] == 'G') 
    {    
         return TRUE;}
         
         
// If x,y is not open, return false.
    if ( maze[y][x] != '.' && maze[y][x] != 'S' ) return FALSE;
    
    // Mark x,y part of solution path.
    maze[y][x] = '+';
    steps++;

    label:
    // If find_path North of x,y is true, return true.
    if ( find_path(x, y - 1) == TRUE ) return TRUE;

    // If find_path East of x,y is true, return true.
    if ( find_path(x + 1, y) == TRUE ) return TRUE;

    // If find_path South of x,y is true, return true.
    if ( find_path(x, y + 1) == TRUE ) return TRUE;

    // If find_path West of x,y is true, return true.
    if ( find_path(x - 1, y) == TRUE ) return TRUE;

    //mark the repeat steps
    maze[y][x] = 'x';
    

    return FALSE;


}

Here is another problem,if i want the maze to be completed traverse even the 'G' had been reach.For example the output of the code is :

+ + +
. . +
. . G

The ' . ' is the area that is not yet traverse.I want the area to be traverse too,when the G is reach it would detect that there is still some space haven't passed. Which means might be include the repeated steps to mark with 'x'.Anything could be tweak in the codes ?

Why dont you just run a loop to replace all the '.' places with the 'x' after you find the path.

Check if this is somewhat of what your looking for. I use the same
logic as mentioned and suggested before. Its not complete but,
its just to help you out.

void prnt(char Map[][5])
{
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			cout<<Map[i][j]<<"  ";
		}
		cout<<endl;
	}

}
void setPos(int x, int y, char Map[][5])
{
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			if( i != x || j != y)
				Map[i][j] = '*';
			else Map[i][j] = 'R';
		}
	}
}
int main()
{ 	
	char Map[5][5] = 
	{
		{'*','*','*','*','*'},
		{'*','*','*','*','*'},
		{'*','*','*','*','*'},
		{'*','*','*','*','*'},
		{'*','*','*','*','*'}
	};
	
 
	
	cout<<"Original content \n";
	prnt(Map);

	int R = 0;//starting row of robot
	int C = 0; //starting col of robot

	//destinition row and column
	const int destR = 4;
	const int destC = 4;

	Map[R][C] = 'R';
	Map[destR][destC] = 'D';
	
	cout<<"\n\n'R'obot in place and 'D'estination locked\n\n";
	prnt(Map);


	//update time in milliseconds
	const unsigned int UPDATE_TIME = 1250; //1.25 seconds

	//slope formula : 
	//  (y_2 - y_1)
	//  -----------
	//  (x_2 - x_1)

	float slope = (destR - R) / (float) (destC - C);

	float newR(R),newC(C); //new position

	while( (newR != destR) &&  (newC != destC) )
	{
		static unsigned int clk_strt = clock();
		int clk_end = clock();

		if( (clk_end - clk_strt) > UPDATE_TIME ) //update position only if update time has been reached
		{
			clk_strt = clk_end; //reset time

			newR += slope;
			newC += slope;
			cout<<"\n\nNew position \n\n ";
			setPos(newR,newC,Map);
			prnt(Map);
		}
	}

	cout<<"\n\nDestination reached. Thank you !\n\n";

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.