Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of this project is to combine and modify the examples created and add s

ID: 3555335 • Letter: T

Question

The goal of this project is to combine and modify the examples created and add support for additional directions (updating all functions in the process as needed):

1.Add 2 additional directions: up and down. Use u and d for these commands.

2.Create a map consisting of 27 rooms. Think of a 3 by 3 cube. Dead ends are acceptable and expected but every room need to be reachable and the two new directions of up and down need to be used.

3.Make sure the overall maze is functional, meaning that the player starts in one location and may reach the end location.

4.Update and test (when the program launches) the path finding functions discussed in class (depth-first, breadth-first, shortest-path).

5.When the program launches the 3 path finding functions should run and provide some sort of output demonstrating their success. After this the normal game should start.

Original code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct room{
   char name[ 32 ];
   struct room * north;
   struct room * south;
   struct room * east;
   struct room * west;
  
   char visited; // Has the room been checked yet?
   int distance; // This is used by the shortest path alg.
   struct room * prev; // Used to show the path
  
   int type;
};

struct room_node{
   struct room * room;
   struct room_node * next;
};

struct room_node * queue = NULL;


void depth_first_search( struct room *, struct room * );
struct room_node * queue_remove();
void queue_insert( struct room_node * );
void breadth_first_search( struct room *, struct room * );
void clear_checks( struct room * [], int );
struct room * find_shortest_distance( struct room * [] );
void shortest_path( struct room * [], struct room * );
void show_path( struct room * );

int main( int argc, char * argv[] ){

   struct room * rooms[ 9 ];
  
   struct room * room01 = ( struct room * )malloc( sizeof( struct room ) );
   struct room * room02 = ( struct room * )malloc( sizeof( struct room ) );
   struct room * room03 = ( struct room * )malloc( sizeof( struct room ) );
   struct room * room04 = ( struct room * )malloc( sizeof( struct room ) );
   struct room * room05 = ( struct room * )malloc( sizeof( struct room ) );
   struct room * room06 = ( struct room * )malloc( sizeof( struct room ) );
   struct room * room07 = ( struct room * )malloc( sizeof( struct room ) );
   struct room * room08 = ( struct room * )malloc( sizeof( struct room ) );
   struct room * room09 = ( struct room * )malloc( sizeof( struct room ) );

   strcpy( room01->name, "Room One" );
   room01->north = NULL;
   room01->south = room04;
   room01->east = room02;
   room01->west = NULL;
   room01->visited = 0;
   room01->type = 0;
   rooms[ 0 ] = room01;
  
   strcpy( room02->name, "Room Two" );
   room02->north = NULL;
   room02->south = room05;
   room02->east = room03;
   room02->west = room01;
   room02->visited = 0;
   room02->type = 0;
   rooms[ 1 ] = room02;
  
   strcpy( room03->name, "Room Three" );
   room03->north = NULL;
   room03->south = NULL;
   room03->east = NULL;
   room03->west = room02;
   room03->visited = 0;
   room03->type = 0;
   rooms[ 2 ] = room03;
  
   strcpy( room04->name, "Room Four" );
   room04->north = room01;
   room04->south = NULL;
   room04->east = room05;
   room04->west = NULL;
   room04->visited = 0;
   room04->type = 0;
   rooms[ 3 ] = room04;
  
   strcpy( room05->name, "Room Five" );
   room05->north = room02;
   room05->south = room08;
   room05->east = room06;
   room05->west = room04;
   room05->visited = 0;
   room05->type = 3;
   rooms[ 4 ] = room05;
  
   strcpy( room06->name, "Room Six" );
   room06->north = NULL;
   room06->south = NULL;
   room06->east = NULL;
   room06->west = room05;
   room06->visited = 0;
   room06->type = 0;
   rooms[ 5 ] = room06;
  
   strcpy( room07->name, "Room Seven" );
   room07->north = NULL;
   room07->south = NULL;
   room07->east = NULL;
   room07->west = NULL;
   room07->visited = 0;
   room07->type = 2;
   rooms[ 6 ] = room07;
  
   strcpy( room08->name, "Room Eight" );
   room08->north = room05;
   room08->south = NULL;
   room08->east = room09;
   room08->west = NULL;
   room08->visited = 0;
   room08->type = 0;
   rooms[ 7 ] = room08;
  
   strcpy( room09->name, "Room Nine" );
   room09->north = NULL;
   room09->south = NULL;
   room09->east = NULL;
   room09->west = room08;
   room09->visited = 0;
   room09->type = 1;
   rooms[ 8 ] = room09;
  
   //depth_first_search( room01, room09 );
   //clear_checks( rooms, 9 );
   //breadth_first_search( room01, room09 );
  
   shortest_path( rooms, rooms[ 0 ] );
   show_path( room09 );
  
   return 0;
}

