Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. #include "cub.h"
  2.  
  3.  
  4. int     parsing_map(t_cub *cub, char *line)
  5. {
  6.     int size_map;
  7.     cub->parse.map[cub->parse.i++] = ft_strdup(line);
  8.     return 1;
  9. }
  10.  
  11. int find_in(char c, char *str)
  12. {
  13.     int i;
  14.  
  15.     i = 0;
  16.     while (str[i])
  17.     {
  18.         if (str[i] == c)
  19.         {
  20.             return 1;
  21.         }
  22.         i++;
  23.     }
  24.     return -1;
  25. }
  26.  
  27. int check_zr(t_cub cub, int i, int j)
  28. {
  29.     char *s;
  30.  
  31.     s = ft_strdup("012");
  32.     if ((find_in(cub.parse.map[j][i - 1], s) == 1)
  33.         && (find_in(cub.parse.map[j][i + 1], s) == 1)
  34.         && (find_in(cub.parse.map[j - 1][i], s) == 1)
  35.         && (find_in(cub.parse.map[j + 1][i], s) == 1))
  36.     {
  37.         return 1;
  38.     }
  39.     else
  40.         return 0;
  41. }
  42.  
  43. int check_sp(t_cub cub, int i, int j)
  44. {
  45.     char *s;
  46.  
  47.     s = ft_strdup("1 ");
  48.     if ((find_in(cub.parse.map[j][i - 1], s) == 1)
  49.         && (find_in(cub.parse.map[j][i + 1], s) == 1)
  50.         && (find_in(cub.parse.map[j - 1][i], s) == 1)
  51.         && (find_in(cub.parse.map[j + 1][i], s) == 1))
  52.     {
  53.         return 1;
  54.     }
  55.     else
  56.         return 0;
  57. }
  58.  
  59. int check_one(t_cub cub, int i, int j)
  60. {
  61.     char *s;
  62.  
  63.     s = ft_strdup("012");
  64.     if ((find_in(cub.parse.map[j][i - 1], s) == 1)
  65.         && (find_in(cub.parse.map[j][i + 1], s) == 1)
  66.         && (find_in(cub.parse.map[j - 1][i], s) == 1)
  67.         && (find_in(cub.parse.map[j + 1][i], s) == 1))
  68.     {
  69.         return 1;
  70.     }
  71.     else
  72.         return 0;
  73. }
  74.  
  75. int     check_around(t_cub cub, char c, int i, int j)
  76. {
  77.     if (cub.parse.map[j][i - 1] == c ||
  78.         cub.parse.map[j][i + 1] == c ||
  79.         cub.parse.map[j - 1][i] == c ||
  80.         cub.parse.map[j + 1][i] == c)
  81.     {
  82.         return 1;
  83.     }
  84.     else
  85.         return 0;
  86. }
  87.  
  88. void    line_check(t_cub cub, int j)
  89. {
  90.     int i;
  91.  
  92.     i = 0;
  93.     while (cub.parse.map[j][i])
  94.     {
  95.         if (cub.parse.map[j][i] == ' ')
  96.         {
  97.             if (check_sp(cub, i, j))
  98.             {
  99.                 i++;
  100.             }
  101.             // if ((check_around(cub, ' ', i, j) || check_around(cub, '1', i, j)) && !check_around(cub, '0', i, j))
  102.             // {
  103.             //  i++;
  104.             // }
  105.             else
  106.                 ft_error("invalid map line check space");
  107.         }
  108.         if (cub.parse.map[j][i] == '1')
  109.         {
  110.             if (check_around(cub, '1', i, j) || check_around(cub, ' ', i, j) || check_around(cub, '0', i, j) )
  111.             {
  112.                 i++;
  113.             }
  114.             else
  115.                 ft_error("invalid map line check space");
  116.         }
  117.         if (cub.parse.map[j][i] == '0')
  118.         {
  119.             if (check_zr(cub, i, j))
  120.             {
  121.                 i++;
  122.             }
  123.             // if ((check_around(cub, '0', i, j) || check_around(cub, '1', i, j)) && !check_around(cub, ' ', i, j))
  124.             // {
  125.                 // printf("coucou\n");
  126.                 // i++;
  127.             // }
  128.             else
  129.                 ft_error("invalid map line check 0 ");
  130.             // printf("cicj\n");
  131.         }
  132.     }
  133. }
  134. void    first_line_check(t_cub cub)
  135. {
  136.     int i;
  137.  
  138.     i = 0;
  139.     while (cub.parse.map[0][i])
  140.     {
  141.         if (cub.parse.map[0][i] == '1' || cub.parse.map[0][i] == ' ')
  142.         {
  143.             i++;
  144.         }
  145.         else
  146.             ft_error("First line ne contient pas que des espaces ou des 1\n");
  147.     }
  148. }
  149. void    last_line_check(t_cub cub)
  150. {
  151.     int i;
  152.  
  153.     i = 0;
  154.     while (cub.parse.map[cub.parse.nbline-1][i])
  155.     {
  156.         if (cub.parse.map[cub.parse.nbline-1][i] == '1' || cub.parse.map[cub.parse.nbline-1][i] == ' ')
  157.         {
  158.             i++;
  159.         }
  160.         else
  161.         {
  162.             ft_error("Last line ne contient pas que des espaces ou des 1\n");
  163.         }
  164.     }
  165. }
  166.  
  167. void    show_map(t_cub cub)
  168. {
  169.     int x;
  170.  
  171.     x = 0;
  172.     while(x < cub.parse.nbline)
  173.     {
  174.         printf("%s\n",cub.parse.map[x]);
  175.         x++;
  176.     }
  177. }
  178.  
  179. int check_map(t_cub cub)
  180. {
  181.     int x;
  182.     int y;
  183.     int i;
  184.  
  185.     y = 0;
  186.     i = 0;
  187.     first_line_check(cub);
  188.     last_line_check(cub);
  189.     return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement