help with robot movement

in this program the figure is supposed to move left and right but the legs move faster when moving left, i can't figure out whats wrong. my code,
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <iostream.h>
#include<conio.h>
#include <windows.h>

 


 
const char left_key='a', right_key='d';
char grid[7][79];
int i, j;

void gotoxy(short x, short y)
{
     HANDLE hConsoleOutput;
     COORD Cursor = {x,y};
     hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
  

#define VMAX 3
#define HMAX 3

void printgrid_sky(); 
void printgrid_land();
void printRobot(char robot[][3],int x, int y);
void printRobot1(char robot1[][3],int x, int y);
void printRobot2(char robot2[][3],int x, int y);
void fillIn(int x,int y);

 const int HEIGHT = 7;
const int WIDTH  = 79;

int main()

{
	int mx=10,mx1=10,mx2=10;
	int my=16,my1=17,my2=18;
	
	int x,y;
	char direction;
	char keypress;

    

	char robot[VMAX][HMAX]={'.','o','.'};
	char robot1[VMAX][HMAX]={'-','|','-'};
	char robot2[VMAX][HMAX]={'/',' ','\\'};

 

     

    
	printgrid_sky();
    printgrid_land();

    gotoxy(mx,my);
    printRobot(robot,x,y);

	gotoxy(mx1,my1);
    printRobot1(robot1,x,y);

	gotoxy(mx2,my2);
    printRobot2(robot2,x,y);
 

    while (mx<=(WIDTH-HMAX) && my<20 && my>0 && mx>=0 && mx1<=(WIDTH-HMAX) && my1<20 && my1>0 && mx1>=0 && mx2<=(WIDTH-HMAX) && my2<20 && my2>0 && mx2>=0)

    {

      if (kbhit())

      {

            keypress=getch();

             

            if ((keypress == right_key) || (keypress == left_key))

                 direction = keypress;

             
            if (direction == left_key)
            {
                if (mx > 0 && mx1 > 0 && mx2 > 0)
                {
                    gotoxy(mx,my);
                    cout<<" "<<flush;

                    mx=mx-1;
                    gotoxy(mx,my);

                    printRobot(robot,x,y);
                    fillIn(x,y);
                    gotoxy(mx,my);





					gotoxy(mx1,my1);
                    cout<<" "<<flush;

                    mx1=mx1-1;
                    gotoxy(mx1,my1);

                    printRobot1(robot1,x,y);
                    fillIn(x,y);
                    gotoxy(mx1,my1);



					
					gotoxy(mx2,my2);
                    cout<<" "<<flush;

                    mx2=mx2-2;
                    gotoxy(mx2,my2);

                    printRobot2(robot2,x,y);
                    fillIn(x,y);
                    gotoxy(mx2,my2);


                }
            }

            if (direction == right_key)
            {
                if (mx < (WIDTH - HMAX) && mx1 < (WIDTH - HMAX) && mx2 < (WIDTH - HMAX))
                {
                    gotoxy(mx,my);
                    cout<<" "<<flush;

                    mx=mx+1;
                    gotoxy(mx,my);
                    printRobot(robot,x,y);
                    fillIn(x,y);
                    gotoxy(mx,my);



					gotoxy(mx1,my1);
                    cout<<" "<<flush;

                    mx1=mx1+1;
                    gotoxy(mx1,my1);
                    printRobot1(robot1,x,y);
                    fillIn(x,y);
                    gotoxy(mx1,my1);



					gotoxy(mx2,my2);
                    cout<<" "<<flush;

                    mx2=mx2+1;
                    gotoxy(mx2,my2);
                    printRobot2(robot2,x,y);
                    fillIn(x,y);
                    gotoxy(mx2,my2);
                }
            }

            direction = ' ';

      }

    }
 
    //  cout << "NOTE: MAKE SURE ALL ENTERED LETTERS ARE LOWER CASE!!!!!!!";
 
return 0;

}

 void printgrid_sky()
{
	char grid1[16][79];
	
	for(int i=0;i<16;i++)
	{
		for(int j=0;j<79;j++)
		{
			HANDLE hOut;
			hOut = GetStdHandle(STD_OUTPUT_HANDLE);
			SetConsoleTextAttribute(hOut, BACKGROUND_BLUE|
			BACKGROUND_INTENSITY);
			
			grid1[i][j]=' ';
			
			cout<<grid1[i][j]<<flush;
		}
		cout<<endl;
	}
}

void printgrid_land()

{

     for (i=0; i<7; i++)

     {

        for (j=0; j<79; j++)

        {
			    HANDLE hOut;
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hOut, BACKGROUND_GREEN|
    BACKGROUND_INTENSITY);


            cout<<grid[i][j];

        }

        cout<<endl;

     }

}

       

void printRobot(char robot[][3],int x, int y) 
{
   int r,c;


   for(c=0,r=2;c<HMAX;c++)
      cout<<robot[r][c]<<flush;

 
   gotoxy(x,y-1); 

   for(c=0,r=1;c<HMAX;c++) 
       cout<<robot[r][c]<<flush;

    gotoxy(x,y-2);

   for(c=0,r=0;c<HMAX;c++)
	   cout<<robot[r][c]<<flush;

}

 

void fillIn(int x,int y) 
{
   int i;

     for(i=VMAX;i>-1;i--) {

      gotoxy(x,y-i);

      cout<<" "<<flush;
 
   }

 

}
void printRobot1(char robot1[][3],int x, int y) 
{
   int r,c;


   for(c=0,r=2;c<HMAX;c++)
      cout<<robot1[r][c]<<flush;

 
   gotoxy(x,y-1); 

   for(c=0,r=1;c<HMAX;c++) 
       cout<<robot1[r][c]<<flush;

    gotoxy(x,y-2);

   for(c=0,r=0;c<HMAX;c++)
	   cout<<robot1[r][c]<<flush;

}
void printRobot2(char robot2[][3],int x, int y) 
{
   int r,c;


   for(c=0,r=2;c<HMAX;c++)
      cout<<robot2[r][c]<<flush;

 
   gotoxy(x,y-1); 

   for(c=0,r=1;c<HMAX;c++) 
       cout<<robot2[r][c]<<flush;

    gotoxy(x,y-2);

   for(c=0,r=0;c<HMAX;c++)
	   cout<<robot2[r][c]<<flush;

}

on line 160 your moving to the right by one
1
2
mx2=mx2+1;
gotoxy(mx2,my2);


on line 120 your moving to the left by two
1
2
mx2=mx2-2;
gotoxy(mx2,my2);


Idk if this is it or not cause I skimmed through your code kind of fast but this jumped out at me.
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
           if (direction == left_key)
            {
                if (mx > 0 && mx1 > 0 && mx2 > 0)
                {
                    gotoxy(mx,my);
                    cout<<" "<<flush;

                    mx=mx-1;
                    gotoxy(mx,my);

                    printRobot(robot,x,y);
                    fillIn(x,y);
                    gotoxy(mx,my);

                    gotoxy(mx1,my1);
                    cout<<" "<<flush;

                    mx1=mx1-1;
                    gotoxy(mx1,my1);

                    printRobot1(robot1,x,y);
                    fillIn(x,y);
                    gotoxy(mx1,my1);

                    gotoxy(mx2,my2);
                    cout<<" "<<flush;

                    mx2=mx2-2;
                    gotoxy(mx2,my2);

                    printRobot2(robot2,x,y);
                    fillIn(x,y);
                    gotoxy(mx2,my2);


Notice the bolded lines? Why are you using only one dimension of your multiple multi-dimensional arrays?
ok i corrected that. now when i move right and reach the end the robot gets out of the 79 column limit, how do i fix that?
somethings gone wrong! i dont know what i did but now the robot moves to the next row when it gets out of the 79 column limit. my code
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream.h>
#include<conio.h>
#include <windows.h>

 


 
const char left_key='a', right_key='d';
char grid[4][79];
int i, j;

void gotoxy(short x, short y)
{
     HANDLE hConsoleOutput;
     COORD Cursor = {x,y};
     hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
     SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
  

#define VMAX 3
#define HMAX 3

void printgrid_sky(); 
void printgrid_land();
void printRobot(char robot[][3],int x, int y);
void printRobot1(char robot1[][3],int x, int y);
void printRobot2(char robot2[][3],int x, int y);
void fillIn(int x,int y);

const int HEIGHT = 4;
const int WIDTH  = 79;

int main()

{
	int mx=10,mx1=10,mx2=10;
	int my=16,my1=17,my2=18;
	
	int x,y;
	char direction;
	char keypress;

    

	char robot[VMAX][HMAX]={'.','o','.'};
	char robot1[VMAX][HMAX]={'-','|','-'};
	char robot2[VMAX][HMAX]={'/',' ','\\'};

 

     

    
	printgrid_sky();
    printgrid_land();

    gotoxy(mx,my);
    printRobot(robot,x,y);

	gotoxy(mx1,my1);
    printRobot1(robot1,x,y);

	gotoxy(mx2,my2);
    printRobot2(robot2,x,y);
 

    while (mx<=(WIDTH-HMAX) && my<20 && my>0 && mx>=0 && mx1<=(WIDTH-HMAX) && my1<20 && my1>0 && mx1>=0 && mx2<=(WIDTH-HMAX) && my2<20 && my2>0 && mx2>=0)

    {

      if (kbhit())

      {

            keypress=getch();

             

            if ((keypress == right_key) || (keypress == left_key))

                 direction = keypress;

             
            if (direction == left_key)
            {
                if (mx > 0 && mx1 > 0 && mx2 > 0)
                {
                    gotoxy(mx,my);
                    cout<<" "<<flush;

                    mx=mx-1;
                    gotoxy(mx,my);

                    printRobot(robot,x,y);
                    fillIn(x,y);
                    gotoxy(mx,my);





					gotoxy(mx1,my1);
                    cout<<" "<<flush;

                    mx1=mx1-1;
                    gotoxy(mx1,my1);

                    printRobot1(robot1,x,y);
                    fillIn(x,y);
                    gotoxy(mx1,my1);



					
					gotoxy(mx2,my2);
                    cout<<" "<<flush;

                    mx2=mx2-1;
                    gotoxy(mx2,my2);

                    printRobot2(robot2,x,y);
                    fillIn(x,y);
                    gotoxy(mx2,my2);


                }
            }

            if (direction == right_key)
            {
                if (mx < (WIDTH - HMAX) && mx1 < (WIDTH - HMAX) && mx2 < (WIDTH - HMAX))
                {
                    gotoxy(mx,my);
                    cout<<" "<<flush;

                    mx=mx+1;
                    gotoxy(mx,my);
                    printRobot(robot,x,y);
                    fillIn(x,y);
                    gotoxy(mx,my);



					gotoxy(mx1,my1);
                    cout<<" "<<flush;

                    mx1=mx1+1;
                    gotoxy(mx1,my1);
                    printRobot1(robot1,x,y);
                    fillIn(x,y);
                    gotoxy(mx1,my1);



					gotoxy(mx2,my2);
                    cout<<" "<<flush;

                    mx2=mx2+1;
                    gotoxy(mx2,my2);
                    printRobot2(robot2,x,y);
                    fillIn(x,y);
                    gotoxy(mx2,my2);
                }
            }

            direction = ' ';

      }

    }

 
return 0;

}

 void printgrid_sky()
{
	char grid1[16][79];
	
	for(int i=0;i<16;i++)
	{
		for(int j=0;j<79;j++)
		{
			HANDLE hOut;
			hOut = GetStdHandle(STD_OUTPUT_HANDLE);
			SetConsoleTextAttribute(hOut, BACKGROUND_BLUE);
			
			grid1[i][j]=' ';
			
			cout<<grid1[i][j]<<flush;
		}
		cout<<endl;
	}
}

void printgrid_land()

{

     for (i=0; i<4; i++)

     {

        for (j=0; j<79; j++)

        {
			    HANDLE hOut;
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hOut, BACKGROUND_GREEN);


            cout<<grid[i][j];

        }

        cout<<endl;

     }

}

       

void printRobot(char robot[][3],int x, int y) 
{
   int r,c;


   for(c=0,r=2;c<HMAX;c++)
      cout<<robot[r][c]<<flush;

 
   gotoxy(x,y-1); 

   for(c=0,r=1;c<HMAX;c++) 
       cout<<robot[r][c]<<flush;

    gotoxy(x,y-2);

   for(c=0,r=0;c<HMAX;c++)
	   cout<<robot[r][c]<<flush;

}

 

void fillIn(int x,int y) 
{
   int i;

     for(i=VMAX;i>-1;i--) {

      gotoxy(x,y-i);

      cout<<" "<<flush;
 
   }

 

}
void printRobot1(char robot1[][3],int x, int y) 
{
   int r,c;


   for(c=0,r=2;c<HMAX;c++)
      cout<<robot1[r][c]<<flush;

 
   gotoxy(x,y-1); 

   for(c=0,r=1;c<HMAX;c++) 
       cout<<robot1[r][c]<<flush;

    gotoxy(x,y-2);

   for(c=0,r=0;c<HMAX;c++)
	   cout<<robot1[r][c]<<flush;

}
void printRobot2(char robot2[][3],int x, int y) 
{
   int r,c;


   for(c=0,r=2;c<HMAX;c++)
      cout<<robot2[r][c]<<flush;

 
   gotoxy(x,y-1); 

   for(c=0,r=1;c<HMAX;c++) 
       cout<<robot2[r][c]<<flush;

    gotoxy(x,y-2);

   for(c=0,r=0;c<HMAX;c++)
	   cout<<robot2[r][c]<<flush;

}

Topic archived. No new replies allowed.