void depth_first_search( struct room * start, struct room * end ){
   int found = 0;
   struct room_node * stack = ( struct room_node * )malloc( sizeof( struct room_node ) );
   stack->room = start;
   stack->next = NULL;
   do{
       struct room_node * temp = stack;
       stack = stack->next;
       if( temp->room == end ){
           printf( "Found Solution in Room: %s ", end->name );
           found = 1;
       }else{
           if( !temp->room->visited ){
               temp->room->visited = 1;
               if( temp->room->north ){
                   struct room_node * north_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
                   north_temp->room = temp->room->north;
                   north_temp->next = stack;
                   stack = north_temp;
               }
               if( temp->room->south ){
                   struct room_node * south_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
                   south_temp->room = temp->room->south;
                   south_temp->next = stack;
                   stack = south_temp;
               }
               if( temp->room->east ){
                   struct room_node * east_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
                   east_temp->room = temp->room->east;
                   east_temp->next = stack;
                   stack = east_temp;
               }
               if( temp->room->west ){
                   struct room_node * west_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
                   west_temp->room = temp->room->west;
                   west_temp->next = stack;
                   stack = west_temp;
               }
           }
           free( temp );
       }
   }while( stack && !found );
   if( !found ){
       printf( "No Solution " );
   }
}

void queue_insert( struct room_node * r ){
   r->next = queue;
   queue = r;
}

struct room_node * queue_remove(){
   struct room_node * temp = queue;
   struct room_node * back = NULL;
   if( !temp ){
       return NULL;
   }else if( !temp->next ){
       queue = NULL;
       return temp;
   }else{
       while( temp->next ){
           back = temp;
           temp = temp->next;
       }
       back->next = NULL;
   }
   return temp;
}

void breadth_first_search( struct room * start, struct room * end ){
   int found = 0;
   struct room_node * new_node = (struct room_node *)malloc( sizeof( struct room_node ) );
   new_node->room = start;
   new_node->next = NULL;
   queue_insert( new_node );
   do{
       struct room_node * temp = queue_remove();
       if( temp->room == end ){
           printf( "Found solution " );
           found = 1;
       }else{
           if( !temp->room->visited ){
               temp->room->visited = 1;
               if( temp->room->north ){
                   struct room_node * north_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
                   north_temp->room = temp->room->north;
                   queue_insert( north_temp );
               }
               if( temp->room->south ){
                   struct room_node * south_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
                   south_temp->room = temp->room->south;
                   queue_insert( south_temp );
               }
               if( temp->room->east ){
                   struct room_node * east_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
                   east_temp->room = temp->room->east;
                   queue_insert( east_temp );
               }
               if( temp->room->west ){
                   struct room_node * west_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
                   west_temp->room = temp->room->west;
                   queue_insert( west_temp );
               }
           }
           free( temp );
       }
   }while( queue && !found );
   if( !found ){
       printf( "No Solution " );
   }
}

void clear_checks( struct room * rooms[], int size ){
   int i = 0;
   for( i = 0; i < size; i++ ){
       rooms[ i ]->visited = 0;
       rooms[ i ]->distance = 9999;
       rooms[ i ]->prev = NULL;
   }
}

