Advertisement
ibanezzaro

Maze

Jan 19th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stddef.h>
  4. #define maxRow 12
  5. #define maxCol 12
  6.  
  7. //DEVO FARLO CON GLI IF NESTATI DIO CANEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE, CIAO DARIO DI DOMANI
  8.  
  9. char labyrinth[maxRow][maxCol] = { {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
  10.                                             {'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'},
  11.                                             {'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'},
  12.                                             {'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
  13.                                             {'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', 'c'},
  14.                                             {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
  15.                                             {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
  16.                                             {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
  17.                                             {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},
  18.                                             {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},
  19.                                             {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
  20.                                             {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'} };
  21.  
  22. int i;
  23. int j;
  24.  
  25. typedef struct location{
  26.     int i;
  27.     int j;
  28.     int entr;
  29. } t_location ;
  30.  
  31. t_location entrance_location(t_location);
  32. void maze_solver(t_location);
  33. void find_entrance();
  34. void find_entrance_updown();
  35. void print_maze();
  36. int finding_point(int);
  37. t_location move_up (t_location);
  38. t_location move_down(t_location);
  39. t_location move_left(t_location);
  40. t_location move_right(t_location);
  41. t_location evaluate_situation(t_location);
  42.  
  43.  
  44. int main()
  45. {
  46.  
  47.     t_location current = {0, 0, 0};
  48.     print_maze();
  49.     find_entrance();
  50.     entrance_location(current);
  51.     printf("%d, %d, %d", current.i, current.j, current.entr);
  52.  
  53.  
  54.  
  55. };
  56.  
  57. void print_maze()
  58. {
  59.  
  60.     for(i = 0; i < maxRow; ++i){
  61.         for(j = 0; j < maxCol; ++j){
  62.             printf("%c\t", labyrinth[i][j]);
  63.         }
  64.         printf("\n");
  65.     }
  66.  
  67. }
  68.  
  69. void find_entrance ()
  70. {
  71.     int flag = 0;
  72.     for(j = 0; j < maxCol; j+=maxCol - 1){
  73.         for(i = 0; i < maxRow; i++){
  74.             finding_point(flag);
  75.  
  76.         }
  77.     }
  78.     if(flag == 0) {
  79.         find_entrance_updown();
  80.     }
  81. }
  82.  
  83. void find_entrance_updown (int flag) // still to implement case in which there's no entrance
  84. {                                    // just need to make findentranceupdown output flag0 and create an if(no entrance)
  85.     for(i = 0; i < maxRow; i+=11){
  86.             for(j = 0; j < maxCol; j++){
  87.                 finding_point(flag);
  88.         }
  89.     }
  90.  
  91. }
  92.  
  93. int finding_point (int flag)
  94. {
  95.     if(labyrinth[i][j] == '.') {
  96.                         printf("Found the entrance\n");
  97.                         labyrinth[i][j] = 'X';
  98.                         flag++;
  99.                         print_maze();
  100.                 }
  101. return flag;
  102. }
  103.  
  104. void maze_solver(t_location current)
  105. {
  106.  
  107.     if(current.entr == 1){
  108.         move_down(current);
  109.     }
  110.     else if(current.entr == 2){
  111.         move_up(current);
  112.     }
  113.     else if(current.entr == 3){
  114.         move_right(current);
  115.     }
  116.     else if(current.entr == 4){
  117.         move_left(current);
  118.     }
  119.  
  120.  
  121.     while(labyrinth[current.i][current.j] != 'c'){
  122.         evaluate_situation(current);
  123.     }
  124.  
  125.  
  126.  
  127. }
  128.  
  129. t_location entrance_location (t_location current)
  130. {
  131.     for( i = 0; i < maxRow; i++) {
  132.         for( j = 0; j < maxCol; j++){
  133.             if(labyrinth[i][j] == 'X'){
  134.                 current.i = i;
  135.                 current.j = j;
  136.                 //questo printf mi assicura che current รจ up to date: printf("%d, %d, %d", current.i, current.j, current.entr);
  137.                 printf("Entrance coord:%d, %d\n", i ,j);
  138.                 if(labyrinth[0][j]){ //sopra
  139.                 current.entr = 1;
  140.                 }
  141.                 else if (labyrinth[11][j]){ //sotto
  142.                 current.entr = 2;
  143.                 }
  144.                 else if( labyrinth[i][0]){ //sinistra
  145.                 current.entr = 3;
  146.                 }
  147.                 else if(labyrinth[i][11]){  //destra
  148.                 current.entr = 4;
  149.                 }
  150.             }
  151.         }
  152.     }
  153. return current;
  154. }
  155.  
  156.  
  157. t_location move_up(t_location current)  //devo ricordarmi di settare gli indici all'entrata prima di lanciare move_up
  158. {
  159.         i = current.i;
  160.         j = current.j;
  161.         if(labyrinth[i+1][j] == '.'){
  162.         labyrinth[i+1][j] = 'X';
  163.         print_maze();
  164.         current.i = i++;
  165.         }
  166.     return current;
  167. }
  168.  
  169. t_location move_down(t_location current)  //devo ricordarmi di settare gli indici all'entrata prima di lanciare move_up
  170. {
  171.     i = current.i;
  172.     j = current.j;
  173.     if(labyrinth[i-1][j] == '.'){
  174.     labyrinth[i-1][j] = 'X';
  175.     print_maze();
  176.     current.i = i--;
  177.     }
  178.     return current;
  179. }
  180.  
  181. t_location move_right(t_location current)  //devo ricordarmi di settare gli indici all'entrata prima di lanciare move_up
  182. {
  183.     i = current.i;
  184.     j = current.j;
  185.     if(labyrinth[i][j+1] == '.'){
  186.     labyrinth[i][j+1] = 'X';
  187.     print_maze();
  188.     current.j = j++;
  189.     } //adesso deve controllare che sia possibile il movimento a destra
  190.     return current;
  191. }
  192.  
  193. t_location move_left(t_location current)  //devo ricordarmi di settare gli indici all'entrata prima di lanciare move_up
  194. {
  195.     i = current.i;
  196.     j = current.j;
  197.     if(labyrinth[i][j-1] == '.'){
  198.     labyrinth[i][j-1] = 'X';
  199.     print_maze();
  200.     current.j = j--;
  201.     } //adesso deve controllare che sia possibile il movimento a destra
  202.     return current;
  203. }
  204.  
  205.  
  206.  
  207. t_location evaluate_situation(t_location current)
  208. {
  209.     i = current.i;
  210.     j = current.j;
  211.  
  212.     if((labyrinth[i+1][j] == 'X') && (labyrinth[i-1][j] == '.') && (labyrinth[i-1][j+1] == '.')){ //ricordati il caso in cui trova C
  213.             move_up(current);
  214.             move_right(current);
  215.     }
  216.     else if((labyrinth[i+1][j] == 'X') && (labyrinth[i-1][j] == '.') && ( labyrinth[i-1][j-1] == '.') && (labyrinth[i-1][j+1] != '.')){
  217.             move_up(current);
  218.             move_left(current);
  219.     }
  220.     else if((labyrinth[i+1][j] == 'X') && (labyrinth[i-1][j-1] == '#') && ( labyrinth[i-1][j+1] == '#')){
  221.             move_up(current);
  222.     }
  223.     //down movements
  224.     else if((labyrinth[i-1][j] == 'X') && (labyrinth[i+1][j] == '.') && (labyrinth[i+1][j-1] == '.')){
  225.             move_down(current);
  226.             move_left(current);
  227.     }
  228.     else if((labyrinth[i-1][j] == 'X') && (labyrinth[i+1][j] == '.') && ( labyrinth[i+1][j+1] == '.') && (labyrinth[i+1][j-1] != '.')){
  229.             move_down(current);
  230.             move_right(current);
  231.     }
  232.     else if((labyrinth[i-1][j] == 'X') && (labyrinth[i+1][j-1] == '#') && ( labyrinth[i+1][j+1] == '#')){
  233.             move_down(current);
  234.     }
  235.     //right movements
  236.     else if((labyrinth[i][j-1] == 'X') && (labyrinth[i][j+1] == '.') && (labyrinth[i+1][j+1] == '.')){
  237.             move_right(current);
  238.             move_down(current);
  239.     }
  240.     else if((labyrinth[i][j-1] == 'X') && (labyrinth[i][j+1] == '.') && ( labyrinth[i-1][j+1] == '.') && (labyrinth[i+1][j+1] == '#')){
  241.             move_right(current);
  242.             move_up(current);
  243.     }
  244.     else if((labyrinth[i][j-1] == 'X') && (labyrinth[i-1][j+1] == '#') && ( labyrinth[i+1][j+1] == '#')){
  245.             move_right(current);
  246.     }
  247.     //left movements
  248.     else if((labyrinth[i][j+1] == 'X') && (labyrinth[i][j-1] == '.') && (labyrinth[i-1][j-1] == '.')){
  249.             move_left(current);
  250.             move_up(current);
  251.     }
  252.     else if((labyrinth[i][j+1] == 'X') && (labyrinth[i][j-1] == '.') && (labyrinth[i+1][j-1] == '.') && (labyrinth[i-1][j-1] == '#')){
  253.             move_left(current);
  254.             move_down(current);
  255.     }
  256.     else if((labyrinth[i][j+1] == 'X') && (labyrinth[i-1][j-1] == '#') && (labyrinth[i+1][j-1] == '#')){
  257.             move_left(current);
  258.     }
  259.  
  260. return current;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement