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
|
void Path::next(Node nodes[],Node dummy){
int num_of_neighbors = 0;
current = path[size-1];
for(int i = 0; i <192;i++){
if((nodes[i].x == current.x)&&(nodes[i].y == current.y-50)&&(nodes[i].state !=1)){//find northern neighbor
// apply_surface( nodes[i].x, nodes[i].y, tile_two, screen);
neighbors[0] = nodes[i];
num_of_neighbors++;
}
if((nodes[i].x == current.x)&&(nodes[i].y == current.y+50)&&(nodes[i].state !=1)){//find southern neighbor
// apply_surface( nodes[i].x, nodes[i].y, tile_two, screen);
neighbors[1] = nodes[i];
num_of_neighbors++;}
if((nodes[i].x == current.x-50)&&(nodes[i].y == current.y)&&(nodes[i].state !=1)){//find eastern neighbor
//apply_surface( nodes[i].x, nodes[i].y, tile_two, screen);
neighbors[2] = nodes[i];
num_of_neighbors++;}
if((nodes[i].x == current.x+50)&&(nodes[i].y == current.y)&&(nodes[i].state !=1)){//find western neighbor
//apply_surface( nodes[i].x, nodes[i].y, tile_two, screen);
neighbors[3] = nodes[i];
num_of_neighbors++;}
if((nodes[i].x == current.x-50)&&(nodes[i].y == current.y-50)&&(nodes[i].state !=1)){//north-western neighbor
//apply_surface( nodes[i].x, nodes[i].y, tile_two, screen);
neighbors[4] = nodes[i];
num_of_neighbors++;}
if((nodes[i].x == current.x-50)&&(nodes[i].y == current.y+50)&&(nodes[i].state !=1)){//south-western neighbor
//apply_surface( nodes[i].x, nodes[i].y, tile_two, screen);
neighbors[6] = nodes[i];
num_of_neighbors++;}
if((nodes[i].x == current.x+50)&&(nodes[i].y == current.y+50)&&(nodes[i].state !=1)){//south-eastern neighbor
//apply_surface( nodes[i].x, nodes[i].y, tile_two, screen);
neighbors[7] = nodes[i];
num_of_neighbors++;}
if((nodes[i].x == current.x+50)&&(nodes[i].y == current.y-50)&&(nodes[i].state !=1)){//north eastern neighbor
//apply_surface( nodes[i].x, nodes[i].y, tile_two, screen);
neighbors[5] = nodes[i];
num_of_neighbors++;}
//cout<<"i have "<<num_of_neighbors << " ";
}
if(num_of_neighbors > 0){
int small = 0;
for(int i = 0; i < 8; i++){
if(neighbors[i].in_path != 1){
small = i;}}
for(int i = 0; i<8; i++){
if(neighbors[i].state == 1){//if the neighbor is a wall, replace it with the dummy node.
neighbors[i] = dummy;
}
if(distance(neighbors[i]) <distance(neighbors[small])){//if distence of current node in neighbors is smaller than distance of node in neighbors list at small
int ok = 1;
for(int n = 0; n<size; n++){//this for loop finds out if neighbors[i] is already in the path list
if(neighbors[i].my_index == path[n].my_index){
ok = 0;}}
if(ok == 1){
small = i;
}}}
if(goal.my_index != current.my_index){
size++;
path.resize(size);
path[size-1]=neighbors[small];
nodes[neighbors[small].my_index].state = 1;
apply_surface( neighbors[small].x, neighbors[small].y, tile_two, screen);
}
for(int i = 0; i<8; i++){
cout<<current.my_index<<endl;}
}
else{
current.state = 2;
//apply_surface( current.x, current.y, wall, screen);
size -= 1;
path.resize(size);
}
memset (neighbors,NULL,8);
num_of_neighbors = 0;
}
};
Path::Path(){
index = 0;
size = 0;
finished = 0;
}
|