void shortest_path( struct room * rooms[], struct room * start ){

   struct room * current_room = NULL;
   clear_checks( rooms, 9 );
   start->distance = 0;

   while( current_room = find_shortest_distance( rooms ) ){
       if( current_room->distance == 9999 ){
           break;
       }
       current_room->visited = 1;
       if( current_room->north ){
           int temp_distance = current_room->distance + 1;
           if( temp_distance < current_room->north->distance ){
               current_room->north->distance = temp_distance;
               current_room->north->prev = current_room;
           }
       }
       if( current_room->south ){
           int temp_distance = current_room->distance + 1;
           if( temp_distance < current_room->south->distance ){
               current_room->south->distance = temp_distance;
               current_room->south->prev = current_room;
           }
       }
       if( current_room->east ){
           int temp_distance = current_room->distance + 1;
           if( temp_distance < current_room->east->distance ){
               current_room->east->distance = temp_distance;
               current_room->east->prev = current_room;
           }
       }
       if( current_room->west ){
           int temp_distance = current_room->distance + 1;
           if( temp_distance < current_room->west->distance ){
               current_room->west->distance = temp_distance;
               current_room->west->prev = current_room;
           }
       }
   }
}

struct room * find_shortest_distance( struct room * rooms[] ){
   struct room * return_room = NULL;
   int i = 0;
   for( i = 0; i < 9; i++ ){
       if( ( !rooms[ i ]->visited ) && ( rooms[ i ]->distance < 9999 ) ){
           if( !return_room ){
               return_room = rooms[ i ];
           }else if( rooms[ i ]->distance < return_room->distance ){
               return_room = rooms[ i ];
           }
       }
   }
   return return_room;
}

void show_path( struct room * end ){
   if( end->prev ){
       show_path( end->prev );
   }
   printf( "%s ", end->name );
}

Explanation / Answer

I don't know what the game is? But implement the five parts above.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct room{
char name[ 32 ];
struct room * north;
struct room * south;
struct room * east;
struct room * west;
struct room * up;
struct room * down;
  
char visited; // Has the room been checked yet?
int distance; // This is used by the shortest path alg.
struct room * prev; // Used to show the path
  
int type;
};
struct room_node{
struct room * room;
struct room_node * next;
};
struct room_node * queue = NULL;

