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?