ibanezzaro

Untitled

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