void depth_first_search( struct room *, struct room * );
struct room_node * queue_remove();
void queue_insert( struct room_node * );
void breadth_first_search( struct room *, struct room * );
void clear_checks( struct room * [], int );
struct room * find_shortest_distance( struct room * [] );
void shortest_path( struct room * [], struct room * );
void show_path( struct room * );
int main( int argc, char * argv[] ){
struct room * rooms[ 27 ];
  
struct room * room01 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room02 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room03 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room04 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room05 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room06 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room07 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room08 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room09 = ( struct room * )malloc( sizeof( struct room ) );

struct room * room10 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room11 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room12 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room13 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room14 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room15 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room16 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room17 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room18 = ( struct room * )malloc( sizeof( struct room ) );

struct room * room19 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room20 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room21 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room22 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room23 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room24 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room25 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room26 = ( struct room * )malloc( sizeof( struct room ) );
struct room * room27 = ( struct room * )malloc( sizeof( struct room ) );

strcpy( room01->name, "Room One" );
room01->north = NULL;
room01->south = room04;
room01->east = room02;
room01->west = NULL;
room01->up = room10;
room01->down = NULL;
room01->visited = 0;
room01->type = 0;
rooms[ 0 ] = room01;
  
strcpy( room02->name, "Room Two" );
room02->north = NULL;
room02->south = room05;
room02->east = room03;
room02->west = room01;
room02->up = room11;
room02->down = NULL;
room02->visited = 0;
room02->type = 0;
rooms[ 1 ] = room02;
  
strcpy( room03->name, "Room Three" );
room03->north = NULL;
room03->south = NULL;
room03->east = NULL;
room03->west = room02;
room03->up = room12;
room03->down = NULL;
room03->visited = 0;
room03->type = 0;
rooms[ 2 ] = room03;
  
strcpy( room04->name, "Room Four" );
room04->north = room01;
room04->south = NULL;
room04->east = room05;
room04->west = NULL;
room04->up = room13;
room04->down = NULL;
room04->visited = 0;
room04->type = 0;
rooms[ 3 ] = room04;
  
strcpy( room05->name, "Room Five" );
room05->north = room02;
room05->south = room08;
room05->east = room06;
room05->west = room04;
room05->up = room14;
room05->down = NULL;
room05->visited = 0;
room05->type = 3;
rooms[ 4 ] = room05;
  
strcpy( room06->name, "Room Six" );
room06->north = NULL;
room06->south = NULL;
room06->east = NULL;
room06->west = room05;
room06->up = room15;
room06->down = NULL;
room06->visited = 0;
room06->type = 0;
rooms[ 5 ] = room06;
  
strcpy( room07->name, "Room Seven" );
room07->north = NULL;
room07->south = NULL;
room07->east = NULL;
room07->west = NULL;
room07->up = room16;
room07->down = NULL;
room07->visited = 0;
room07->type = 2;
rooms[ 6 ] = room07;
  
strcpy( room08->name, "Room Eight" );
room08->north = room05;
room08->south = NULL;
room08->east = room09;
room08->west = NULL;
room08->up = room17;
room08->down = NULL;
room08->visited = 0;
room08->type = 0;
rooms[ 7 ] = room08;
  
strcpy( room09->name, "Room Nine" );
room09->north = NULL;
room09->south = NULL;
room09->east = NULL;
room09->west = room08;
room09->up = room18;
room09->down = NULL;
room09->visited = 0;
room09->type = 1;
rooms[ 8 ] = room09;

strcpy( room10->name, "Room Ten" );
room10->north = NULL;
room10->south = room13;
room10->east = room11;
room10->west = NULL;
room10->up = room19;
room10->down = room01;
room10->visited = 0;
room10->type = 0;
rooms[ 9 ] = room10;
  
strcpy( room11->name, "Room Eleven" );
room11->north = NULL;
room11->south = room14;
room11->east = room12;
room11->west = room10;
room11->up = room20;
room11->down = room02;
room11->visited = 0;
room11->type = 0;
rooms[ 10 ] = room11;
  
strcpy( room12->name, "Room Twelve" );
room12->north = NULL;
room12->south = NULL;
room12->east = NULL;
room12->west = room02;
room12->up = room21;
room12->down = room03;
room12->visited = 0;
room12->type = 0;
rooms[ 11 ] = room12;
  
strcpy( room13->name, "Room Thirteen" );
room13->north = room10;
room13->south = NULL;
room13->east = room14;
room13->west = NULL;
room13->up = room22;
room13->down = room04;
room13->visited = 0;
room13->type = 0;
rooms[ 12 ] = room13;
  
strcpy( room14->name, "Room Fourteen" );
room14->north = room11;
room14->south = room17;
room14->east = room15;
room14->west = room13;
room14->up = room23;
room14->down = room05;
room14->visited = 0;
room14->type = 3;
rooms[ 13 ] = room14;
  
strcpy( room15->name, "Room Fifteen" );
room15->north = NULL;
room15->south = NULL;
room15->east = NULL;
room15->west = room14;
room15->up = room24;
room15->down = room06;
room15->visited = 0;
room15->type = 0;
rooms[ 14 ] = room15;
  
strcpy( room16->name, "Room Sixteen" );
room16->north = NULL;
room16->south = NULL;
room16->east = NULL;
room16->west = NULL;
room16->up = room25;
room16->down = room07;
room16->visited = 0;
room16->type = 2;
rooms[ 15 ] = room16;
  
strcpy( room17->name, "Room Seventeen" );
room17->north = room14;
room17->south = NULL;
room17->east = room18;
room17->west = NULL;
room17->up = room26;
room17->down = room08;
room17->visited = 0;
room17->type = 0;
rooms[ 16 ] = room17;
  
strcpy( room18->name, "Room Eighteen" );
room18->north = NULL;
room18->south = NULL;
room18->east = NULL;
room18->west = room17;
room18->up = room27;
room18->down = room09;
room18->visited = 0;
room18->type = 1;
rooms[ 17 ] = room18;

strcpy( room19->name, "Room Ninteen" );
room19->north = NULL;
room19->south = room22;
room19->east = room20;
room19->west = NULL;
room19->up = NULL;
room19->down = room10;
room19->visited = 0;
room19->type = 0;
rooms[ 18 ] = room19;
  
strcpy( room20->name, "Room Twenty" );
room20->north = NULL;
room20->south = room23;
room20->east = room21;
room20->west = room19;
room20->up = NULL;
room20->down = room11;
room20->visited = 0;
room20->type = 0;
rooms[ 19 ] = room20;
  
strcpy( room21->name, "Room Twenty One" );
room21->north = NULL;
room21->south = NULL;
room21->east = NULL;
room21->west = room20;
room21->up = NULL;
room21->down = room12;
room21->visited = 0;
room21->type = 0;
rooms[ 20 ] = room21;
  
strcpy( room22->name, "Room Twenty Two" );
room22->north = room19;
room22->south = NULL;
room22->east = room23;
room22->west = NULL;
room22->up = NULL;
room22->down = room13;
room22->visited = 0;
room22->type = 0;
rooms[ 21 ] = room22;
  
strcpy( room23->name, "Room Twenty Three" );
room23->north = room20;
room23->south = room26;
room23->east = room24;
room23->west = room22;
room23->up = NULL;
room23->down = room14;
room23->visited = 0;
room23->type = 3;
rooms[ 22 ] = room23;
  
strcpy( room24->name, "Room Twenty Four" );
room24->north = NULL;
room24->south = NULL;
room24->east = NULL;
room24->west = room23;
room24->up = NULL;
room24->down = room15;
room24->visited = 0;
room24->type = 0;
rooms[ 23 ] = room24;
  
strcpy( room25->name, "Room Twenty Five" );
room25->north = NULL;
room25->south = NULL;
room25->east = NULL;
room25->west = NULL;
room25->up = NULL;
room25->down = room16;
room25->visited = 0;
room25->type = 2;
rooms[ 24 ] = room25;
  
strcpy( room26->name, "Room Twenty Six" );
room26->north = room23;
room26->south = NULL;
room26->east = room27;
room26->west = NULL;
room26->up = NULL;
room26->down = room17;
room26->visited = 0;
room26->type = 0;
rooms[ 25 ] = room26;
  
strcpy( room27->name, "Room Twenty Seven" );
room27->north = NULL;
room27->south = NULL;
room27->east = NULL;
room27->west = room26;
room27->up = NULL;
room27->down = room18;
room27->visited = 0;
room27->type = 1;
rooms[ 26 ] = room27;
  
depth_first_search( room01, room27 );
clear_checks( rooms, 27 );
breadth_first_search( room01, room27 );
  
shortest_path( rooms, rooms[ 0 ] );
show_path( room27 );
  
return 0;
}
void depth_first_search( struct room * start, struct room * end ){
int found = 0;
struct room_node * stack = ( struct room_node * )malloc( sizeof( struct room_node ) );
stack->room = start;
stack->next = NULL;
do{
struct room_node * temp = stack;
stack = stack->next;
if( temp->room == end ){
printf( "Found Solution in Room: %s ", end->name );
found = 1;
}else{
if( !temp->room->visited ){
temp->room->visited = 1;
if( temp->room->north ){
struct room_node * north_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
north_temp->room = temp->room->north;
north_temp->next = stack;
stack = north_temp;
}
if( temp->room->south ){
struct room_node * south_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
south_temp->room = temp->room->south;
south_temp->next = stack;
stack = south_temp;
}
if( temp->room->east ){
struct room_node * east_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
east_temp->room = temp->room->east;
east_temp->next = stack;
stack = east_temp;
}
if( temp->room->west ){
struct room_node * west_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
west_temp->room = temp->room->west;
west_temp->next = stack;
stack = west_temp;
}
if( temp->room->up ){
struct room_node * up_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
up_temp->room = temp->room->up;
up_temp->next = stack;
stack = up_temp;
}
if( temp->room->down ){
struct room_node * down_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
down_temp->room = temp->room->down;
down_temp->next = stack;
stack = down_temp;
}
}
free( temp );
}
}while( stack && !found );
if( !found ){
printf( "No Solution " );
}
}
void queue_insert( struct room_node * r ){
r->next = queue;
queue = r;
}
struct room_node * queue_remove(){
struct room_node * temp = queue;
struct room_node * back = NULL;
if( !temp ){
return NULL;
}else if( !temp->next ){
queue = NULL;
return temp;
}else{
while( temp->next ){
back = temp;
temp = temp->next;
}
back->next = NULL;
}
return temp;
}
void breadth_first_search( struct room * start, struct room * end ){
int found = 0;
struct room_node * new_node = (struct room_node *)malloc( sizeof( struct room_node ) );
new_node->room = start;
new_node->next = NULL;
queue_insert( new_node );
do{
struct room_node * temp = queue_remove();
if( temp->room == end ){
printf( "Found solution " );
found = 1;
}else{
if( !temp->room->visited ){
temp->room->visited = 1;
if( temp->room->north ){
struct room_node * north_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
north_temp->room = temp->room->north;
queue_insert( north_temp );
}
if( temp->room->south ){
struct room_node * south_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
south_temp->room = temp->room->south;
queue_insert( south_temp );
}
if( temp->room->east ){
struct room_node * east_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
east_temp->room = temp->room->east;
queue_insert( east_temp );
}
if( temp->room->west ){
struct room_node * west_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
west_temp->room = temp->room->west;
queue_insert( west_temp );
}
if( temp->room->up ){
struct room_node * up_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
up_temp->room = temp->room->up;
queue_insert( up_temp );
}
if( temp->room->down ){
struct room_node * down_temp = (struct room_node *)malloc( sizeof( struct room_node ) );
down_temp->room = temp->room->down;
queue_insert( down_temp );
}
}
free( temp );
}
}while( queue && !found );
if( !found ){
printf( "No Solution " );
}
}
void clear_checks( struct room * rooms[], int size ){
int i = 0;
for( i = 0; i < size; i++ ){
rooms[ i ]->visited = 0;
rooms[ i ]->distance = 9999;
rooms[ i ]->prev = NULL;
}
}
void shortest_path( struct room * rooms[], struct room * start ){
struct room * current_room = NULL;
clear_checks( rooms, 27 );
start->distance = 0;
while( current_room = find_shortest_distance( rooms ) ){
if( current_room->distance == 9999 ){
break;
}
current_room->visited = 1;
if( current_room->north ){
int temp_distance = current_room->distance + 1;
if( temp_distance < current_room->north->distance ){
current_room->north->distance = temp_distance;
current_room->north->prev = current_room;
}
}
if( current_room->south ){
int temp_distance = current_room->distance + 1;
if( temp_distance < current_room->south->distance ){
current_room->south->distance = temp_distance;
current_room->south->prev = current_room;
}
}
if( current_room->east ){
int temp_distance = current_room->distance + 1;
if( temp_distance < current_room->east->distance ){
current_room->east->distance = temp_distance;
current_room->east->prev = current_room;
}
}
if( current_room->west ){
int temp_distance = current_room->distance + 1;
if( temp_distance < current_room->west->distance ){
current_room->west->distance = temp_distance;
current_room->west->prev = current_room;
}
}
if( current_room->up ){
int temp_distance = current_room->distance + 1;
if( temp_distance < current_room->up->distance ){
current_room->up->distance = temp_distance;
current_room->up->prev = current_room;
}
}
if( current_room->down ){
int temp_distance = current_room->distance + 1;
if( temp_distance < current_room->down->distance ){
current_room->down->distance = temp_distance;
current_room->down->prev = current_room;
}
}
}
}
struct room * find_shortest_distance( struct room * rooms[] ){
struct room * return_room = NULL;
int i = 0;
for( i = 0; i < 27; i++ ){
if( ( !rooms[ i ]->visited ) && ( rooms[ i ]->distance < 9999 ) ){
if( !return_room ){
return_room = rooms[ i ];
}else if( rooms[ i ]->distance < return_room->distance ){
return_room = rooms[ i ];
}
}
}
return return_room;
}
void show_path( struct room * end ){
if( end->prev ){
show_path( end->prev );
}
printf( "%s ", end->name );
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote