In this program, there is a 6 x 6 map constructed using 2 dimensional array. The aim of this program is to navigate an object through the obstacles. Each coordinate contains a value, each value represents the distance between the object and its destination if it is to be the object's new coordinate. The initial coordinate of the object is (0,5) and its destination coordinate is (5,0). Value 50 means an obstacle. The object should choose the next coordinate that contains the smallest value. The value of the destination coordinate is 0. This is the map:
0 | 1 | 2 | 3 | 4 | 5
0| 7 | 50| 3 | 2 | 1 | 0
1| 6 | 5 | 4 | 3 | 2 | 1
2| 7 | 6 | 5 | 50| 50| 50
3| 8 | 7 | 6 | 7 | 8 | 9
4| 50| 50| 50| 50| 50| 10
5| 0 | 15| 14| 13| 12| 11
The program is suppose to output a series of coordinates from (0,5) to (5,0).
#include<stdio.h>
#include<stdlib.h>
//These are the functions to move the object through the map.
void moveForward(int new_c[1][2]);
void moveBackward(int new_c[1][2]);
void moveLeft(int new_c[1][2]);
void moveRight(int new_c[1][2]);
int main()
{
int map[6][6]={{7,50,3,2,1,0},
{6,5,4,3,2,1},
{7,6,5,50,50,50},
{8,7,6,7,8,9},
{50,50,50,50,50,10},
{0,15,14,13,12,11}};
int coordinate[1][2]={{0,5}};
int x = 0;
int y = 5;
printf("(%d,%d),",x,y);
while(x!=5&&y!=0)
{
if(map[y-1][x] < map[y+1][x] && //Forward < Backward
map[y-1][x] < map[y][x-1] && //Forward < Left
map[y-1][x] < map[y][x+1] && //Forward < Right
y - 1 >= 0 &&
y + 1 <= 5 &&
x - 1 >= 0 &&
x + 1 <= 5)
{
moveForward(coordinate);
y = y - 1;
}
else
{
if(map[y+1][x] < map[y-1][x] && //Backward < Forward
map[y+1][x] < map[y][x-1] && //Backward < Left
map[y+1][x] < map[y][x+1] && //Backward < Right
y - 1 >= 0 &&
y + 1 <= 5 &&
x - 1 >= 0 &&
x + 1 <= 5)
{
moveBackward(coordinate);
y = y + 1;
}
else
{
if(map[y][x-1] < map[y][x+1] && //Left < Right
map[y][x-1] < map[y+1][x] && //Left < Backward
map[y][x-1] < map[y-1][x] && //Left < Forward
y - 1 >= 0 &&
y + 1 <= 5 &&
x - 1 >= 0 &&
x + 1 <= 5)
{
moveLeft(coordinate);
x = x + 1;
}
else
{
if(map[y][x+1] < map[y][x-1] && //Right < Left
map[y][x+1] < map[y+1][x] && //Right < B
map[y][x+1] < map[y-1][x] && //Right < F
y - 1 >= 0 &&
y + 1 <= 5 &&
x - 1 >= 0 &&
x + 1 <= 5)
{
moveRight(coordinate);
x = x - 1;
}
}
}
}
}
return 0;
}
void moveForward(int new_c[1][2])
{ //This modifies the y coordinate.
new_c[0][1] = new_c[0][1] - 1;
printf("(%d,%d),", new_c[0][0], new_c[0][1]);
}
void moveBackward(int new_c[1][2])
{ //This modifies the y coordinate.
new_c[0][1] = new_c[0][1] + 1;
printf("(%d,%d),", new_c[0][0], new_c[0][1]);
}
void moveLeft(int new_c[1][2])
{ //This modifies the x coordinate.
new_c[0][0] = new_c[0][0] - 1;
printf("(%d,%d),", new_c[0][0], new_c[0][1]);
}
void moveRight(int new_c[1][2])
{ //This modifies the x coordinate.
new_c[0][0] = new_c[0][0] + 1;
printf("(%d,%d),", new_c[0][0], new_c[0][1]);
}
Output:
(0,5),
Then the cursor just blinking like usual. There's no statement on the bottom telling returned value or execution time.
if(map[y][x+1]<map[y][x-1]&&map[y][x+1]<map[y+1][x]&&map[y][x+1]<map[y-1][x]&&y-1>=0&&y+1<=5&&x-1>=0&&x+1<=5)That looks pretty wrong to me. Do you actually know what this line of code is supposed to do? I sure don't, and I'd delete it and start over.