Maze problem



The robot which signify as 'R' cannot correctly marked on the right position of the maze .I suspect there is something problem occur within the temp variables, when initialize it with 0.It would accumulate the value assign to it in all the process of looping which cause the incorrect position mark on the maze .After that, i declared four temp variable for w,a,s,d,the program stop when i press either one of the w,a,s,d.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#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 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 <<"Now 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--;
                             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;
                             temp =0;
                              }
                              }
                             
               else 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++;
                             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;
temp = 0;
}
                             }
            else 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);
                    while(temp != numsquare)
                    {        col--;
                             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;
temp = 0;
}
}
            else 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++;
                             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;
temp = 0;
}
                             }
               else{
               cout <<"You did not enter the right order."<<endl;
               }        
                
               if(ro!=rowend && col!=colsend)
               cout<<"The robot haven't reach the destination yet,please indicate it until the end.";
               }while(ro!=rowend && col!=colsend);            

     
    
}
         



Anyway to correct it ?
Topic archived. No new replies allowed.